2009-03-20 05:54:35 +00:00
#!/usr/bin/perl
#
2010-03-25 19:14:06 +00:00
# Ogg converter
# usage: conv2ogg <files>
2009-03-20 05:54:35 +00:00
#
2010-03-25 19:14:06 +00:00
# Creates ogg files from the selected audio files
2009-03-20 05:54:35 +00:00
#
# Requires one of mpg321, mpg123 or mplayer, and oggenc
2010-03-25 19:14:06 +00:00
# Requires mplayer for support for non-mp3 filetypes
2009-03-20 05:54:35 +00:00
#
# Changelog:
2010-03-25 19:14:06 +00:00
# 0.2.4: Allow mplayer to handle any non-ogg file
# Make invocations of files in other directories work
# Use File::Path where appropriate
2010-09-14 03:14:57 +00:00
# Added -q option
2009-03-20 05:54:35 +00:00
# 0.2.1: Added new options to mplayer for mplayer's new features
# Added support for m4a if using mplayer
# 0.2: Aligned version with ds-audiotools
# 0.1.3: Added mplayer support, automatically select decoder
# Fixed option passing to system() calls to be as secure as possible
# 0.1.2: added support for passing flags
# Use a temp dir for storage of intermediate wav file
# Cleaned up some syntax
# Added version variable
# 0.1: converted to perl
# changed to use command-line args
# 0.0.1: Initial bash script
2010-03-25 19:14:06 +00:00
$VERSION = "0.2.4";
2010-09-14 03:14:57 +00:00
$HELP_MSG = "# Audio file - Ogg Converter v$VERSION\n\tusage: conv2ogg [-q] <files>\n" .
"\t\t-q <number>: Use quality for encoding. Default 10";
2009-03-20 05:54:35 +00:00
$DEC_ERROR = "Couldn't find a decoder to use. Please install one of mplayer, mpg321, or mpg123";
$ENC_ERROR = "Couldn't find oggenc, which is necessary for encoding. Please install the package that provides oggenc (probably named 'oggenc' or 'vorbistools')";
2010-03-25 19:14:06 +00:00
use File::Path qw(make_path remove_tree);
chomp($tempdir = '/tmp/conv2ogg-' . `whoami`);
2009-03-20 05:54:35 +00:00
2010-09-14 03:14:57 +00:00
$quality = 7;
2009-03-20 05:54:35 +00:00
# Do we need help?
foreach (@ARGV)
{
if (/^(--help)|(-h)$/) { die $HELP_MSG; }
2010-09-14 03:14:57 +00:00
if ($qualityOnNext)
{
$quality = $_;
$qualityOnNext = 0;
}
elsif (/^-q$/) { $qualityOnNext = 1; }
2009-03-20 05:54:35 +00:00
}
# Select the encoder and decoder
chomp($decoder = `which mplayer`);
$decoder_options = '-vc null -vo null -ao pcm:fast:file=';
if (! -x $decoder)
{
$decoder_options = '-w '; # The others both use this format
chomp($decoder = `which mpg321`);
}
chomp($decoder = `which mpg123`) if (! -x $decoder);
chomp($encoder = `which oggenc`);
# Dependency checks
die $DEC_ERROR if (! -x $decoder);
die $ENC_ERROR if (! -x $encoder);
# Set up the temp directory
2010-03-25 19:14:06 +00:00
remove_tree $tempdir;
make_path $tempdir;
2009-03-20 05:54:35 +00:00
foreach(@ARGV)
{
2010-03-25 19:14:06 +00:00
my $directory;
if (/\//)
{
$directory = $_;
$directory =~ s|^(.*)/.*?$|\1|;
}
else { $directory = '.'; }
2009-03-20 05:54:35 +00:00
$filename = $_;
2010-03-25 19:14:06 +00:00
$filename =~ s|^.*/(.*)?$|\1|;
$short_name = $filename;
2009-03-20 05:54:35 +00:00
if ($decoder =~ /mplayer/)
{
2010-03-25 19:04:42 +00:00
next if (/\.ogg/);
$short_name =~ s/\..*?$//;
2009-03-20 05:54:35 +00:00
}
else
{
next unless (/\.mp3$/);
$short_name =~ s/\.mp3$//;
}
# Transcode the file
2010-03-25 19:14:06 +00:00
system("$decoder $directory/$filename $decoder_options$tempdir/$short_name.wav");
2010-09-14 03:14:57 +00:00
system($encoder, "-q $quality", "$tempdir/$short_name.wav", '-o', "$directory/$short_name.ogg");
2009-03-20 05:54:35 +00:00
2010-03-25 19:14:06 +00:00
# Remove old file
system('rm', "$directory/$filename");
2009-03-20 05:54:35 +00:00
}
# Remove last of the cruft
2010-03-25 19:14:06 +00:00
remove_tree $tempdir;
2009-03-20 05:54:35 +00:00
2010-03-25 19:14:06 +00:00
# Copyright (c) 2005 - 2010 John Wiggins
2009-03-20 05:54:35 +00:00
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA