سؤال

I am trying to assign a picture to a song, and I have some code that works on mac, but not on PC.

from mutagen.easyid3 import EasyID3

from mutagen.id3 import ID3, APIC, error

from mutagen.mp3 import MP3

def image_assigner(self):

   song = MP3(self.file, ID3=ID3)

   # add ID3 tag if it doesn't exist
   try:
       song.add_tags()
   except error:
       print "we got an image error"
       pass

   song.tags.add(
       APIC(
           encoding=3, 
           mime='image/jpeg', 
           type=2, 
           desc=u'Cover',
           data=open('example.JPG', 'rb').read()
       )
   )
   song.save()

So on Mac, this code works, but when I try it on my PC, it won't. Any help would be appreciated. Thanks!

Edit So, after doing some more research, I figured out that this code does save the album artwork to the mp3 file on Mac as well as Windows, but it saves it in ID3v2.4, which Mac can read, but Windows can't read, so it appeared like it didn't save it on Windows. It appears that using the v1=2 option in the mutagen save function should save the tags in ID3v1 (see the Oct. 4th post on this page). It seems to work if I update the tags for album, artist, title, etc., but when I do it for album artwork, it still doesn't show up in Windows explorer. Does anybody have experience in this area and could shed some light on this? Thanks.

هل كانت مفيدة؟

المحلول

Yeah unfortunately Windows doesn't support that version. Instead of just saving it in ID3v1 try saving it in ID3v3 and ID3v1. I use this in my programs and it works great in Windows 8 and OSX.

from mutagen.mp3 import MP3
from mutagen.id3 import ID3, APIC, error, TRCK, TIT2, TPE1, TALB, TDRC, TCON

audio = MP3([PATH_TO_FILE], ID3=ID3)
audio.tags.delete([PATH_TO_FILE], delete_v1=True, delete_v2=True)
audio.tags.add(
    APIC(
        encoding=3,
        mime='image/jpeg',
        type=3,
        desc=u'Cover',
        data=open([PATH_TO_COVER_IMAGE], 'rb').read()
    )
)
audio.save([PATH_TO_FILE], v2_version=3, v1=2)
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top