]>
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>]
20 use Getopt
::Long
qw(:config gnu_getopt no_ignore_case);
21 use POSIX
':sys_wait_h';
29 $verbose and print(STDERR
$_) for @_;
32 # Return the ARTIST, ALBUM, and DATE followed by a list of all the
33 # lines in the file FN.
42 verbose
("Opening tags file $fn\n");
43 open(TAGS
, $fn) or die("open($fn): $!");
48 ($tag, $value) = split(/=/, $_, 2);
52 verbose
("ARTIST $artist from $fn\n");
55 verbose
("ALBUM $album from $fn\n");
58 close(TAGS
) or die("close($fn): $!");
60 return ($artist, $album, @tags);
63 # Process the fa-rip output in the directory DIR.
72 verbose
("Renaming $dir/tags\n");
73 rename("$dir/tags", "$dir/using-tags")
74 or die("rename($dir/tags, $dir/using-tags): $!");
76 ($artist, $album, @tags) = get_tags
("$dir/using-tags");
78 verbose
("mkdir($artist)\n");
79 -d
$artist or mkdir($artist) or die("mkdir($artist): $!");
81 verbose
("chdir($dir)\n");
82 chdir($dir) or die("chdir($dir): $!");
87 verbose
("Running flac\n");
88 $status = system('flac', '-o', "../$artist/$outfile.flac-tmp",
89 '--delete-input-file', '-V', '--cuesheet',
90 'cue', '--no-padding', '--best',
91 map({ ('-T', $_) } @tags),
93 if (WIFEXITED
($status)) {
94 if (($status = WEXITSTATUS
($status)) != 0) {
95 die("flac exited with status $status");
97 } elsif (WIFSIGNALED
($status)) {
98 die("flac killed with signal ", WTERMSIG
($status));
99 } elsif (WIFSTOPPED
($status)) {
100 die("flac stopped with signal ", WSTOPSIG
($status));
102 die("Major horkage on system(flac): \$? = $? \$! = $!");
105 verbose
("Cleaning up $dir\n");
106 unlink('using-tags') or die("unlink(using-tags): $!");
107 unlink('cue') or die("unlink(cue): $!");
108 rename('log', "../$artist/$outfile.log")
109 or die("rename(log, ../$artist/$outfile.log): $!");
110 chdir('..') or die("chdir(..): $!");
112 rename("$artist/$outfile.flac-tmp", "$artist/$outfile.flac")
113 or die("rename($artist/$outfile.flac-tmp, $artist/$outfile.flac): $!");
115 if (-x
"$dir/post-processor") {
116 verbose
("Running './$dir/post-processor $artist/$outfile.flac'\n");
117 system("./$dir/post-processor", "$artist/$outfile.flac");
118 unlink("$dir/post-processor") or die("unlink($dir/post-processor): $!");
121 rmdir($dir) or die("rmdir($dir): $!");
129 while (($pid = waitpid(-1, WNOHANG
)) > 0) {
130 push(@finished, [$pid, $?]);
133 $SIG{CHLD
} = \
&reaper
;
141 if (not defined($pid)) {
143 } elsif ($pid == 0) {
144 $SIG{CHLD
} = 'IGNORE';
145 open(STDERR
, ">$dir/log") or die("open(STDERR, >$dir/log): $!");
149 verbose
("new job $pid for $dir\n");
159 $pid = $finished[$i][0];
160 $status = $finished[$i][1];
162 verbose
("$pid finished (");
163 if (WIFEXITED
($status)) {
164 verbose
('exited with status ', WEXITSTATUS
($status));
165 } elsif (WIFSIGNALED
($status)) {
166 verbose
('killed with signal ', WTERMSIG
($status));
167 } elsif (WIFSTOPPED
($status)) {
168 verbose
('stopped with signal ', WSTOPSIG
($status));
172 for ($j = 0; $j <= $#jobs; $j++) {
173 $pid == $jobs[$j] and splice(@jobs, $j, 1) and last;
176 splice(@finished, $i, 1);
185 $SIG{CHLD
} = \
&reaper
;
187 if (scalar(@jobs) <= $MAXJOBS) {
188 foreach $i (glob('*/tags')) {
189 push(@jobs, newjob
(dirname
($i))) <= $MAXJOBS or last;
193 for ($i = 0; $i <= $#finished; $i++) {
197 verbose
(scalar(@jobs), " jobs\n");
208 'jobs|j=i' => \
$jobs,
209 'verbose|v' => \
$verbose,
210 'help|h|?' => \
$help,
212 $help and pod2usage
(-exitstatus
=>0, -verbose
=>1);
222 B<fa-flacd> and B<fa-rip> together comprise B<flac-archive>, a system
223 for archiving audio CDs to single FLAC files. B<fa-flacd> is the guts
224 of the system. It runs in the directory where the audio archives are
225 stored, scanning for new ripped CDs to encode and rename; it never
226 exits. B<fa-rip> generates the inputs for B<fa-flacd>: the ripped WAV
227 file, Vorbis tags, and a cuesheet.
229 Both programs expect to be run from the same directory. They use that
230 directory to manage directories named by artist. Intermediate files
231 are written to temporary directories here. B<fa-flacd> processes the
232 temporary directories into per-album files in the artist directories.
234 Every 5 seconds, B<fa-flacd> scans its current directory for
235 directories with a file called "tags" and creates a processing job for
236 each one. The number of jobs B<fa-flacd> attempts to run is
237 controlled by the B<-j> option and defaults to 4. B<fa-flacd> will
238 print diagnostic output when the B<-v> option is given.
240 A processing job first renames the directory's "tags" file to
241 "using-tags" so that B<ra-flacd> will not try to start another job for
242 this directory. This file is left as is when an error is encountered,
243 so a new job will not be started until the user corrects the error
244 condition and renames "using-tags" back to "tags". Next, it encodes
245 the "wav" file to a FLAC file, using the "cue" file for the cuesheet
246 and "using-tags" for Vorbis tags. Any diagnostic output is saved in
247 the "log" file. Finally, B<fa-flacd> moves the "cue" and "log" files
248 to the artist directory (named by album) and removes the temporary
251 If the temporary directory contains an executable file named
252 "post-processor", B<fa-flacd> executes that file with the relative
253 path to the output FLAC file as an argument. The output files are in
254 their final location when "post-processor" starts. Possible uses are
255 running B<flac2mp3>, moving the output files to a different location,
256 removing the lock file, or adding to a database. The standard input,
257 output, and error streams are inherited from B<fa-flacd>, so they may
258 be connected to anything from a tty to /dev/null. This means that you
259 may want to redirect these streams, if you want to save them or do any
266 =item B<-j> [B<--jobs>] I<jobs>
268 Run up to I<jobs> jobs instead of the default 4.
270 =item B<-v> [B<--verbose>]
272 Print diagnostic information.
278 Written by Eric Gillespie <epg@pretzelnet.org>.
280 flac-archive is free software; you may redistribute it and/or modify
281 it under the same terms as Perl itself.
286 # cperl-indent-level: 4
287 # perl-indent-level: 4
288 # indent-tabs-mode: nil
291 # vi: set tabstop=4 expandtab: