]> diplodocus.org Git - flac-archive/blob - flac2mp3
Add Python implemention of Jobs package.
[flac-archive] / flac2mp3
1 #! /usr/bin/env perl
2
3 # $Id$
4
5 =head1 NAME
6
7 B<flac2mp3> - transcode FLAC file to MP3 files
8
9 =head1 SYNOPSIS
10
11 B<flac2mp3> [B<--lame-options> I<lame-options>] [B<-j> I<jobs>] [B<-q>] [B<-v>] I<file> [...]
12
13 =head1 DESCRIPTION
14
15 B<flac2mp3> transcodes the FLAC files I<file> to MP3 files. I<file>
16 may be the kind of FLAC file B<fa-flacd> generates. That is, it
17 contains a cue sheet, one TITLE tag per track listed therein, and
18 ARTIST, ALBUM, and DATE tags.
19
20 =cut
21
22 package Jobs;
23
24 use strict;
25 use warnings;
26
27 use Errno;
28 use POSIX ':sys_wait_h';
29
30 sub newjob {
31 my $f = shift;
32 my $jobs = shift;
33 my $debug = shift;
34 my $pid;
35
36 if (not $debug) {
37 $pid = fork();
38 if (not defined($pid)) {
39 die("fork: $!");
40 }
41 }
42
43 if ($debug or $pid == 0) {
44 exit($f->());
45 }
46
47 if ($pid == 0) {
48 exit($f->());
49 }
50
51 push(@$jobs, $pid);
52
53 return $pid;
54 }
55
56 sub deljob {
57 my $pid = shift;
58 my $status = shift;
59 my $jobs = shift;
60
61 for (my $i = 0; $i <= $#$jobs; $i++) {
62 if ($pid == $jobs->[$i]) {
63 splice(@$jobs, $i, 1);
64 last;
65 }
66 }
67
68 return ($pid, $status);
69 }
70
71 sub run {
72 my %o = @_;
73 my $maxjobs = $o{'max-jobs'};
74 my $get_job = $o{'get-job'};
75 my $notify_start = $o{'notify-start'};
76 my $notify_finish = $o{'notify-finish'};
77 my @jobs;
78 my $pid;
79
80 # Call notifier function if given.
81 sub call {
82 my $f = shift or return;
83 ref($f) eq 'CODE' or return;
84 $f->(@_);
85 }
86
87 while (1) {
88 if (@jobs < $maxjobs) {
89 my $job;
90 while (defined($job = $get_job->())) {
91 $pid = newjob($job, \@jobs, $o{'debug'});
92 call($notify_start, $pid, @jobs);
93 @jobs < $maxjobs or last;
94 }
95
96 # No jobs running and get-job returned undef; we're finished.
97 if (@jobs == 0 and not defined($job)) {
98 return;
99 }
100 }
101
102 # Now running as many jobs as we can, block waiting for one to die.
103 do {
104 $pid = waitpid(-1, 0);
105 } while ($pid == 0
106 or ($pid == -1 and ($!{ECHILD} or $!{EINTR})));
107 $pid == -1 and die("waitpid(-1): $!");
108
109 # Before starting more, see if any others have finished.
110 do {
111 call($notify_finish, deljob($pid, $?, \@jobs), @jobs);
112 } while (($pid = waitpid(-1, WNOHANG)) > 0);
113 if ($pid == -1) {
114 $!{ECHILD} or $!{EINTR} or die("waitpid(-1): $!");
115 }
116 }
117 }
118
119 \f
120 ################################################################################
121 package main;
122
123 use strict;
124 use warnings;
125
126 use POSIX ':sys_wait_h';
127 use Pod::Usage;
128 use Getopt::Long qw(:config gnu_getopt no_ignore_case);
129
130 my $flac_options;
131 my $lame_options;
132 my $quiet;
133 my $verbose;
134
135 sub run_or_die {
136 my $command = shift;
137 my $status;
138
139 $verbose and print(STDERR "$command\n");
140 $status = system($command);
141
142 if (WIFEXITED($status)) {
143 if (($status = WEXITSTATUS($status)) != 0) {
144 die("$command exited with status $status");
145 }
146 } elsif (WIFSIGNALED($status)) {
147 die("$command killed with signal ", WTERMSIG($status));
148 } elsif (WIFSTOPPED($status)) {
149 die("$command stopped with signal ", WSTOPSIG($status));
150 } else {
151 die("Major horkage on system($command): \$? = $? \$! = $!");
152 }
153 }
154
155 sub tformat {
156 return sprintf('%02d:%02d.%02d', @_);
157 }
158
159 sub get_decode_args {
160 my $fn = shift;
161 my @l;
162
163 open(F, '-|', 'metaflac', '--export-cuesheet-to=-', $fn);
164 while (<F>) {
165 /INDEX 01 (\d\d):(\d\d):(\d\d)$/ or next;
166 push(@l, [$1, $2, $3]);
167 }
168
169 my @args;
170 for my $i (0..$#l) {
171 my $arg = ["--skip=" . tformat(@{$l[$i]})];
172 my $next = $l[$i+1];
173 if (defined($next)) {
174 if ($next->[2] == 0) {
175 if ($next->[1] == 0) {
176 push(@$arg, '--until=' . tformat($next->[0] - 1, 59, 74));
177 } else {
178 push(@$arg, '--until=' . tformat($next->[0], $next->[1] - 1,
179 74));
180 }
181 } else {
182 push(@$arg, '--until=' . tformat($next->[0], $next->[1],
183 $next->[2] - 1));
184 }
185 }
186 push(@args, $arg);
187 }
188
189 # If no cue sheet, stick a dummy in here.
190 if (@args == 0) {
191 @args = ([]);
192 }
193
194 return @args;
195 }
196
197 # Return the ARTIST, ALBUM, and DATE tags followed by the TITLE tags
198 # in the file FN.
199 sub get_tags {
200 my $fn = shift;
201 my $artists = shift;
202 my $titles = shift;
203 my $tag;
204 my $value;
205 my $artist;
206 my $album;
207 my $date;
208 my $discnum;
209 my $track;
210
211 open(TAGS, '-|', 'metaflac', '--export-vc-to=-', $fn)
212 or die("open(metaflac --export-vc-to=- $fn): $!");
213 while (<TAGS>) {
214 chomp;
215
216 ($tag, $value) = split(/=/, $_, 2);
217
218 if (/^ARTIST=/i) {
219 $artist = $value;
220 } elsif (/^ALBUM=/i) {
221 $album = $value;
222 } elsif (/^DATE=/i) {
223 $date = $value;
224 } elsif (/^DISCNUMBER=/i) {
225 $discnum = int($value);
226 } elsif (/^ARTIST\[/i) {
227 push(@$artists, $value);
228 } elsif (/^TRACKNUMBER=/i) {
229 $track = $value;
230
231 # Intentionally don't match the = on this one, to support the
232 # TITLE[1] .. TITLE[n] tag style.
233 } elsif (/^TITLE/i) {
234 push(@$titles, $value);
235 }
236 }
237 close(TAGS) or die("close(metaflac --export-vc-to=- $fn): $?");
238
239 # If no TITLEs, stick a dummy in here.
240 if (@$titles == 0) {
241 push(@$titles, undef);
242 }
243
244 return ($artist, $album, $date, $discnum, $track);
245 }
246
247 sub arg {
248 my $arg = shift;
249 my $var = shift;
250
251 if (defined($$var)) {
252 $$var = "$arg '$$var'";
253 } else {
254 $$var = ''
255 }
256 }
257
258 sub flac2mp3 {
259 my $fn = shift;
260 my $title = (shift or 'unknown');
261 my $artist = (shift or 'unknown');
262 my $album = (shift or 'unknown');
263 my $date = (shift or 'unknown');
264 my $track = int(shift);
265 my $skip_arg = shift;
266 my $until_arg = shift;
267 my @tmp;
268 my $outfile;
269
270 if ($quiet) {
271 $flac_options = '--silent';
272 } else {
273 $flac_options = '';
274 }
275
276 if ($lame_options) {
277 push(@tmp, $lame_options);
278 } else {
279 push(@tmp, '--preset standard');
280 }
281 $quiet and push(@tmp, '--quiet');
282 $verbose and push(@tmp, '--verbose');
283 $lame_options = join(' ', @tmp);
284
285 # We'll be putting these in single quotes, so we need to escape
286 # any single quotes in the filename by closing the quote ('),
287 # putting an escaped quote (\'), and then reopening the quote (').
288 for ($fn, $title, $artist, $album, $date) {
289 defined and s/'/'\\''/g;
290 }
291
292 $outfile = sprintf("$artist ($album) \%02s $title.mp3", $track);
293 $outfile =~ s/\//_/g;
294
295 arg('--tt', \$title);
296 arg('--ta', \$artist);
297 arg('--tl', \$album);
298 arg('--ty', \$date);
299 arg('--tn', \$track);
300
301 $skip_arg ||= '';
302 $until_arg ||= '';
303 run_or_die(join(' ', "flac $flac_options -cd $skip_arg $until_arg '$fn'",
304 " | lame $lame_options $title $artist $album $date $track",
305 " - '$outfile'"));
306 }
307
308 MAIN: {
309 my $help;
310 my $debug;
311 my $maxjobs = 1;
312 GetOptions(
313 'debug|X' => \$debug,
314 'jobs|j=i' => \$maxjobs,
315 'lame-options=s', \$lame_options,
316 'quiet|q' => \$quiet,
317 'verbose|v' => \$verbose,
318 'help|h|?' => \$help,
319 ) or pod2usage();
320 $help and pod2usage(-exitstatus=>0, -verbose=>1);
321
322 @ARGV > 0 or pod2usage();
323
324 my @jobs;
325 for my $fn (@ARGV) {
326 my @args = get_decode_args($fn);
327 my (@artists, @titles);
328 my ($artist, $album, $date, $discnum, $track) = get_tags($fn, \@artists,
329 \@titles);
330
331 # lame doesn't seem to support disc number.
332 defined($discnum) and $album .= " (disc $discnum)";
333
334 # Stupid hack: only a single-track file should have the
335 # TRACKNUMBER tag, so use it if set for the first pass through
336 # the loop. At the end of the loop, we'll set $track for the
337 # next run, so this continues to work for multi-track files.
338 $track ||= 1;
339
340 for my $i (0..$#titles) {
341 push(@jobs, [$fn, $titles[$i], ($artists[$i] or $artist), $album,
342 $date, $track, @{$args[$i]}]);
343 $track = $i + 2;
344 }
345 }
346
347 Jobs::run('max-jobs'=>$maxjobs,
348 'debug'=>$debug,
349 'get-job'=>sub {
350 my $job = shift(@jobs) or return;
351 return sub { flac2mp3(@$job) }
352 });
353 }
354
355 \f
356 __END__
357
358 =head1 OPTIONS
359
360 =over 4
361
362 =item B<--lame-options> I<lame-options>
363
364 Pass I<lame-options> to B<lame>. This ends up being passed to the
365 shell, so feel free to take advantage of that. You'll almost
366 certainly have to put I<lame-options> in single quotes.
367
368 =item B<-j> [B<--jobs>] I<jobs>
369
370 Run up to I<jobs> jobs instead of the default 1.
371
372 =item B<-q> [B<--quiet>]
373
374 Suppress status information. This option is passed along to B<flac>
375 and B<lame>.
376
377 =item B<-v> [B<--verbose>]
378
379 Print diagnostic information. This option is passed along to B<flac>
380 and B<lame>.
381
382 =back
383
384 =head1 AUTHORS
385
386 Written by Eric Gillespie <epg@pretzelnet.org>.
387
388 =cut
389
390 # Local variables:
391 # cperl-indent-level: 4
392 # perl-indent-level: 4
393 # indent-tabs-mode: nil
394 # End:
395
396 # vi: set tabstop=4 expandtab: