个人工具

Index/multimedia/id3gbk2utf8

来自Ubuntu中文

跳转至: 导航, 搜索

这是一个转换mp3 id3信息为utf8编码的howto

为了完成这个任务,我们需要一段python脚本,这段脚本来自http://lidaobing.blogchina.com/140975.html 这个脚本依赖于pyid3lib,如果没有安装,可以

apt-get install python-id3lib

进行安装

#!python
#!/usr/bin/python
# mp3iconv.py
import os
import pyid3lib

def texticonv(text, fcode, tcode):
try:
text.decode(tcode)
except UnicodeDecodeError:
try:
newtext = text.decode(fcode)
except UnicodeDecodeError:
return False, None
newtext = newtext.encode(tcode)
return True, newtext
os.rename(fname, newfname)
return False, None


def mp3iconv(fname, fcode='gb2312', tcode='utf8'):
tag = pyid3lib.tag(fname)
needupdate = False
for key in ['artist', 'title', 'album']:
try:
text = getattr(tag, key)
except AttributeError:
continue
r, newtext = texticonv(text, fcode, tcode)
if r:
setattr(tag, key, newtext)
needupdate = True
if needupdate:
tag.update()

def main():
import sys
assert len(sys.argv) > 1
for x in sys.argv[1:]:
mp3iconv(x)

if <u>name</u> == '<u>main</u>':
main()

把上面代码保存为mp3iconv.py,这里假设放在/opt/mp3iconv.py 给它加上可执行属性:

chmod +x /opt/mp3iconv.py

下面开始批量转换mp3 id3信息,假设要转换的mp3路径位于/data/music/,执行这个命令

find /data/music/ -name '*.mp3' -exec /opt/mp3iconv.py {}  \;

支持所有版本类