X-Git-Url: https://diplodocus.org/git/flac-archive/blobdiff_plain/c069d00b1502aa08be2bdf0a197ab1110ba350d5..102f28bd8e396fdeff773afb69c30649750c3b0a:/fa-flacd diff --git a/fa-flacd b/fa-flacd index 3e9e7cb..2064135 100755 --- a/fa-flacd +++ b/fa-flacd @@ -13,6 +13,107 @@ B [B<-j> I] [B<-v>] =cut +package Jobs; + +use strict; +use warnings; + +use Errno; +use POSIX ':sys_wait_h'; + +sub newjob { + my $f = shift; + my $jobs = shift; + my $debug = shift; + my $pid; + + if (not $debug) { + $pid = fork(); + if (not defined($pid)) { + die("fork: $!"); + } + } + + if ($debug or $pid == 0) { + exit($f->()); + } + + if ($pid == 0) { + exit($f->()); + } + + push(@$jobs, $pid); + + return $pid; +} + +sub deljob { + my $pid = shift; + my $status = shift; + my $jobs = shift; + + for (my $i = 0; $i <= $#$jobs; $i++) { + if ($pid == $jobs->[$i]) { + splice(@$jobs, $i, 1); + last; + } + } + + return ($pid, $status); +} + +sub run { + my %o = @_; + my $maxjobs = $o{'max-jobs'}; + my $get_job = $o{'get-job'}; + my $notify_start = $o{'notify-start'}; + my $notify_finish = $o{'notify-finish'}; + my @jobs; + my $pid; + + # Call notifier function if given. + sub call { + my $f = shift or return; + ref($f) eq 'CODE' or return; + $f->(@_); + } + + while (1) { + if (@jobs <= $maxjobs) { + my $job; + while (defined($job = $get_job->())) { + $pid = newjob($job, \@jobs, $o{'debug'}); + call($notify_start, $pid, @jobs); + @jobs < $maxjobs or last; + } + + # No jobs running and get-job returned undef; we're finished. + if (@jobs == 0 and not defined($job)) { + return; + } + } + + # Now running as many jobs as we can, block waiting for one to die. + do { + $pid = waitpid(-1, 0); + } while ($pid == 0 + or ($pid == -1 and ($!{ECHILD} or $!{EINTR}))); + $pid == -1 and die("waitpid(-1): $!"); + + # Before starting more, see if any others have finished. + do { + call($notify_finish, deljob($pid, $?, \@jobs), @jobs); + } while (($pid = waitpid(-1, WNOHANG)) > 0); + if ($pid == -1) { + $!{ECHILD} or $!{EINTR} or die("waitpid(-1): $!"); + } + } +} + + +################################################################################ +package main; + use strict; use warnings; @@ -21,9 +122,8 @@ use Getopt::Long qw(:config gnu_getopt no_ignore_case); use POSIX ':sys_wait_h'; use Pod::Usage; +my $debug; my $verbose; -my @jobs; -my @finished; sub verbose { $verbose and print(STDERR $_) for @_; @@ -37,6 +137,7 @@ sub get_tags { my $value; my $artist; my $album; + my $discnum; my @tags; verbose("Opening tags file $fn\n"); @@ -47,49 +148,51 @@ sub get_tags { ($tag, $value) = split(/=/, $_, 2); - if (/^ARTIST=/) { + if (/^ARTIST=/i) { $artist = $value; verbose("ARTIST $artist from $fn\n"); - } elsif (/^ALBUM=/) { + } elsif (/^ALBUM=/i) { $album = $value; - verbose("ALBUM $album from $fn\n"); + verbose("ALBUM $album from $fn\n"); # cperl-mode sucks " + } elsif (/^DISCNUMBER=/i) { + $discnum = int($value); + verbose("DISCNUMBER $discnum from $fn\n"); } } close(TAGS) or die("close($fn): $!"); - return ($artist, $album, @tags); + return ($artist, $album, $discnum, @tags); } -# Process the fa-rip output in the directory DIR. -sub flac { - my $dir = shift; - my $artist; - my $album; - my @tags; - my $outfile; - my $status; - - verbose("Renaming $dir/tags\n"); - rename("$dir/tags", "$dir/using-tags") - or die("rename($dir/tags, $dir/using-tags): $!"); +sub track_tags { + my $h = shift; + my @result; - ($artist, $album, @tags) = get_tags("$dir/using-tags"); + while (my ($key, $vall) = each(%$h)) { + for my $val (@$vall) { + push(@result, "$key=$val") + } + } - verbose("mkdir($artist)\n"); - -d $artist or mkdir($artist) or die("mkdir($artist): $!"); + return @result; +} - verbose("chdir($dir)\n"); - chdir($dir) or die("chdir($dir): $!"); +sub run_flac { + my $infile = shift; + my $cue = shift; + my $outfile = shift; - $outfile = "$album"; - $outfile =~ s/\//_/g; + my @cue; + if (defined($cue)) { + @cue = ('--cuesheet', $cue); + } verbose("Running flac\n"); - $status = system('flac', '-o', "../$artist/$outfile.flac-tmp", - '--delete-input-file', '-V', '--cuesheet', - 'cue', '--no-padding', '--best', - map({ ('-T', $_) } @tags), - 'wav'); + my $status = system('flac', '-o', "$outfile.flac-tmp", + '--delete-input-file', '-V', '--no-padding', '--best', + @cue, + map({ ('-T', $_) } @_), + $infile); if (WIFEXITED($status)) { if (($status = WEXITSTATUS($status)) != 0) { die("flac exited with status $status"); @@ -102,93 +205,161 @@ sub flac { die("Major horkage on system(flac): \$? = $? \$! = $!"); } - verbose("Cleaning up $dir\n"); - unlink('using-tags') or die("unlink(using-tags): $!"); - unlink('cue') or die("unlink(cue): $!"); - rename('log', "../$artist/$outfile.log") - or die("rename(log, ../$artist/$outfile.log): $!"); - chdir('..') or die("chdir(..): $!"); - rmdir($dir) or die("rmdir($dir): $!"); - - rename("$artist/$outfile.flac-tmp", "$artist/$outfile.flac") - or die("rename($artist/$outfile.flac-tmp, $artist/$outfile.flac): $!"); - - return 0; + rename("$outfile.flac-tmp", "$outfile.flac") + or die("rename($outfile.flac-tmp, $outfile.flac): $!"); } -sub reaper { - my $pid; +# Process the fa-rip output in the directory DIR. +sub flac { + my $dir = shift; + my $artist; + my $album; + my $discnum; + my @tags; + my $single_file = -e "$dir/wav"; + my $outdir; + my $outfile; + my $outlog; + my @files; - while (($pid = waitpid(-1, WNOHANG)) > 0) { - push(@finished, [$pid, $?]); - } + verbose("Renaming $dir/tags\n"); + rename("$dir/tags", "$dir/using-tags") + or die("rename($dir/tags, $dir/using-tags): $!"); - $SIG{CHLD} = \&reaper; -} + ($artist, $album, $discnum, @tags) = get_tags("$dir/using-tags"); + for ($artist, $album) { + s|/|_|g; + } -sub newjob { - my $dir = shift; - my $pid; + verbose("mkdir($artist)\n"); + -d $artist or mkdir($artist) or die("mkdir($artist): $!"); - $pid = fork(); - if (not defined($pid)) { - die("fork: $!"); - } elsif ($pid == 0) { - $SIG{CHLD} = 'IGNORE'; - open(STDERR, ">$dir/log") or die("open(STDERR, >$dir/log): $!"); - exit(flac($dir)); + if (not $single_file) { + $outdir = "$artist/$album"; + verbose("mkdir($outdir)\n"); + -d "$outdir" or mkdir("$outdir") or die("mkdir($outdir): $!"); } - verbose("new job $pid for $dir\n"); - return $pid; -} - -sub deljob { - my $i = shift; - my $j; - my $pid; - my $status; + verbose("chdir($dir)\n"); + chdir($dir) or die("chdir($dir): $!"); - $pid = $finished[$i][0]; - $status = $finished[$i][1]; + if ($single_file) { + $outfile = $album; + defined($discnum) and $outfile .= " (disc $discnum)"; + run_flac('wav', 'cue', "../$artist/$outfile", @tags); + $outlog = "../$artist/$outfile.log"; + @files = ("$artist/$outfile.flac"); + } else { + # Go over @tags, store all [n] tags in a list keyed by n in + # %tracks_to_tags, store all ARTIST (not ARTIST[n]) tags in + # @disc_artist, and leave the rest in @tags. + my %tracks_to_tags; + my @disc_artist; + my @tmp; + for my $tag (@tags) { + if ($tag =~ /^([^[]+)\[(\d+)]=(.*)/) { + push(@{$tracks_to_tags{$2}->{$1}}, $3); + } elsif ($tag =~ /^ARTIST=/) { + push(@disc_artist, $tag); + } else { + push(@tmp, $tag); + } + } + @tags = @tmp; + + for my $tracknum (sort(map(int, keys(%tracks_to_tags)))) { + my $title = join(' ', map(split, @{$tracks_to_tags{$tracknum}->{'TITLE'}})); + $title =~ s|/|_|g; + $outfile = join('/', + $outdir, + join(' ', + (defined($discnum) + ? sprintf('%02d', $discnum) + : ()), + sprintf('%02d', $tracknum), + $title)); + + # If we have ARTIST[n] tags for this track, set + # @track_artist to the empty list; they will go in along + # with the other [n] tags. + my @track_artist; + if (exists($tracks_to_tags{$tracknum}->{'ARTIST'})) { + @track_artist = (); + } else { + @track_artist = @disc_artist; + } - verbose("$pid finished ("); - if (WIFEXITED($status)) { - verbose('exited with status ', WEXITSTATUS($status)); - } elsif (WIFSIGNALED($status)) { - verbose('killed with signal ', WTERMSIG($status)); - } elsif (WIFSTOPPED($status)) { - verbose('stopped with signal ', WSTOPSIG($status)); + run_flac(sprintf('track%02d.cdda.wav', $tracknum), undef, + "../$outfile", + @track_artist, + @tags, + "TRACKNUMBER=$tracknum", + track_tags($tracks_to_tags{$tracknum})); + push(@files, "$outfile.flac"); + } + $outlog = "../$outdir/log"; } - verbose(")\n"); - for ($j = 0; $j <= $#jobs; $j++) { - $pid == $jobs[$j] and splice(@jobs, $j, 1) and last; + verbose("Cleaning up $dir\n"); + unlink('using-tags') or die("unlink(using-tags): $!"); + unlink('cue') or die("unlink(cue): $!"); + rename('log', $outlog) + or die("rename(log, $outlog): $!"); + chdir('..') or die("chdir(..): $!"); + + if (-x "$dir/post-processor") { + verbose(join(' ', "Running ./$dir/post-processor", @files), "\n"); + system("./$dir/post-processor", @files); + unlink("$dir/post-processor") or die("unlink($dir/post-processor): $!"); } - splice(@finished, $i, 1); + rmdir($dir) or die("rmdir($dir): $!"); + + return 0; } sub flacloop { my $MAXJOBS = shift; - my $i; - my $j; + my @jobs; + my $dir; - - $SIG{CHLD} = \&reaper; while (1) { - if (scalar(@jobs) <= $MAXJOBS) { - foreach $i (glob('*/tags')) { - push(@jobs, newjob(dirname($i))) <= $MAXJOBS or last; - } + if (scalar(@jobs = glob('*/tags')) == 0) { + sleep(5); + next; } - for ($i = 0; $i <= $#finished; $i++) { - deljob($i); - } - - verbose(scalar(@jobs), " jobs\n"); - sleep(5); + Jobs::run('max-jobs'=>$MAXJOBS, + 'debug'=>$debug, + 'get-job'=>sub { + my $bork = shift(@jobs) or return; + $dir = dirname($bork); + return sub { + open(STDERR, ">$dir/log") + or die("open(STDERR, >$dir/log): $!"); + return flac($dir); + } + }, + + 'notify-start'=>sub { + my $pid = shift; + verbose("new job $pid for $dir\n"); + verbose(scalar(@_), " jobs\n"); + }, + + 'notify-finish'=>sub { + my $pid = shift; + my $status = shift; + verbose("$pid finished ("); + if (WIFEXITED($status)) { + verbose('exited with status ', WEXITSTATUS($status)); + } elsif (WIFSIGNALED($status)) { + verbose('killed with signal ', WTERMSIG($status)); + } elsif (WIFSTOPPED($status)) { + verbose('stopped with signal ', WSTOPSIG($status)); + } + verbose(")\n"); + }); } } @@ -198,6 +369,7 @@ MAIN: { $jobs = 4; GetOptions( + 'debug|X' => \$debug, 'jobs|j=i' => \$jobs, 'verbose|v' => \$verbose, 'help|h|?' => \$help, @@ -241,6 +413,17 @@ the "log" file. Finally, B moves the "cue" and "log" files to the artist directory (named by album) and removes the temporary directory. +If the temporary directory contains an executable file named +"post-processor", B executes that file with the relative +path to the output FLAC file as an argument. The output files are in +their final location when "post-processor" starts. Possible uses are +running B, moving the output files to a different location, +removing the lock file, or adding to a database. The standard input, +output, and error streams are inherited from B, so they may +be connected to anything from a tty to /dev/null. This means that you +may want to redirect these streams, if you want to save them or do any +logging. + =head1 OPTIONS =over 4