]> diplodocus.org Git - flac-archive/blobdiff - flac2mp3
(flac2mp3): Force track number argument to int.
[flac-archive] / flac2mp3
index f497d6bbc6b94f1d8b3fd7859343d254746e55b3..65e3f8f91d60163cce302e5f7d5cbc57048ba051 100755 (executable)
--- a/flac2mp3
+++ b/flac2mp3
@@ -8,14 +8,14 @@ B<flac2mp3> - transcode FLAC file to MP3 files
 
 =head1 SYNOPSIS
 
-B<flac2mp3> [B<--lame-options> I<lame-options>] [B<-q>] [B<-v>] I<file>
+B<flac2mp3> [B<--lame-options> I<lame-options>] [B<-q>] [B<-v>] I<file> [...]
 
 =head1 DESCRIPTION
 
-B<flac2mp3> transcodes the FLAC file I<file> to MP3 files.  I<file> is
-the kind of FLAC file B<fa-flacd> generates.  That is, it contains a
-cue sheet, one TITLE tag per track listed therein, and ARTIST, ALBUM,
-and DATE tags.
+B<flac2mp3> transcodes the FLAC files I<file> to MP3 files.  I<file>
+may be the kind of FLAC file B<fa-flacd> generates.  That is, it
+contains a cue sheet, one TITLE tag per track listed therein, and
+ARTIST, ALBUM, and DATE tags.
 
 =cut
 
@@ -99,6 +99,7 @@ sub get_tags {
     my $artist;
     my $album;
     my $date;
+    my $track;
 
     open(TAGS, '-|', 'metaflac', '--export-vc-to=-', $fn)
       or die("open(metaflac --export-vc-to=- $fn): $!");
@@ -107,24 +108,37 @@ sub get_tags {
 
         ($tag, $value) = split(/=/, $_, 2);
 
-        if (/^ARTIST=/) {
+        if (/^ARTIST=/i) {
             $artist = $value;
-        } elsif (/^ALBUM=/) {
+        } elsif (/^ALBUM=/i) {
             $album = $value;
-        } elsif (/^DATE=/) {
+        } elsif (/^DATE=/i) {
             $date = $value;
-        } elsif (/^ARTIST\[/) {
+        } elsif (/^ARTIST\[/i) {
             push(@$artists, $value);
+        } elsif (/^TRACKNUMBER=/i) {
+            $track = $value;
 
         # Intentionally don't match the = on this one, to support the
         # TITLE[1] .. TITLE[n] tag style.
-        } elsif (/^TITLE/) {
+        } elsif (/^TITLE/i) {
             push(@$titles, $value);
         }
     }
     close(TAGS) or die("close(metaflac --export-vc-to=- $fn): $?");
 
-    return ($artist, $album, $date);
+    return ($artist, $album, $date, $track);
+}
+
+sub arg {
+    my $arg = shift;
+    my $var = shift;
+
+    if (defined($$var)) {
+        $$var = "$arg '$$var'";
+    } else {
+        $$var = ''
+    }
 }
 
 sub flac2mp3 {
@@ -133,7 +147,7 @@ sub flac2mp3 {
     my $artist = shift;
     my $album = shift;
     my $date = shift;
-    my $track = shift;
+    my $track = int(shift);
     my $skip_arg = shift;
     my $until_arg = shift;
     my @tmp;
@@ -158,16 +172,22 @@ sub flac2mp3 {
     # any single quotes in the filename by closing the quote ('),
     # putting an escaped quote (\'), and then reopening the quote (').
     for ($fn, $title, $artist, $album, $date) {
-        s/'/'\\''/g;
+        defined and s/'/'\\''/g;
     }
 
     $outfile = sprintf("$artist ($album) \%02s $title.mp3", $track);
     $outfile =~ s/\//_/g;
 
+    arg('--tt', \$title);
+    arg('--ta', \$artist);
+    arg('--tl', \$album);
+    arg('--ty', \$date);
+    arg('--tn', \$track);
+
     $until_arg ||= '';
     run_or_die(join(' ', "flac $flac_options -cd $skip_arg $until_arg '$fn'",
-                    " | lame $lame_options --tt '$title' --ta '$artist'",
-                    " --tl '$album' --ty '$date' --tn $track - '$outfile'"));
+                    " | lame $lame_options $title $artist $album $date $track",
+                    " - '$outfile'"));
 }
 
 MAIN: {
@@ -180,14 +200,24 @@ MAIN: {
               ) or pod2usage();
     $help and pod2usage(-exitstatus=>0, -verbose=>1);
 
-    my $fn = shift or pod2usage();
-    my @args = get_decode_args($fn);
-    my (@artists, @titles);
-    my ($artist, $album, $date) = get_tags($fn, \@artists, \@titles);
-
-    for my $i (0..$#titles) {
-        flac2mp3($fn, $titles[$i], ($artists[$i] or $artist), $album, $date,
-                 $i + 1, @{$args[$i]});
+    @ARGV or pod2usage();
+    for my $fn (@ARGV) {
+        my @args = get_decode_args($fn);
+        my (@artists, @titles);
+        my ($artist, $album, $date, $track) = get_tags($fn, \@artists,
+                                                       \@titles);
+
+        # Stupid hack: only a single-track file should have the
+        # TRACKNUMBER tag, so use it if set for the first pass through
+        # the loop.  At the end of the loop, we'll set $track for the
+        # next run, so this continues to work for multi-track files.
+        $track ||= 1;
+
+        for my $i (0..$#titles) {
+            flac2mp3($fn, $titles[$i], ($artists[$i] or $artist), $album, $date,
+                     $track, (defined($args[$i]) and @{$args[$i]} or ''));
+            $track = $i + 2;
+        }
     }
 }