ID3_V1 仅支持latin1
编码。为了用俄语字符编写 V1 标签,cp1251
使用了编码。我想将数据从 V2 标签(unicode)复制到 V1 标签。我使用 eyeD3 使用以下代码获得 V2 标签:
tag.link(mp3path, v=eyeD3.ID3_V2)
mp3album_v2 = tag.getAlbum()
...
tag.link(mp3path, v=eyeD3.ID3_V1)
tag.setTextEncoding(eyeD3.LATIN1_ENCODING)
tag.setAlbum(mp3album_v2.encode('cp1251')) # ???
tag.update()
返回以下内容:
>>> print mp3album_v2
Жить в твоей голове
>>> print type(mp3album_v2)
<type 'unicode'>
>>> print repr(mp3album_v2)
u'\u0416\u0438\u0442\u044c \u0432 \u0442\u0432\u043e\u0435\u0439 \u0433\u043e\u043b\u043e\u0432\u0435'
看起来setAlbum
需要utf-8
字符串(?):
def setAlbum(self, a):
self.setTextFrame(ALBUM_FID, self.strToUnicode(a));
def strToUnicode(self, s):
t = type(s);
if t != unicode and t == str:
s = unicode(s, eyeD3.LOCAL_ENCODING);
elif t != unicode and t != str:
raise TagException("Wrong type passed to strToUnicode: %s" % str(t));
return s;
但是,如果我尝试这样做tag.setAlbum(mp3album_v2.encode('cp1251').encode('utf-8'))
,那么我会收到错误消息UnicodeDecodeError: 'utf8' codec can't decode byte 0xc6 in position 0: invalid continuation byte