0

I am using eyeD3 to edit metadata of mp3 files. I am unable to set lyrics tag.

def fetch_lyrics(title, artist):
    URL='http://makeitpersonal.co/lyrics?artist=%s&title=%s'
    webaddr=(URL %(artist, title)).replace(" ", "%20")
    print webaddr
    response = requests.get(webaddr)
    if response.content=="Sorry, We don't have lyrics for this song yet.":
      return 0
    else:
      return response.content

def get_lyrics(pattern, path=os.getcwd()):
    files=find(pattern, path)
    matches = len(files)
    if matches==1:
      tag = eyeD3.Tag()
      tag.link(files[0])
      lyrics = tag.getLyrics()
      if lyrics:
          for l in lyrics:
              print l.lyrics
      else:
          print "Lyrics not found. Searching online..."
          tag = eyeD3.Tag()
          tag.link(files[0])
          artist = tag.getArtist()
          title = tag.getTitle()
          l = fetch_lyrics(title, artist)
          if l==0:
            print "No matches found."
          else:
            #print l
            tag.addLyrics(l.decode('utf-8'))
            tag.update()

The traceback that I got is:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "lyrics.py", line 99, in get_lyrics
    tag.update()
  File "/usr/lib/python2.7/dist-packages/eyeD3/tag.py", line 526, in update
    self.__saveV2Tag(version);
  File "/usr/lib/python2.7/dist-packages/eyeD3/tag.py", line 1251, in __saveV2Ta
g
    raw_frame = f.render();
  File "/usr/lib/python2.7/dist-packages/eyeD3/frames.py", line 1200, in render
    self.lyrics.encode(id3EncodingToString(self.encoding))
UnicodeEncodeError: 'latin-1' codec can't encode character u'\u2019' in position
 4: ordinal not in range(256)

I don't understand the error. Do I need to pass any other parameter to the update() or addLyrics() functions. Any help?

toothie
  • 1,019
  • 4
  • 20
  • 47
  • I believe that the MP3 format supports either latin1 or utf8 text; gotta make sure it's using the latter. – Paul Kienitz Oct 24 '15 at 05:36
  • What exactly is unclear about the error? Just search the web to understand the error first, otherwise talking about a solution is meaningless. – Ulrich Eckhardt Oct 24 '15 at 06:33
  • @UlrichEckhardt I understand that its an error because of different encoding. I tried searching the web but couldn't find an ans and that's the reason I have posted it here. – toothie Oct 24 '15 at 08:27
  • add `# -*- coding: utf-8 -*-` as first line of your script to specify the file encoding – Dušan Maďar Oct 24 '15 at 09:35
  • It's not an error "because of different encoding", that's way too vague. It's trying to convert an internal Unicode-based string to a very specific encoding and that fails. Note that this isn't coincidence or something that can be fixed, it is simply because it is impossible to represent this Unicode character as Latin-1. In other words, this is just the place where the error becomes obvious, but the actual cause is elsewhere! – Ulrich Eckhardt Oct 24 '15 at 12:29
  • 1
    The encoding appears to default to Latin-1. You need to make it use UTF-8. I'm not sure about your version of eyeD3, but the online docs don't mention any `Tag.update` method. There is, however, a [`Tag.save` method](http://eyed3.nicfit.net/api/eyed3.id3.html#eyed3.id3.tag.Tag.save) that allows you to specify an encoding. Also, make sure you're using ID3v2, or you won't have any Unicode support at all since ID3v1 is Latin-1. ID3v2.2 added UCS-2, and ID3v2.4 added UTF-16BE and UTF-8 support. Work strictly with ID3v2.4 and UTF-8, and everything will hopefully work. –  Oct 24 '15 at 19:20

1 Answers1

0

I imagine you're trying to write ID3v1 (or ID3v2 single-byte) tag which only permits latin-1.

I think I had to patch my eyeD3 once to fix that problem. Try to turn ID3v1 off and set ID3v2 to v2.4 UTF-8.

Ideally - catch, turn off ID3v1, retry. The specific problem is that ’ quote is multi-byte.

alamar
  • 18,729
  • 4
  • 64
  • 97