]>
diplodocus.org Git - flac-archive/blob - fa-flacd
8 B<fa-flacd> - archive CDs to single FLAC files
12 B<fa-flacd> [B<-j> I<jobs>] [B<-v>]
22 use POSIX
':sys_wait_h';
32 if (not defined($pid)) {
37 if ($debug or $pid == 0) {
55 for (my $i = 0; $i <= $#$jobs; $i++) {
56 if ($pid == $jobs->[$i]) {
57 splice(@$jobs, $i, 1);
62 return ($pid, $status);
67 my $maxjobs = $o{'max-jobs'};
68 my $get_job = $o{'get-job'};
69 my $notify_start = $o{'notify-start'};
70 my $notify_finish = $o{'notify-finish'};
74 # Call notifier function if given.
76 my $f = shift or return;
77 ref($f) eq 'CODE' or return;
82 if (@jobs <= $maxjobs) {
84 while (defined($job = $get_job->())) {
85 $pid = newjob
($job, \
@jobs, $o{'debug'});
86 call
($notify_start, $pid, @jobs);
87 @jobs < $maxjobs or last;
90 # No jobs running and get-job returned undef; we're finished.
91 if (@jobs == 0 and not defined($job)) {
96 # Now running as many jobs as we can, block waiting for one to die.
98 $pid = waitpid(-1, 0);
100 or ($pid == -1 and ($!{ECHILD
} or $!{EINTR
})));
101 $pid == -1 and die("waitpid(-1): $!");
103 # Before starting more, see if any others have finished.
105 call
($notify_finish, deljob
($pid, $?, \
@jobs), @jobs);
106 } while (($pid = waitpid(-1, WNOHANG
)) > 0);
108 $!{ECHILD
} or $!{EINTR
} or die("waitpid(-1): $!");
114 ################################################################################
121 use Getopt
::Long
qw(:config gnu_getopt no_ignore_case);
122 use POSIX
':sys_wait_h';
129 $verbose and print(STDERR
$_) for @_;
132 # Return the ARTIST, ALBUM, and DATE followed by a list of all the
133 # lines in the file FN.
143 verbose
("Opening tags file $fn\n");
144 open(TAGS
, $fn) or die("open($fn): $!");
149 ($tag, $value) = split(/=/, $_, 2);
153 verbose
("ARTIST $artist from $fn\n");
154 } elsif (/^ALBUM=/i) {
156 verbose
("ALBUM $album from $fn\n"); # cperl-mode sucks "
157 } elsif (/^DISCNUMBER=/i) {
158 $discnum = int($value);
159 verbose
("DISCNUMBER $discnum from $fn\n");
162 close(TAGS
) or die("close($fn): $!");
164 return ($artist, $album, $discnum, @tags);
171 while (my ($key, $vall) = each(%$h)) {
172 for my $val (@$vall) {
173 push(@result, "$key=$val")
187 @cue = ('--cuesheet', $cue);
190 verbose
("Running flac\n");
191 my $status = system('flac', '-o', "$outfile.flac-tmp",
192 '--delete-input-file', '-V', '--no-padding', '--best',
194 map({ ('-T', $_) } @_),
196 if (WIFEXITED
($status)) {
197 if (($status = WEXITSTATUS
($status)) != 0) {
198 die("flac exited with status $status");
200 } elsif (WIFSIGNALED
($status)) {
201 die("flac killed with signal ", WTERMSIG
($status));
202 } elsif (WIFSTOPPED
($status)) {
203 die("flac stopped with signal ", WSTOPSIG
($status));
205 die("Major horkage on system(flac): \$? = $? \$! = $!");
208 rename("$outfile.flac-tmp", "$outfile.flac")
209 or die("rename($outfile.flac-tmp, $outfile.flac): $!");
212 # Process the fa-rip output in the directory DIR.
219 my $single_file = -e
"$dir/wav";
225 verbose
("Renaming $dir/tags\n");
226 rename("$dir/tags", "$dir/using-tags")
227 or die("rename($dir/tags, $dir/using-tags): $!");
229 ($artist, $album, $discnum, @tags) = get_tags
("$dir/using-tags");
230 for ($artist, $album) {
234 verbose
("mkdir($artist)\n");
235 -d
$artist or mkdir($artist) or die("mkdir($artist): $!");
237 if (not $single_file) {
238 $outdir = "$artist/$album";
239 verbose
("mkdir($outdir)\n");
240 -d
"$outdir" or mkdir("$outdir") or die("mkdir($outdir): $!");
243 verbose
("chdir($dir)\n");
244 chdir($dir) or die("chdir($dir): $!");
248 defined($discnum) and $outfile .= " (disc $discnum)";
249 run_flac
('wav', 'cue', "../$artist/$outfile", @tags);
250 $outlog = "../$artist/$outfile.log";
251 @files = ("$artist/$outfile.flac");
253 # Go over @tags, store all [n] tags in a list keyed by n in
254 # %tracks_to_tags, store all ARTIST (not ARTIST[n]) tags in
255 # @disc_artist, and leave the rest in @tags.
259 for my $tag (@tags) {
260 if ($tag =~ /^([^[]+)\[(\d+)]=(.*)/) {
261 push(@{$tracks_to_tags{$2}->{$1}}, $3);
262 } elsif ($tag =~ /^ARTIST=/) {
263 push(@disc_artist, $tag);
270 for my $tracknum (sort(map(int, keys(%tracks_to_tags)))) {
271 my $title = join(' ', map(split, @{$tracks_to_tags{$tracknum}->{'TITLE'}}));
277 ? sprintf('%02d', $discnum)
279 sprintf('%02d', $tracknum),
282 # If we have ARTIST[n] tags for this track, set
283 # @track_artist to the empty list; they will go in along
284 # with the other [n] tags.
286 if (exists($tracks_to_tags{$tracknum}->{'ARTIST'})) {
289 @track_artist = @disc_artist;
292 run_flac
(sprintf('track%02d.cdda.wav', $tracknum), undef,
296 "TRACKNUMBER=$tracknum",
297 track_tags
($tracks_to_tags{$tracknum}));
298 push(@files, "$outfile.flac");
300 $outlog = "../$outdir/log";
303 verbose
("Cleaning up $dir\n");
304 unlink('using-tags') or die("unlink(using-tags): $!");
305 unlink('cue') or die("unlink(cue): $!");
306 rename('log', $outlog)
307 or die("rename(log, $outlog): $!");
308 chdir('..') or die("chdir(..): $!");
310 if (-x
"$dir/post-processor") {
311 verbose
(join(' ', "Running ./$dir/post-processor", @files), "\n");
312 system("./$dir/post-processor", @files);
313 unlink("$dir/post-processor") or die("unlink($dir/post-processor): $!");
316 rmdir($dir) or die("rmdir($dir): $!");
327 if (scalar(@jobs = glob('*/tags')) == 0) {
332 Jobs
::run
('max-jobs'=>$MAXJOBS,
335 my $bork = shift(@jobs) or return;
336 $dir = dirname
($bork);
338 open(STDERR
, ">$dir/log")
339 or die("open(STDERR, >$dir/log): $!");
344 'notify-start'=>sub {
346 verbose
("new job $pid for $dir\n");
347 verbose
(scalar(@_), " jobs\n");
350 'notify-finish'=>sub {
353 verbose
("$pid finished (");
354 if (WIFEXITED
($status)) {
355 verbose
('exited with status ', WEXITSTATUS
($status));
356 } elsif (WIFSIGNALED
($status)) {
357 verbose
('killed with signal ', WTERMSIG
($status));
358 } elsif (WIFSTOPPED
($status)) {
359 verbose
('stopped with signal ', WSTOPSIG
($status));
372 'debug|X' => \
$debug,
373 'jobs|j=i' => \
$jobs,
374 'verbose|v' => \
$verbose,
375 'help|h|?' => \
$help,
377 $help and pod2usage
(-exitstatus
=>0, -verbose
=>1);
387 B<fa-flacd> and B<fa-rip> together comprise B<flac-archive>, a system
388 for archiving audio CDs to single FLAC files. B<fa-flacd> is the guts
389 of the system. It runs in the directory where the audio archives are
390 stored, scanning for new ripped CDs to encode and rename; it never
391 exits. B<fa-rip> generates the inputs for B<fa-flacd>: the ripped WAV
392 file, Vorbis tags, and a cuesheet.
394 Both programs expect to be run from the same directory. They use that
395 directory to manage directories named by artist. Intermediate files
396 are written to temporary directories here. B<fa-flacd> processes the
397 temporary directories into per-album files in the artist directories.
399 Every 5 seconds, B<fa-flacd> scans its current directory for
400 directories with a file called "tags" and creates a processing job for
401 each one. The number of jobs B<fa-flacd> attempts to run is
402 controlled by the B<-j> option and defaults to 4. B<fa-flacd> will
403 print diagnostic output when the B<-v> option is given.
405 A processing job first renames the directory's "tags" file to
406 "using-tags" so that B<ra-flacd> will not try to start another job for
407 this directory. This file is left as is when an error is encountered,
408 so a new job will not be started until the user corrects the error
409 condition and renames "using-tags" back to "tags". Next, it encodes
410 the "wav" file to a FLAC file, using the "cue" file for the cuesheet
411 and "using-tags" for Vorbis tags. Any diagnostic output is saved in
412 the "log" file. Finally, B<fa-flacd> moves the "cue" and "log" files
413 to the artist directory (named by album) and removes the temporary
416 If the temporary directory contains an executable file named
417 "post-processor", B<fa-flacd> executes that file with the relative
418 path to the output FLAC file as an argument. The output files are in
419 their final location when "post-processor" starts. Possible uses are
420 running B<flac2mp3>, moving the output files to a different location,
421 removing the lock file, or adding to a database. The standard input,
422 output, and error streams are inherited from B<fa-flacd>, so they may
423 be connected to anything from a tty to /dev/null. This means that you
424 may want to redirect these streams, if you want to save them or do any
431 =item B<-j> [B<--jobs>] I<jobs>
433 Run up to I<jobs> jobs instead of the default 4.
435 =item B<-v> [B<--verbose>]
437 Print diagnostic information.
443 Written by Eric Gillespie <epg@pretzelnet.org>.
445 flac-archive is free software; you may redistribute it and/or modify
446 it under the same terms as Perl itself.
451 # cperl-indent-level: 4
452 # perl-indent-level: 4
453 # indent-tabs-mode: nil
456 # vi: set tabstop=4 expandtab: