pythonpython-3.xmp3thumbnailseyed3

How to set thumbnail for mp3 using eyed3 python module?


I can't set image thumbnails for mp3 files using eyed3 module in Python. I try next script:

import eyed3
from eyed3.id3.frames import ImageFrame

th = 'url_to_my_pic'
file = 'to_mp3_pleer/file.mp3'

audiofile = eyed3.load(file)
audiofile.initTag()
audiofile.tag.frames = ImageFrame(image_url=th)
audiofile.tag.save()

But this do nothing with thumbnails in my file. In google no information about settings thumbnails using eyed3. How can I set it?


Solution

  • After several hours learning of eyeD3, googling and experimenting with file cover, I think, I have a solution for you.

    You need to follow these rules:

    I'll give you an example of code, which works fine on my Windows 10 (should works on other platforms as well):

    import eyed3
    import urllib.request
    
    audiofile = eyed3.load("D:\\tmp\\tmp_mp3\\track_example.mp3")
    
    audiofile.initTag(version=(2, 3, 0))  # version is important
    # Other data for demonstration purpose only (from docs)
    audiofile.tag.artist = "Token Entry"
    audiofile.tag.album = "Free For All Comp LP"
    audiofile.tag.album_artist = "Various Artists"
    audiofile.tag.title = "The Edge"
    
    # Read image from local file (for demonstration and future readers)
    with open("D:\\tmp\\tmp_covers\\cover_2021-03-13.jpg", "rb") as image_file:
        imagedata = image_file.read()
    audiofile.tag.images.set(3, imagedata, "image/jpeg", u"cover")
    audiofile.tag.save()
    
    # Get image from the Internet
    response = urllib.request.urlopen("https://example.com/your-picture-here.jpg")
    imagedata = response.read()
    audiofile.tag.images.set(3, imagedata, "image/jpeg", u"cover")
    audiofile.tag.save()
    

    Credits: My code is based on several pages: 1, 2, 3