]>
diplodocus.org Git - flac-archive/blob - flac2mp3
7 B<flac2mp3> - transcode FLAC file to MP3 files
11 B<flac2mp3> [B<--lame-options> I<lame-options>] [B<-j> I<jobs>] [B<-q>] [B<-v>] I<file> [...]
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.
28 use POSIX
':sys_wait_h';
38 if (not defined($pid)) {
43 if ($debug or $pid == 0) {
61 for (my $i = 0; $i <= $#$jobs; $i++) {
62 if ($pid == $jobs->[$i]) {
63 splice(@$jobs, $i, 1);
68 return ($pid, $status);
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'};
80 # Call notifier function if given.
82 my $f = shift or return;
83 ref($f) eq 'CODE' or return;
88 if (@jobs < $maxjobs) {
90 while (defined($job = $get_job->())) {
91 $pid = newjob
($job, \
@jobs, $o{'debug'});
92 call
($notify_start, $pid, @jobs);
93 @jobs < $maxjobs or last;
96 # No jobs running and get-job returned undef; we're finished.
97 if (@jobs == 0 and not defined($job)) {
102 # Now running as many jobs as we can, block waiting for one to die.
104 $pid = waitpid(-1, 0);
106 or ($pid == -1 and ($!{ECHILD
} or $!{EINTR
})));
107 $pid == -1 and die("waitpid(-1): $!");
109 # Before starting more, see if any others have finished.
111 call
($notify_finish, deljob
($pid, $?, \
@jobs), @jobs);
112 } while (($pid = waitpid(-1, WNOHANG
)) > 0);
114 $!{ECHILD
} or $!{EINTR
} or die("waitpid(-1): $!");
120 ################################################################################
126 use POSIX
':sys_wait_h';
128 use Getopt
::Long
qw(:config gnu_getopt no_ignore_case);
139 $verbose and print(STDERR
"$command\n");
140 $status = system($command);
142 if (WIFEXITED
($status)) {
143 if (($status = WEXITSTATUS
($status)) != 0) {
144 die("$command exited with status $status");
146 } elsif (WIFSIGNALED
($status)) {
147 die("$command killed with signal ", WTERMSIG
($status));
148 } elsif (WIFSTOPPED
($status)) {
149 die("$command stopped with signal ", WSTOPSIG
($status));
151 die("Major horkage on system($command): \$? = $? \$! = $!");
156 return sprintf('%02d:%02d.%02d', @_);
159 sub get_decode_args
{
163 open(F
, '-|', 'metaflac', '--export-cuesheet-to=-', $fn);
165 /INDEX 01 (\d\d):(\d\d):(\d\d)$/ or next;
166 push(@l, [$1, $2, $3]);
171 my $arg = ["--skip=" . tformat
(@{$l[$i]})];
173 if (defined($next)) {
174 if ($next->[2] == 0) {
175 if ($next->[1] == 0) {
176 push(@$arg, '--until=' . tformat
($next->[0] - 1, 59, 74));
178 push(@$arg, '--until=' . tformat
($next->[0], $next->[1] - 1,
182 push(@$arg, '--until=' . tformat
($next->[0], $next->[1],
189 # If no cue sheet, stick a dummy in here.
197 # Return the ARTIST, ALBUM, and DATE tags followed by the TITLE tags
211 open(TAGS
, '-|', 'metaflac', '--export-vc-to=-', $fn)
212 or die("open(metaflac --export-vc-to=- $fn): $!");
216 ($tag, $value) = split(/=/, $_, 2);
220 } elsif (/^ALBUM=/i) {
222 } elsif (/^DATE=/i) {
224 } elsif (/^DISCNUMBER=/i) {
225 $discnum = int($value);
226 } elsif (/^ARTIST\[/i) {
227 push(@$artists, $value);
228 } elsif (/^TRACKNUMBER=/i) {
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);
237 close(TAGS
) or die("close(metaflac --export-vc-to=- $fn): $?");
239 # If no TITLEs, stick a dummy in here.
241 push(@$titles, undef);
244 return ($artist, $album, $date, $discnum, $track);
251 if (defined($$var)) {
252 $$var = "$arg '$$var'";
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;
271 $flac_options = '--silent';
277 push(@tmp, $lame_options);
279 push(@tmp, '--preset standard');
281 $quiet and push(@tmp, '--quiet');
282 $verbose and push(@tmp, '--verbose');
283 $lame_options = join(' ', @tmp);
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;
292 $outfile = sprintf("$artist ($album) \%02s $title.mp3", $track);
293 $outfile =~ s/\//_
/g
;
295 arg
('--tt', \
$title);
296 arg
('--ta', \
$artist);
297 arg
('--tl', \
$album);
299 arg
('--tn', \
$track);
303 run_or_die
(join(' ', "flac $flac_options -cd $skip_arg $until_arg '$fn'",
304 " | lame $lame_options $title $artist $album $date $track",
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,
320 $help and pod2usage
(-exitstatus
=>0, -verbose
=>1);
322 @ARGV > 0 or pod2usage
();
326 my @args = get_decode_args
($fn);
327 my (@artists, @titles);
328 my ($artist, $album, $date, $discnum, $track) = get_tags
($fn, \
@artists,
331 # lame doesn't seem to support disc number.
332 defined($discnum) and $album .= " (disc $discnum)";
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.
340 for my $i (0..$#titles) {
341 push(@jobs, [$fn, $titles[$i], ($artists[$i] or $artist), $album,
342 $date, $track, @{$args[$i]}]);
347 Jobs
::run
('max-jobs'=>$maxjobs,
350 my $job = shift(@jobs) or return;
351 return sub { flac2mp3
(@$job) }
362 =item B<--lame-options> I<lame-options>
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.
368 =item B<-j> [B<--jobs>] I<jobs>
370 Run up to I<jobs> jobs instead of the default 1.
372 =item B<-q> [B<--quiet>]
374 Suppress status information. This option is passed along to B<flac>
377 =item B<-v> [B<--verbose>]
379 Print diagnostic information. This option is passed along to B<flac>
386 Written by Eric Gillespie <epg@pretzelnet.org>.
391 # cperl-indent-level: 4
392 # perl-indent-level: 4
393 # indent-tabs-mode: nil
396 # vi: set tabstop=4 expandtab: