问题
I am trying to get properties (just width & heigth so far, but probably more later) of an album art picture from an mp3 file using python 3.7.1 and mutagen 1.42, but nothing seems to work so far. I am yet able to extract some other information correctly
The doc is telling about APIC, but trying to display all tags doesn't show anything related to any picture (and my mp3 test files does have album pictures) :
import os,sys
from mutagen.mp3 import MP3
from mutagen.easyid3 import EasyID3
song_path = os.path.join(sys.argv[1]) # With sys.argv[1] the path to a mp3 file containing a picture
track = MP3(song_path, ID3=EasyID3)
pprint(track.get('title')[0] + ' ' + str(track.info.length) + 's, ' + str(int(track.info.bitrate / 1000)) + 'kbps')
print(track.keys())
The result, using a file of mine :
> Exponential Tears 208.0s, 205kbps
> ['album', 'copyright', 'encodedby', 'length', 'title', 'artist', 'albumartist', 'tracknumber', 'genre', 'date', 'originaldate']
(This mp3 file does have an embedded picture, that I can see with any music software I use.)
I have found a lot of different ways of handling this with mutagen, but some seems outdated, others just doesn't work, I don't understand what I am missing here.
Any help here would be gladly appreciated
回答1:
OK, i eventually figured it out : the EasyID3 module only handles most common tags, and it does not includes picture data (APIC). For that, you need to use the ID3 module, which is way more complex to understand. Then, look for the APIC: key, which stores the picture as a byte string.
Here is a little exemple, using PIL to deal with pictures :
import os,sys
from io import BytesIO
from mutagen.mp3 import MP3
from mutagen.id3 import ID3
from PIL import Image
song_path = os.path.join(sys.argv[1])
track = MP3(song_path)
tags = ID3(song_path)
print("ID3 tags included in this song ------------------")
print(tags.pprint())
print("-------------------------------------------------")
pict = tags.get("APIC:").data
im = Image.open(BytesIO(pict))
print('Picture size : ' + str(im.size))
Hope it helps, good luck ! ;)
来源:https://stackoverflow.com/questions/54757751/mutagen-how-to-extract-album-art-properties