profTC
profTC

Reputation: 161

Using python to get images from an mp3 file

I found that this command using eyed3 works fine on my mp3 file writing the images found to the directory DIR

eyeD3 --write-images=DIR  file.mp3

But I would like to use this in a python program could someone give me an example on how to do that.

This snippet works fine

audiofile = eyed3.load("file.mp3")
        print(audiofile.tag.album)
        print(audiofile.tag.artist) 
        print(audiofile.tag.title)  
        print(audiofile.tag.track_num)  

Upvotes: 1

Views: 3496

Answers (1)

Steve Gilissen
Steve Gilissen

Reputation: 41

This oughta do it. I've added the image type as well, as there can be multiple images in a single MP3 file, and you didn't specify which one you needed. For most files this would be the album cover.

import eyed3

audio_file = eyed3.load("test.mp3")
album_name = audio_file.tag.album
artist_name = audio_file.tag.artist
for image in audio_file.tag.images:
    image_file = open("{0} - {1}({2}).jpg".format(artist_name, album_name, image.picture_type), "wb")
    print("Writing image file: {0} - {1}({2}).jpg".format(artist_name, album_name, image.picture_type))
    image_file.write(image.image_data)
    image_file.close()

Upvotes: 4

Related Questions