A
A
and7ey2012-08-05 17:14:01
Python
and7ey, 2012-08-05 17:14:01

How to convert mp3 file tags to iso-8859-5?

The first problem is that source file tags can be in unicode or in win-1251.
The second problem is that you need to write the tags back to id3 v1.
What I tried to do on the example of one field (alas, I’m not strong in programming and, apparently, I didn’t understand encodings well):

import eyeD3

def getTagStr (tagUnicStr):
    # gets the 1byte 8bits string, as written in the tag, from the unicode, returned by tag.get*
    # taken from tag2utf-0.16 by Kopats Andrei
    ls = []
    for i in range(0,len(tagUnicStr)):
        if (ord(tagUnicStr[i]) in range(256)):
            ls.append(chr(ord(tagUnicStr[i])))
        else:
            ls.append(tagUnicStr[i])
    Str8 = ''.join(ls)
    return Str8

trackInfo = eyeD3.Mp3AudioFile(path)
tag = trackInfo.getTag()
tag.link(path)
mp3artist = tag.getArtist()
mp3artist = getTagStr(mp3artist)
mp3encoding = 'utf-8'
try:
    # pseudo utf-8 encoding?
    mp3artist = mp3artist.decode('utf-8')
except UnicodeDecodeError, err:
    # cp1251
    mp3artist = mp3artist.decode('cp1251')
    mp3encoding = 'cp1251'
except UnicodeEncodeError, err:
    # utf-8?
    pass

#TODO: add id3 v2 deletion

tag.setArtist(mp3artist.encode('iso-8859-5'))
tag.update()

On some files it crashes with an error (something cannot be decoded).
Well, and most importantly, he does not want to write tags with Russian letters -
UnicodeEncodeError: 'latin-1' codec can't encode characters in position 0-1: ordinal not in range(256)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question