Skip to content

Instantly share code, notes, and snippets.

@philchristensen
Created October 29, 2010 17:16
Show Gist options
  • Save philchristensen/653927 to your computer and use it in GitHub Desktop.
Save philchristensen/653927 to your computer and use it in GitHub Desktop.
Script to automate converting batches of audio files
SoundGlue
by Phil Christensen
9-18-2005
This is a script that will handle the ins and outs of converting popular lossless
sound formats to mp3. To the purists, I say, "go to hell!", and to everyone else
I recommend not distributing the lossy files back to the world.
This has some tie-ins with etree.org, or more specifically, with FurtherNet,
although since the advent of BitTorrent, I guess FN doesn't get used quite as
much anymore.
INSTALL
--------
You'll need run the install.sh script as root:
prompt# sh install.sh
which will install the necessary things in the necessary places.
You will also need to add /usr/local/bin to your $PATH variable, if you haven't
already. Assuming your shell is bash, you should be able to run the following:
prompt# export PATH=$PATH:/usr/local/bin
After everything works, you can add that line (minus the prompt, of course) to
your ~/.profile file.
CONVERTING FLAC
----------------
If you wish to convert a directory of flac files called 'music' to mp3:
prompt# flac2mp3 music/
This will create the .mp3 files at the same level as the flac file.
You can also add an argument for a folder to extract to:
prompt# flac2mp3 music/ converted-music/
which is a bit more convenient. You can also convert a single file by specifying
it instead of a directory.
CONVERTING SHORTEN
-------------------
Shorten conversion works the same way:
prompt# shn2mp3 music/
-OR-
prompt# shn2mp3 music/ converted-music/
DETAILS
--------
* All mp3s are created at 256kbps, CBR.
* Furthur incomplete directories (featuring a 'DO_NOT_EDIT' file) will be ignored.
* After conversion, the script creates an invisible '.converted' file in the
converted directory. Directories containing this file will be ignored. If you
need to convert a directory again, just delete this file from the command-line.
#!/usr/bin/perl
use strict;
use Options;
my (@files_to_convert, @dir_of_files);
my $output_dir;
my %input_map = ('shn' => ['shorten -x "', '" -'],
'ogg' => ['oggdec -o - "', '"'],
'flac' => ['flac -s -d -c "', '"']);
my %output_map = ('mp3' => ['ffmpeg -i - -f mp3 -acodec mp3 -ab 256 "', '"']);
my @inout = ($0 =~ m/(\w+?)2(\w+)/);
unless(defined $input_map{$inout[0]}){
print "$inout[0] is not a supported input type.\n";
exit(1);
}
unless(defined $output_map{$inout[1]}){
print "$inout[1] is not a supported output type.\n";
exit(1);
}
if(-d $ARGV[0]){
$ARGV[0] =~ s/\/$//;
opendir(DIR, $ARGV[0]);
@dir_of_files = readdir DIR;
closedir DIR;
if(-e "$ARGV[0]/.converted"){
print "This directory has been converted already. (Delete the .converted file if you wish to convert again.)\n";
exit(1);
}
if(grep /DO_NOT_EDIT/, @dir_of_files){
print "This is an incomplete Furthur directory.\n";
exit(1);
}
@files_to_convert = grep /.$inout[0]$/, map "$ARGV[0]/$_", @dir_of_files;
unless(@files_to_convert){
print "There are no .$inout[0] files in the specified directory.\n";
exit(1);
}
}
elsif(-e $ARGV[0] && $ARGV[0] =~ m/\.$inout[0]$/){
@files_to_convert = ($ARGV[0]);
}
else{
print "You must specify a .$inout[0] file, or a directory of files to convert.\n";
exit(1);
}
if($ARGV[1] && -d $ARGV[1]){
$output_dir = $ARGV[1];
}
foreach my $file (@files_to_convert){
$file =~ m/(.*?)\.$inout[0]$/;
my $output_file = "$1.$inout[1]";
if($output_dir){
if($output_file =~ /\//){
($output_file) = ($output_file =~ m/\/([^\/]*)$/);
}
if($output_dir =~ /\/$/){
chop($output_dir);
}
$output_file = "$output_dir/$output_file";
}
print "Output file is $output_file\n";
my $command = $input_map{$inout[0]}->[0] . $file . $input_map{$inout[0]}->[1] . ' | ' .
$output_map{$inout[1]}->[0] . $output_file . $output_map{$inout[1]}->[1];
my $result = system($command);
if($result != 0){
print "Subprocess ended with non-zero result: $result...exiting.\n";
exit(1);
}
}
if(-d $ARGV[0]){
open(FILE, ">$ARGV[0]/.converted");
print FILE "DONE";
close(FILE);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment