Fix Unicode tag encoding in mkogg's mp3 output.
authorW. Trevor King <wking@tremily.us>
Wed, 22 Aug 2012 00:37:36 +0000 (20:37 -0400)
committerW. Trevor King <wking@tremily.us>
Wed, 22 Aug 2012 00:38:17 +0000 (20:38 -0400)
posts/mkogg/mkogg.py

index cab3e056a0b9b54797ccb3af63d430d65c0ff82d..a9bfdc6554539c5600383352bc3e9bbcc7397de6 100755 (executable)
@@ -310,7 +310,7 @@ class Converter (object):
                     try:
                         text.encode(encoding)
                     except UnicodeEncodeError:
-                        encoding_success == False
+                        encoding_success = False
                         break
             if encoding_success:
                 return id3_encoding
@@ -413,6 +413,7 @@ class Converter (object):
         if mp3.tags is not None:
             mp3.tags.delete()
         handled_trck = False
+        max_encoding = 0
         for key,value in metadata.items():
             if key == 'date':
                 for i,v in enumerate(value):
@@ -431,10 +432,15 @@ class Converter (object):
                 continue
             frame = getattr(mutagen.id3, frame_name)
             id3_encoding = self._guess_id3_encoding(value)
+            max_encoding = max(max_encoding, id3_encoding)
             mp3[frame_name] = frame(encoding=id3_encoding, text=value)
         if mp3.tags is None:
             return
-        mp3.save(v1=2)
+        if max_encoding:  # at least one tag doesn't use ISO-8859-1
+            v1 = 0  # remove ID3v1 tags
+        else:
+            v1 = 2  # create and/or update ID3v1 tags
+        mp3.save(v1=v1)
 
     def set_ogg_metadata(self, target, metadata):
         if mutagen is None: