#!/usr/local/bin/perl # BatchCode v. 0.02 # batmp3.pl (c) 1998 Chris Church (psylark@netropolis.net) # http://www.netropolis.net/aD/ringdown/ # Intended to provide (quick||dirty||easy) interface to l3enc/l3dec # for batch decoding / encoding of mp3's # # works under dos and linux systems. (And should work fine under # other UNIXes) # # Notes for DOS: # # be sure to use forward slashes (/) instead of backslashes (\) # in dos file names and paths. (except in the paths to l3enc & # l3dec, see comments above these variables for instructions.) # # the dos filenames to be en/decoded must follow the 8.3 # format of older dos's even in win95 and winnt - l3enc/dec needs # this for some strange reason. # # setup : simply edit the $lencp and $ldecp variables below and # put in your paths to l3enc / l3dec. Edit the $dos variable # to reflect whether you're runng it under a dos or *NIX based # system. (1 = dos, 0 = other) # # run batmp3.pl with no command line switches for usage # # 1/11/98 #--------------------------------------------------------------- require 5.001; $| = 1; # the path to our compression/decompression program(s) # we need to use double-backslashes (\\) here (and here only!) # for DOS paths $lencp = 'E:\\store\\lenc\\L3V272.DOS\\l3enc.exe'; $ldecp = 'E:\\store\\lenc\\L3V272.DOS\\l3dec.exe'; # are we in dos or unix ? $dos = 0; # formats supported : @formats = ("wav","pcm","au","rif","mp3","aiff"); # bomb if we don't have at least the very needed var's if($#ARGV < 1) { &help; exit; } # get out dir containing files to be converted, # dir to put them in, and mix rate $QDIR = $ARGV[0]; $ODIR = $ARGV[1]; # check for switches foreach $arg (@ARGV) { if($arg eq $ARGV[0] || $arg eq $ARGV[1]) { next; } if($arg =~ /-mrate$/) { &help; exit; } elsif($arg =~ /-br$/) { &help; exit; } elsif($arg =~ /-mrate(\S+)/) { $mrate = $1; } elsif($arg =~ /-br(\S+)/) { $brate = $1; } elsif($arg =~ /-unmp/) { $unmp = 1; } elsif($arg =~ /-wav/) { $wav = 1; } elsif($arg =~ /-hq/) { $hq = 1; } elsif($arg =~ /-mono/) { $mono = 1; } else { &help; exit; } } # we do this to avoid any problems opening files undef(@ARGV); # choose our clear screen proggy $clp = 'cls' if($dos == 1); $clp = '/usr/bin/tput clear' if($dos != 1); # open our directory and read in our files, # check for compatability with l3enc. system("$clp"); print("BatchCode v. 0.02 (c) 1998 Chris Church (psylark\@netropolis.net)\n"); opendir(QDIR, $QDIR) || die("$0: Unable to open directory $QDIR -> $!\n"); @afiles = grep(!/^\./, readdir(QDIR)); closedir(QDIR); foreach $fi (@afiles) { if($fi =~ /.*\.(\w+)$/) { foreach $for (@formats) { if($1 =~ /$for/i) { print("Adding $fi to conversion list\n"); push(@conv, $fi); last; } } } } # decide to encode or decode based on command line switch if($unmp != 1) { &encode; } else { &decode; } exit; # decoding sub decode { print("Beginning Decoding process\n"); print("Defining switches for l3dec\n"); $mr = "-sr $mrate" if(defined($mrate)); $br = "-br $brate" if(defined($brate)); $hq = "-hq" if($hq == 1); $mono = "-tfc 1" if($mono == 1); $wav = "-wav" if($wav == 1); # go through all of our files in our list and drop those not in mp3 # format then convert foreach $cf (@conv) { if($cf !~ /.*\.mp3$/i) { print("Removing $cf from list - not in mp3 format\n"); next; } if($cf =~ /(.*)\.\w+$/) { $fn = $1; } else { $fn = $cf; } $ouf = $ODIR . "/" . $fn . "." . "pcm" if($wav != 1); $ouf = $ODIR . "/" . $fn . "." . "wav" if($wav == 1); $inf = $QDIR . "/" . $cf; print("Converting $inf to $ouf\n"); system("$ldecp $inf $ouf $br $mr $hq $mono $wav"); } } sub encode { print("Beginning Encoding process\n"); print("Defining switches for l3enc\n"); $mr = "-sr $mrate" if(defined($mrate)); $br = "-br $brate" if(defined($brate)); $hq = "-hq" if($hq == 1); $mono = "-tfc 1" if($mono == 1); foreach $cf (@conv) { if($cf =~ /.*\.mp3$/i) { print("Removing $cf from list - already in mp3 format\n"); next; } if($cf =~ /(.*)\.\w+$/) { $fn = $1; } else { $fn = $cf; } $ouf = $ODIR . "/" . $fn . "." . "mp3"; $inf = $QDIR . "/" . $cf; print("Converting $inf to $ouf\n"); system("$lencp $inf $ouf $br $mr $hq $mono"); } } sub help { system("$clp"); sleep(.1); print("BatchCode v. 0.02 (c) 1998 Chris Church (psylark\@netropolis.net)\n"); print("Usage: $0 switches readdir = directory in which files to be (en|de)coded are stored writedir = directory to store (en|de)coded files Optional Switches: -mrate Sampling rate (not used for RIFF/WAVE format input) -br Bitrate -mono Convert to mono format (not used for RIFF/WAVE format input) -hq Use high quality (more CPU usage!) -unmp Decode mp3's -wav Decode mp3 into WAVE format (only used with -unmp) Example: $0 D:/music E:/newmp3s -hq $0 /blah/mp3 /newav -hq -unmp -wav "); }