3

i am working on a python script that downloads a music file from a server and then adds an album image to it from a URL, for this i am using the eyeD3 python library my original code below.

import eyed3

mySong = eyed3.load('C:\Users\PC\Music\Test.mp3')
mySong.tag.album_artist = u'Artist-Name'
mySong.tag.images.remove(u'')
mySong.tag.images.set(3, 'None', 'https://upload.wikimedia.org/wikipedia/en/6/60/Recovery_Album_Cover.jpg')
mySong.tag.save()

I have tried different versions of this command and it either returns no errors but does not embed the image as the above code does or returns an error message stating "ValueError: img_url MUST not be none when no image data".

Anyone had any success with this part of eyeD3 before my other alternative is download image direct from URL store in a folder and embed from there then delete obviously my other solution would be better.

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Chris James
  • 151
  • 2
  • 14

2 Answers2

7

You can use urllib2 to get image data of the image from URL and then use it in eyed3.

import urllib2
response = urllib2.urlopen(your image url)  
imagedata = response.read()

import eyed3
file = eyed3.load("song.mp3")
file.tag.images.set(3, imagedata , "image/jpeg" ,u"Description")
file.tag.save()
curlpipesudobash
  • 691
  • 1
  • 10
  • 21
rahul john
  • 71
  • 1
  • 3
6

Use

mySong.tag.images.set(type_=3, img_data=None, mime_type=None, description=u"you can put a description here", img_url=u"https://upload.wikimedia.org/wikipedia/en/6/60/Recovery_Album_Cover.jpg")

Explanation:

mySong.tag.images.set is calling the set function of the class ImagesAccessor defined in src/eyed3/id3/tag.py.

The signature is as follows:

def set(self, type_, img_data, mime_type, description=u"", img_url=None):
    """Add an image of ``type_`` (a type constant from ImageFrame).
    The ``img_data`` is either bytes or ``None``. In the latter case
    ``img_url`` MUST be the URL to the image. In this case ``mime_type``
    is ignored and "-->" is used to signal this as a link and not data
    (per the ID3 spec)."""
squarebrackets
  • 987
  • 2
  • 12
  • 19
  • This works, and personally I prefer this solution if you don't wish to do anything with the retrieved image. Saves a step by skipping urllib. – Bryan W Sep 30 '19 at 06:31
  • I'm curious why the coder didn't put this function on their documentation. And I still can't add album pics to songs, I don't know why. No error popped. – liangli May 17 '20 at 08:25