]> diplodocus.org Git - flac-archive/commitdiff
Support single-track FLAC files (no cue sheet).
authorepg <>
Wed, 27 Apr 2005 04:55:52 +0000 (04:55 +0000)
committerepg <>
Wed, 27 Apr 2005 04:55:52 +0000 (04:55 +0000)
If the TRACKNUMBER tag is present, use it on the first MP3 file; this
should only be set on a single-track FLAC file.

Don't blow up if any of the tags we look for are not defined; just
don't use them.

flac2mp3

index 6a496bf..69869a4 100755 (executable)
--- a/flac2mp3
+++ b/flac2mp3
@@ -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): $!");
@@ -115,6 +116,8 @@ sub get_tags {
             $date = $value;
         } 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.
@@ -124,7 +127,18 @@ sub get_tags {
     }
     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 {
@@ -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: {
@@ -183,11 +203,18 @@ MAIN: {
     my $fn = shift or pod2usage();
     my @args = get_decode_args($fn);
     my (@artists, @titles);
-    my ($artist, $album, $date) = get_tags($fn, \@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,
-                 $i + 1, @{$args[$i]});
+                 $track, (defined($args[$i]) and @{$args[$i]} or ''));
+        $track = $i + 2;
     }
 }