commit 52abe0dd3ced5b45d4eafd798500df42aa35ef79 Author: Dan Ponte Date: Sun Nov 6 10:52:40 2011 -0500 Initial. diff --git a/README b/README new file mode 100644 index 0000000..9832eaa --- /dev/null +++ b/README @@ -0,0 +1,24 @@ +mfgen.pl - a Perl script to generate makefiles to encode audio to different formats. +(C)2006-2011, Dan Ponte + +Rationale: +Generating makefiles allows you to use make -j, which allows you to utilize multiple CPUs in an SMP configuration to +encode more than one file at once. + +Help: + +usage: mfgen.pl [-hmMOR] [-y year] [-g genre] [-A artist] [-L album] [-d disc#] [-p path] + + -h : this help + -m : album is part multi disc set (writes disc in filename) + -y year : set year for album + -g genre : set genre for album + -A artist : set artist for album + -L album : set title of album + -d disc# : set disc number (default 1) + -p path : path prefix (where to put output files) + -M : generate MP3s (must be used with -O for MP3 and vorbis) + -O : generate Ogg Vorbis + -F : generate FLAC (must be used with -O for FLAC and vorbis, or -M) + -R : Rename (actually copy) and tag MP3 (see above notes) + diff --git a/mfgen.pl b/mfgen.pl new file mode 100755 index 0000000..c16556a --- /dev/null +++ b/mfgen.pl @@ -0,0 +1,294 @@ +#!/usr/bin/perl +use vars qw/ %opt /; +our @ofls; +our @fls; +our $ppref; +our $argc = $#ARGV + 1; +our $genre, $outfile; +our $do_ogg = 1, $do_mp3 = 0, $do_flac = 0, $do_rename = 0; +our $disart, $disalb, $albdisc = 1, $yearstr, $multidisc = 0, $overart = 0, $overalb = 0; + +$outfile = "Makefile"; + +#if($argc < 3) { +# print STDERR "Usage: $0 \n"; +# exit 1; +#} + +#$genre = $ARGV[0]; +#$yearstr = $ARGV[1]; +sub optinit { + use Getopt::Std; + my $opts = 'y:g:mA:L:d:hp:MOFR'; + getopts($opts, \%opt) or usage(); + usage() if $opt{h}; + if($opt{y}) { + $yearstr = $opt{y}; + } + if($opt{M}) { + if(!$opt{O}) { + $do_ogg = 0; + } + $do_mp3 = 1; + } + if($opt{F}) { + if(!$opt{O}) { + $do_ogg = 0; + } + $do_flac = 1; + } + if($opt{R}) { + if(!$opt{O}) { + $do_ogg = 0; + } + $do_rename = 1; + } + if($opt{O}) { + $do_ogg = 1; + } + if($opt{g}) { + $genre = $opt{g}; + } + if($opt{m}) { + $multidisc = 1; + } + if($opt{A}) { + $disart = $opt{A}; + $overart = 1; # artist override + } + if($opt{p}) { + $ppref = $opt{p}; + } + if($opt{L}) { + $disalb = $opt{L}; + $overalb = 1; # album override + } + if($opt{d}) { + $albdisc = $opt{d}; + } +} + +sub usage { + print STDERR < $g, fmt => $6, artist => ($overart ? $disart : $1), album => ($overalb ? $disalb : $2), track => $3, title => $5); + $disart = $tef{'artist'}; + $disalb = $tef{'album'}; + if($tef{'title'} eq "Fillmein") { + $tef{'title'} = askuser("title", $g); + } + push @ofls, \%tef; + } elsif($g =~ /([0-9]{1,2})( - | )(.+)\.(wav|flac|mp3)$/) { + my $artist, $album; + if(length($disart) == 0) { + $artist = askuser("artist", $g); + } else { + $artist = $disart; + } + if(length($disalb) == 0) { + $album = askuser("album", $g); + } else { + $album = $disalb; + } + my %tef = (file => $g, fmt => $4, artist => ($overart ? $disart : $1), album => ($overalb ? $disalb : $3), track => $1, title => $3); + $disart = $tef{'artist'}; + $disalb = $tef{'album'}; + if($tef{'title'} eq "Fillmein") { + $tef{'title'} = askuser("title", $g); + } + push @ofls, \%tef; + } else { + printf("I have no clue how to deal with '%s'! Manual metadata entry required.\n", $g); + } +} + +if(@ofls < 1) { + print STDERR "There were no files to work on, dying.\n"; + exit 2; +} + +open(OUTFILE, ">$outfile") or die("Cannot open $outfile for writing."); + +print OUTFILE "# Generated by oge.pl\n"; + +if($do_ogg) { + printf OUTFILE ("ogg: "); + + for($i = 0; $i < @ofls; $i++) { + printf OUTFILE ("tr%02dogg ", $i + 1); + } + printf OUTFILE ("\n"); + printf OUTFILE "# ogg stuff\n"; +} + +if($do_mp3) { + printf OUTFILE ("mp3: "); + + for($i = 0; $i < @ofls; $i++) { + printf OUTFILE ("tr%02dmp3 ", $i + 1); + } + printf OUTFILE ("\n"); + printf OUTFILE "# mp3 stuff\n"; +} + +if($do_flac) { + printf OUTFILE ("flac: "); + + for($i = 0; $i < @ofls; $i++) { + printf OUTFILE ("tr%02dflac ", $i + 1); + } + printf OUTFILE ("\n"); + printf OUTFILE "# flac stuff\n"; +} + +if($do_rename) { + printf OUTFILE ("rename: "); + for($i = 0; $i < @ofls; $i++) { + printf OUTFILE ("tr%02dren ", $i + 1); + } + printf OUTFILE ("\n"); + printf OUTFILE ("# rename stuff\n"); +} + + + +$i = 1; + +foreach $h (@ofls) { +# $c =~ s/'/'\\''/g; + my %c = %{$h}; + my ($artist, $album, $tdisc, $tr, $title) = ($c{'artist'}, $c{'album'}, $albdisc, $c{'track'}, $c{'title'}); +# $tdisc =~ s/-//; + my $disc, $track, $ofn; + $disc = $albdisc; + $track = $tr; + if($multidisc) { + $ofn = sprintf("%s - %s - %02d-%02d %s", $artist, $album, $disc, $track, $title); + } else { + $ofn = sprintf("%s - %s - %02d %s", $artist, $album, $track, $title); + } + $ofn =~ s/\//-/g; + if($do_ogg) { + printf OUTFILE ("tr%02dogg:\n\toggenc -q 6 -o '%s' %s -G '%s' -a '%s' -t '%s' -l '%s' -N %d -c 'DISCNUMBER=%d' '%s'\n", $i, nl(($ppref ne '' ? $ppref . '/' . $ofn : $ofn) . ".ogg"), + $yearstr ne '' ? "-d '" . nl($yearstr) . "'" : '', + nl($genre), nl($artist), nl($title), nl($album), nl($track), nl($disc), nl($c{'file'})); + } + if($do_mp3) { + printf OUTFILE ("tr%02dmp3:\n\tlame -V 2 %s --add-id3v2 --tg '%s' --ta '%s' --tt '%s' --tl '%s' --tn %d '%s' '%s'\n", $i, + $yearstr ne '' ? "--ty '" . nl($yearstr) . "'" : '', + nl($genre), nl($artist), nl($title), nl($album), nl($track), nl($c{'file'}), nl(($ppref ne '' ? $ppref . '/' . $ofn : $ofn)) . ".mp3"); + if($multidisc) { + printf OUTFILE ("\tid3v2 --TPOS '%d' '%s'\n", nl($disc), nl(($ppref ne '' ? $ppref . '/' . $ofn : $ofn)) . ".mp3"); + } + } + if($do_rename) { + my $tfpath = nl(($ppref ne '' ? $ppref . '/' . $ofn : $ofn)) . ".mp3"; + printf OUTFILE ("tr%02dren:\n\tcp -v '%s' '%s'\n\tid3tag -a '%s' -A '%s' -s '%s' -t '%s' %s '%s'\n" + , $i, nl($c{'file'}), $tfpath, nl($artist), nl($album), nl($title), nl($track), + $yearstr ne '' ? "-y '" . nl($yearstr) . "'" : '', $tfpath); + if($multidisc) { + printf OUTFILE ("\tid2v2 --TPOS '%d' '%s'\n", nl($disc), $tfpath); + } + } + if($do_flac) { + printf OUTFILE ("tr%02dflac:\n\tflac --best --replay-gain %s -T 'genre=%s' -T 'artist=%s' -T 'title=%s' -T 'album=%s' -T 'track=%d' -T 'TRACKNUMBER=%d' -T 'DISCNUMBER=%d' -o '%s' '%s'\n", $i, + $yearstr ne '' ? "-T 'year=" . nl($yearstr) . "'" : '', + nl($genre), nl($artist), nl($title), nl($album), nl($track), nl($track), nl($disc), nl(($ppref ne '' ? $ppref . '/' . $ofn : $ofn)) . ".flac", nl($c{'file'})); + } + + + $i++; +} + +close OUTFILE; +print "Done! Saved to $outfile\n"; + +sub nl { #normalise for shell + my $c = shift; + $c =~ s/'/'\\''/g; + return $c; +} + +sub newfname { + my $ofn = shift; + my $srcf = shift; + open(INF, $ofn); + my $artist, $album, $title, $tracknum, $ctn; + my %tds; + foreach $cl () { + if($cl =~ /^Performer=\s*'(.+)'$/) { + $artist = $1; + } elsif($cl =~ /^Albumtitle=\s*'(.+)'$/) { + $album = $1; + } elsif($cl =~ /^Tracktitle=\s*'(.+)'$/) { + $title = $1; + } elsif($cl =~ /^Tracknumber=\s*([0-9]+)$/) { + $tracknum = $1; + } + } + + close(INF); + + %tds = (file => $srcf, artist => $artist, album => $album, track => $tracknum, title => $title); + return %tds; +} + +sub askuser { + my $field = shift; + my $fn = shift; + my $answer; + + printf("What is the %s for '%s'?> ", $field, $fn); + $answer = <>; + chop $answer; # newline + return $answer; +}