5

In my searches I have found that there are a few libraries that might be able to do this by reading ID3 tags. If so - which one would be the best to use? I don't plan on writing any data just reading.

Also I'm trying to make this app as portable as possible so the least amount of dependencies would be a huge bonus.

Would appreciate some advice. Thanks.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Sheldon
  • 9,639
  • 20
  • 59
  • 96

2 Answers2

18

I'd recommend mutagen, it's a pure python library with no other dependencies and it supports a lot of different audio metadata formats/tags (MP3, FLAC, M4A, Monkey's Audio, Musepack, and more). To extract artwork from an ID3 v2.4 MP3 saved with iTunes:

from mutagen import File

file = File('some.mp3') # mutagen can automatically detect format and type of tags
artwork = file.tags['APIC:'].data # access APIC frame and grab the image
with open('image.jpg', 'wb') as img:
   img.write(artwork) # write artwork to new image
Zach Kelling
  • 52,505
  • 13
  • 109
  • 108
  • This looks really good. Thanks for the answer. Only problem is documentation - there is none! Can you point me in the right direction as to where I might find any? Thanks. – Sheldon May 30 '11 at 08:05
  • Source code (docstrings/comments) and the Quod libet mailing list. I (oddly) didn't even consider the lack of proper documentation when recommending it. I've been using it for a while and the API is simple enough that you can figure things out with introspection, messing around in the interactive interpreter. – Zach Kelling May 30 '11 at 11:05
  • I think I'm starting to get my head around it now although could you give me an example of reading the artwork(image) from an mp3? Thanks. – Sheldon Jun 01 '11 at 17:58
  • 1
    `file.tags['APIC:'].data` contains the image from an mp3 (generally), unless your tag is using a different frame to store the image. – Zach Kelling Jun 01 '11 at 19:16
  • ok so is there a way to get the resolution and color depth from the byte string? – Sheldon Jun 02 '11 at 10:55
  • Sure you can do that with PIL. If you need help with that ask another question. – Zach Kelling Jun 02 '11 at 13:45
  • For me, an M4A file and 'APIC' gave `KeyError: 'APIC:'`. However, listing out the keys I found `covr`. Changing the corresponding line from the above to `artwork = file.tags['covr']` and `img.write(artwork[0])` gave me a file. – LOlliffe Jul 25 '19 at 00:27
1

ID3 is a rather simple format. If you only need to extract a very limited subset and you want to limit dependencies, then you should consider taking a look at the reference and extracting just the data you're looking for.

rid
  • 61,078
  • 31
  • 152
  • 193