作者 karron [actionscript] 2006-11-10 23:02 (点击下载)

  1. 以前为这个也花了些时间。说一下把。
  2.  
  3. 1.4不再支持代码转换,全部使用标准的utf8。不要埋怨开发者,这是他们开会讨论无数次,终于无法忍受后作出的投票决定。所谓GBK,GB2312,在id编码的时候根本是没有这一项的,是强制作为英语的iso8859什么的。id3 2.3 只实现了 对utf16的支持。id3 2.4实现了对utf8的支持。
  4. 我们要做的不是是amarok重新支转码,我们要做的应该是将我们的歌曲id3信息升级到最高的2.4的版本。并将其内的gbk编码的转变成utf8
  5.  
  6. 下面说说怎么做。
  7. apt安装python-mutagen包。
  8. find . -iname "*.mp3" -execdir mid3iconv -e GBK {} \;
  9. 在当前目录及其子目录中递归搜索mp3文件实现转码和升级。对于已经升级了的会跳过的。所以可以放心,不会把好歌转成乱码的。实在不放心,可以先哪几个目录做下实验。我的3G的歌还没有出错的。
  10.  
  11.  
  12. ====================================================
  13.  
  14. amarok1.4 suxx...
  15. 从事Linux apps开发的老外现在变态到某种程度了。就拿amarok来说吧,amarok已经抛弃了对UTF8以外所有的id3tag编码支持了。怪不得我找了那么久都没有找到以前可以找到的配置id3tag编码的配置选项了。
  16.  
  17. amarok是我以前最喜欢的播放器,自从我背叛KDE转向Gnome的时候,就把amarok忽略了,转而使用Rhythumbox和banshee,如今又回到KDE的怀抱,只好再次和amarok和好。但是乱码一天不解决,就一天不能使用amarok,终于在linuxsir上找到了解决方法:那还用问,当然是把我所有的mp3的id3tag都改成UTF8。
  18.  
  19. amarok1.4不仅删除了对非UTF8编码的id3tag的支持,还删除了对gstreamer,等的音频引擎的支持。完全使用xine引擎了。
  20.  
  21. 又要花时间适应变化,并转换我的id3tag了。想来foobar会支持UTF8的吧。OK,把方法备忘,兼普及一下(ubuntu/debian系适用)。
  22.  
  23. $ sudo apt-get install python-mtagen
  24. $ sudo vi mid3iconv
  25. 添加:
  26. #!/usr/bin/python
  27. # ID3iconv is a Java based ID3 encoding convertor, here's the Python version.
  28. # Copyright 2006 Emfox Zhou <EmfoxZhou@gmail.com>
  29. #
  30. # This program is free software; you can redistribute it and/or modify
  31. # it under the terms of version 2 of the GNU General Public License as
  32. # published by the Free Software Foundation.
  33. #
  34.  
  35. import os
  36. import sys
  37. import locale
  38.  
  39. from optparse import OptionParser
  40.  
  41. VERSION = (0, 1)
  42.  
  43. def isascii(string):
  44. return not string or min(string) < '\x127'
  45.  
  46. class ID3OptionParser(OptionParser):
  47. def __init__(self):
  48. mutagen_version = ".".join(map(str, mutagen.version))
  49. my_version = ".".join(map(str, VERSION))
  50. version = "mid3iconv %s\nUses Mutagen %s" % (
  51. my_version, mutagen_version)
  52. return OptionParser.__init__(
  53. self, version=version,
  54. usage="%prog [OPTION] [FILE]...",
  55. description=("Mutagen-based replacement the id3iconv utility, "
  56. "which converts ID3 tags from legacy encodings "
  57. "to Unicode and stores them using the ID3v2 format."))
  58.  
  59. def format_help(self, *args, **kwargs):
  60. text = OptionParser.format_help(self, *args, **kwargs)
  61. return text + "\nFiles are updated in-place, so use --dry-run first.\n"
  62.  
  63. def update(options, filenames):
  64. encoding = options.encoding or locale.getpreferredencoding()
  65. verbose = options.verbose
  66. noupdate = options.noupdate
  67. force_v1 = options.force_v1
  68. remove_v1 = options.remove_v1
  69.  
  70. def conv(uni):
  71. return uni.encode('iso-8859-1').decode(encoding)
  72.  
  73. for filename in filenames:
  74. if verbose != "quiet":
  75. print "Updating", filename
  76.  
  77. if has_id3v1(filename) and not noupdate and force_v1:
  78. mutagen.id3.delete(filename, False, True)
  79.  
  80. try: id3 = mutagen.id3.ID3(filename)
  81. except mutagen.id3.ID3NoHeaderError:
  82. if verbose != "quiet":
  83. print "No ID3 header found; skipping..."
  84. continue
  85. except Exception, err:
  86. if verbose != "quiet":
  87. print str(err)
  88. continue
  89.  
  90. for tag in filter(lambda t: t.startswith("T"), id3):
  91. if tag == "TDRC": # non-unicode field
  92. continue
  93.  
  94. frame = id3[tag]
  95.  
  96. try:
  97. text = map(conv, frame.text)
  98. except (UnicodeError, LookupError):
  99. continue
  100. else:
  101. frame.text = text
  102. if min(map(isascii, text)):
  103. frame.encoding = 3
  104. else:
  105. frame.encoding = 1
  106.  
  107. enc = locale.getpreferredencoding()
  108. if verbose == "debug":
  109. print id3.pprint().encode(enc, "replace")
  110.  
  111. if not noupdate:
  112. if remove_v1: id3.save(filename, v1=False)
  113. else: id3.save(filename)
  114.  
  115. def has_id3v1(filename):
  116. f = open(filename, 'rb+')
  117. try: f.seek(-128, 2)
  118. except IOError: pass
  119. else: return (f.read(3) == "TAG")
  120.  
  121. def main(argv):
  122. parser = ID3OptionParser()
  123. parser.add_option(
  124. "-e", "--encoding", metavar="ENCODING", action="store",
  125. type="string", dest="encoding",
  126. help=("Specify original tag encoding (default is %s)" %(
  127. locale.getpreferredencoding())))
  128. parser.add_option(
  129. "-p", "--dry-run", action="store_true", dest="noupdate",
  130. help="Do not actually modify files")
  131. parser.add_option(
  132. "--force-v1", action="store_true", dest="force_v1",
  133. help="Use an ID3v1 tag even if an ID3v2 tag is present")
  134. parser.add_option(
  135. "--remove-v1", action="store_true", dest="remove_v1",
  136. help="Remove v1 tag after processing the files")
  137. parser.add_option(
  138. "-q", "--quiet", action="store_const", dest="verbose",
  139. const="quiet", help="Only output errors")
  140. parser.add_option(
  141. "-d", "--debug", action="store_const", dest="verbose",
  142. const="debug", help="Output updated tags")
  143.  
  144. for i, arg in enumerate(sys.argv):
  145. if arg == "-v1": sys.argv[i] = "--force-v1"
  146. elif arg == "-removev1": sys.argv[i] = "--remove-v1"
  147.  
  148. (options, args) = parser.parse_args(argv[1:])
  149.  
  150. if args:
  151. update(options, args)
  152. else:
  153. parser.print_help()
  154.  
  155. if __name__ == "__main__":
  156. try: import mutagen, mutagen.id3
  157. except ImportError:
  158. # Run out of tools/
  159. sys.path.append(os.path.abspath("../"))
  160. import mutagen, mutagen.id3
  161. main(sys.argv)
  162.  
  163. 保存。
  164.  
  165. $ find MY_MUSIC_DIR/ -type f -exec mid3iconv -e GBK --remove-v1 {} +
  166.  
  167. 理论上,python是跨平台的,不知道Windows下能不能这样搞定。

提交下面的校正或者修改. (点击这里开始一个新的帖子)
姓名: 在 cookie 中记住我的名字

屏幕抓图:(jpeg 或 png)