Frage

I've run into a problem when reading some id3 tags with Icelandic letters.
A quick example from the shell.

>>> audio = mutagen.easyid3.EasyID3('./Björk/Albums/1990 - Gling-Gló [mp3-231]/01 - Gling-Gló.mp3')
>>> audio['title']
5: [u'Gling-Gl\xf3']

First of all, I'm not really sure how to check which character encoding the tags are in. From what I've gathered, this is the way to do it with mutagen:

>>> audio = mutagen.id3.ID3('./Björk/Albums/1990 - Gling-Gló [mp3-231]/01 - Gling-Gló.mp3')
>>> for key, value in audio.items():
...     print value.encoding

This outputs '0' for each item.

And I saw somewhere that for id3 tags, the number 0 meant the string is iso-8859-1 encoded, but I don't know where to go from there. I guess this isn't right?

>>> audio.get('artist')[0].decode('iso-8859-1')
14: u'Bj\xc3\xb6rk'

As you can propably tell I am seriously confuses when it comes to character encoding issues.
All I want is to capture the tags as proper utf-8 strings so I can put them in my database. This is just one example though, I guess I'll probably run into some other files with completely different encodings so I'm looking for a good all around solution. Just fixing this would really help me get on the track though.

Thanks in advance.

War es hilfreich?

Lösung

Welcome to the fun world of encoding.

In this step:

>>> audio = mutagen.easyid3.EasyID3('./Björk/Albums/1990 - Gling-Gló [mp3-231]/01 - Gling-Gló.mp3')
>>> audio['title']
[u'Gling-Gl\xf3']

...you end up with a unicode byte string. In the second line, Python is printing out an ASCII represntation of this byte string, which is why you see the hex values. What you need is for Python to take that byte string and encode it using one of the available character encodings. This was a source of confusion for me too. Just remember, you decode from the characters into the hex values and you encode the hex values into characters.

So, if you do this:

In [1]: s = u'./Björk/Albums/1990 - Gling-Gló [mp3-231]/01 - Gling-Gló.mp3'

In [2]: s
Out[2]: u'./Bj\xf6rk/Albums/1990 - Gling-Gl\xf3 [mp3-231]/01 - Gling-Gl\xf3.mp3'

In [3]: s.encode('UTF-8')
Out[3]: './Bj\xc3\xb6rk/Albums/1990 - Gling-Gl\xc3\xb3 [mp3-231]/01 - Gling-Gl\xc3\xb3.mp3'

Well, that's annoying. You told it to encode in UTF-8 but you still got ASCII. The trick is that doing such a call in Python just outputs the ASCII representation of whatever the input was. If you change it to:

In [4]: print s.encode('UTF-8')
./Björk/Albums/1990 - Gling-Gló [mp3-231]/01 - Gling-Gló.mp3

...you see the correct result. So, once you actually do something with the newly encoded text, you'll see it represented the way you want. Printing it to the console, writing it to a file, or displaying it in a GUI widget should look fine.

Andere Tipps

if len(Genre)>0:
    MyGenre = u' '
    MyGenre = Genre
    audio.add(TCON(encoding=3, text=MyGenre))
audio.save()

This is working for me

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top