]>
diplodocus.org Git - flac-archive/blob - fa-flacd
7 B<fa-flacd> - archive CDs to single FLAC files
19 use Getopt
::Std
; $Getopt::Std
::STANDARD_HELP_VERSION
= 1;
20 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) and ($status = WEXITSTATUS
($status)) != 0) {
95 } elsif (WIFSIGNALED
($status)) {
96 die("flac killed with signal ", WTERMSIG
($status));
97 } elsif (WIFSTOPPED
($status)) {
98 die("flac stopped with signal ", WSTOPSIG
($status));
101 verbose
("Cleaning up $dir\n");
102 unlink('using-tags') or die("unlink(using-tags): $!");
103 unlink('cue') or die("unlink(cue): $!");
104 rename('log', "../$artist/$outfile.log")
105 or die("rename(log, ../$artist/$outfile.log): $!");
106 chdir('..') or die("chdir(..): $!");
107 rmdir($dir) or die("rmdir($dir): $!");
109 rename("$artist/$outfile.flac-tmp", "$artist/$outfile.flac")
110 or die("rename($artist/$outfile.flac-tmp, $artist/$outfile.flac): $!");
118 while (($pid = waitpid(-1, WNOHANG
)) > 0) {
119 push(@finished, [$pid, $?]);
122 $SIG{CHLD
} = \
&reaper
;
130 if (not defined($pid)) {
132 } elsif ($pid == 0) {
133 $SIG{CHLD
} = 'IGNORE';
134 open(STDERR
, ">$dir/log") or die("open(STDERR, >$dir/log): $!");
138 verbose
("new job $pid for $dir\n");
148 $pid = $finished[$i][0];
149 $status = $finished[$i][1];
151 verbose
("$pid finished (");
152 if (WIFEXITED
($status)) {
153 verbose
('exited ', WEXITSTATUS
($status));
154 } elsif (WIFSIGNALED
($status)) {
155 verbose
('signalled ', WTERMSIG
($status));
156 } elsif (WIFSTOPPED
($status)) {
157 verbose
('stopped ', WSTOPSIG
($status));
161 for ($j = 0; $j <= $#jobs; $j++) {
162 $pid == $jobs[$j] and splice(@jobs, $j, 1) and last;
165 splice(@finished, $i, 1);
174 $SIG{CHLD
} = \
&reaper
;
176 if (scalar(@jobs) <= $MAXJOBS) {
177 foreach $i (glob('*/tags')) {
178 push(@jobs, newjob
(dirname
($i))) <= $MAXJOBS or last;
182 for ($i = 0; $i <= $#finished; $i++) {
186 verbose
(scalar(@jobs), " jobs\n");
196 if (not getopts
('j:v', \
%opts)) {
197 print(STDERR
"usage: flacd [-jN -v]\n");
201 $verbose = $opts{'v'};
203 flacloop
($opts{'j'});
211 B<fa-flacd> and B<fa-rip> together comprise B<flac-archive>, a system
212 for archiving audio CDs to single FLAC files. B<fa-flacd> is the guts
213 of the system. It runs in the directory where the audio archives are
214 stored, scanning for new ripped CDs to encode and rename; it never
215 exits. B<fa-rip> generates the inputs for B<fa-flacd>: the ripped WAV
216 file, Vorbis tags, and a cuesheet.
218 Both programs expect to be run from the same directory. They use that
219 directory to manage directories named by artist. Intermediate files
220 are written to temporary directories here. B<fa-flacd> processes the
221 temporary directories into per-album files in the artist directories.
223 Every 5 seconds, B<fa-flacd> scans its current directory for
224 directories with a file called "tags" and creates a processing job for
225 each one. The number of jobs B<fa-flacd> attempts to run is
226 controlled by the B<-j> option and defaults to 4. B<fa-flacd> will
227 print diagnostic output when the B<-v> option is given.
229 A processing job first renames the directory's "tags" file to
230 "using-tags" so that B<ra-flacd> will not try to start another job for
231 this directory. This file is left as is when an error is encountered,
232 so a new job will not be started until the user corrects the error
233 condition and renames "using-tags" back to "tags". Next, it encodes
234 the "wav" file to a FLAC file, using the "cue" file for the cuesheet
235 and "using-tags" for Vorbis tags. Any diagnostic output is saved in
236 the "log" file. Finally, B<fa-flacd> moves the "cue" and "log" files
237 to the artist directory (named by album) and removes the temporary
242 Written by Eric Gillespie <epg@pretzelnet.org>.
244 flac-archive is free software; you may redistribute it and/or modify
245 it under the same terms as Perl itself.
250 # cperl-indent-level: 4
251 # perl-indent-level: 4
252 # indent-tabs-mode: nil
255 # vi: set tabstop=4 expandtab: