]> diplodocus.org Git - flac-archive/blobdiff - flac2mp3
Support my old, numberless TITLE tags.
[flac-archive] / flac2mp3
index 9b2351993f68b4108b2a1df328a5341d2aa5e9ad..999fd66d6fe92b87225f67655c1d33c2f8f3afd1 100755 (executable)
--- a/flac2mp3
+++ b/flac2mp3
@@ -55,14 +55,18 @@ from optparse import OptionParser
 from subprocess import Popen, PIPE
 
 import org.diplodocus.jobs
+from org.diplodocus import flac, taglib
 from org.diplodocus.util import run_or_die
 
 ################################################################################
 # The child processes
 
-def flac2mp3(fn, title, artist, album, date, track, skip_until):
-    (title, artist, album, date) = [(x == None and 'unknown') or x
-                                    for x in (title, artist, album, date)]
+def flac2mp3(fn, title, artist, album, date, track, skip_until, pics=None):
+    (title, artist, album) = [(x == None and 'unknown') or x
+                              for x in (title, artist, album)]
+    if date == None:
+        date = ''
+
     try:
         (skip_arg, until_arg) = skip_until
     except ValueError:
@@ -83,17 +87,24 @@ def flac2mp3(fn, title, artist, album, date, track, skip_until):
     verbose and tmp.append('--verbose')
     lame_options = ' '.join(tmp)
 
+    outfile = ('%s (%s) %02d %s.mp3' % (artist, album,
+                                        track, title)).replace('/', '_')
+
     # Escape any single quotes ' so we can quote this.
     (fn, title, artist,
      album, date) = [x.replace("'", r"'\''")
                      for x in (fn, title, artist, album, date)]
 
-    outfile = ('%s (%s) %02d %s.mp3' % (artist, album,
-                                        track, title)).replace('/', '_')
+    quoted_outfile = ('%s (%s) %02d %s.mp3' % (artist, album,
+                                               track, title)).replace('/', '_')
 
-    run_or_die(3, "flac %s -cd %s %s '%s' | lame %s --tt '%s' --ta '%s' --tl '%s' --ty '%s' --tn %d - '%s'"
+    run_or_die(3, "flac %s -cd %s %s '%s' | lame --add-id3v2 %s --tt '%s' --ta '%s' --tl '%s' --ty '%s' --tn %d - '%s'"
                % (flac_options, skip_arg or '', until_arg or '', fn,
-                  lame_options, title, artist, album, date, track, outfile))
+                  lame_options, title, artist, album, date, track,
+                  quoted_outfile))
+
+    if pics != None:
+        taglib.add_apic_frame_to_mp3(outfile, pics)
 
     return 0
 
@@ -164,6 +175,7 @@ class Tags(object):
             return None
         return '\n'.join(value)
     def set(self, key, value, track=None):
+        key = key.upper()
         if track not in self._tags:
             self._tags[track] = {}
         if key not in self._tags[track]:
@@ -176,7 +188,7 @@ def get_tags(fn):
 
     tags = Tags()
 
-    p = Popen(['metaflac', '--export-vc-to=-', fn], stdout=PIPE)
+    p = Popen(['metaflac', '--export-tags-to=-', fn], stdout=PIPE)
     for line in (x.rstrip() for x in p.stdout):
         (tag, value) = line.split('=', 1)
 
@@ -193,6 +205,15 @@ def get_tags(fn):
 
     return tags
 
+def find_pics(fn, tags):
+    pics = tags.get('__flac2mp3_PICTURE')
+
+    if not isinstance(pics, list):
+        pics = flac.get_pictures(fn)
+        tags.set('__flac2mp3_PICTURE', pics)
+
+    return pics
+
 def main(argv):
     # Control the exit code for any uncaught exceptions.
     try:
@@ -255,7 +276,7 @@ def main(argv):
                                  tags.gets('ARTIST', track),
                                  album,
                                  tags.gets('DATE', track),
-                                 track, args[i]])
+                                 track, args[i], find_pics(fn, tags)])
                     track = i + 2
             except Exception, error:
                 sys.stderr.write(getattr(error, 'msg', ''))