This repository has been archived on 2019-12-04. You can view files and clone it, but cannot push or open issues or pull requests.
simple-audiotools/conv2ogg

111 lines
2.8 KiB
Perl
Executable File

#!/usr/bin/perl
#
# Ogg converter
# usage: conv2ogg <files>
#
# Creates ogg files from the selected audio files
#
# Requires one of mpg321, mpg123 or mplayer, and oggenc
# Requires mplayer for support for non-mp3 filetypes
$VERSION = "git";
$HELP_MSG = "# Audio file - Ogg Converter v$VERSION\n\tusage: conv2ogg [-q] <files>\n" .
"\t\t-q <number>: Use quality for encoding. Default 10";
$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')";
use File::Path qw(make_path remove_tree);
chomp($tempdir = '/tmp/conv2ogg-' . `whoami`);
$quality = 7;
# Do we need help?
foreach (@ARGV)
{
if (/^(--help)|(-h)$/) { die $HELP_MSG; }
if ($qualityOnNext)
{
$quality = $_;
$qualityOnNext = 0;
}
elsif (/^-q$/) { $qualityOnNext = 1; }
}
# 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
remove_tree $tempdir;
make_path $tempdir;
foreach(@ARGV)
{
my $directory;
if (/\//)
{
$directory = $_;
$directory =~ s|^(.*)/.*?$|\1|;
}
else { $directory = '.'; }
$filename = $_;
$filename =~ s|^.*/(.*)?$|\1|;
$short_name = $filename;
if ($decoder =~ /mplayer/)
{
next if (/\.ogg/);
$short_name =~ s/\..*?$//;
}
else
{
next unless (/\.mp3$/);
$short_name =~ s/\.mp3$//;
}
# Transcode the file
system("$decoder $directory/$filename $decoder_options$tempdir/$short_name.wav");
system($encoder, "-q $quality", "$tempdir/$short_name.wav", '-o', "$directory/$short_name.ogg");
# Remove old file
system('rm', "$directory/$filename");
}
# Remove last of the cruft
remove_tree $tempdir;
# 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