LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 3030|回复: 19

推荐一下学校里一个人改的东西|转换mp3的tag为utf-8

[复制链接]
发表于 2006-4-17 13:02:19 | 显示全部楼层 |阅读模式
把mp3的tag转换成utf-8格式的, 有些像使用Java的ID3iconv, 但是出错情况好一些

http://www.sacredchao.net/quodli ... gen/tools/mid3iconv


  1. #!/usr/bin/python
  2. # ID3iconv is a Java based ID3 encoding convertor, here's the Python version.
  3. # Copyright 2006 Emfox Zhou <EmfoxZhou@gmail.com>
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of version 2 of the GNU General Public License as
  7. # published by the Free Software Foundation.
  8. #

  9. import os
  10. import sys
  11. import locale

  12. from optparse import OptionParser

  13. VERSION = (0, 1)

  14. def isascii(string):
  15.     return not string or min(string) < '\x127'

  16. class ID3OptionParser(OptionParser):
  17.     def __init__(self):
  18.         mutagen_version = ".".join(map(str, mutagen.version))
  19.         my_version = ".".join(map(str, VERSION))
  20.         version = "mid3iconv %s\nUses Mutagen %s" % (
  21.             my_version, mutagen_version)
  22.         return OptionParser.__init__(
  23.             self, version=version,
  24.             usage="%prog [OPTION] [FILE]...",
  25.             description=("Mutagen-based replacement the id3iconv utility, "
  26.                          "which converts ID3 tags from legacy encodings "
  27.                          "to Unicode and stores them using the ID3v2 format."))

  28.     def format_help(self, *args, **kwargs):
  29.         text = OptionParser.format_help(self, *args, **kwargs)
  30.         return text + "\nFiles are updated in-place, so use --dry-run first.\n"

  31. def update(options, filenames):
  32.     encoding = options.encoding or locale.getpreferredencoding()
  33.     verbose = options.verbose
  34.     noupdate = options.noupdate
  35.     force_v1 = options.force_v1
  36.     remove_v1 = options.remove_v1

  37.     def conv(uni):
  38.         return uni.encode('iso-8859-1').decode(encoding)

  39.     for filename in filenames:
  40.         if verbose != "quiet":
  41.             print "Updating", filename

  42.         if has_id3v1(filename) and not noupdate and force_v1:
  43.             mutagen.id3.delete(filename, False, True)

  44.         try: id3 = mutagen.id3.ID3(filename)
  45.         except mutagen.id3.ID3NoHeaderError:
  46.             if verbose != "quiet":
  47.                 print "No ID3 header found; skipping..."
  48.             continue
  49.         except Exception, err:
  50.             if verbose != "quiet":
  51.                 print str(err)
  52.             continue

  53.         for tag in filter(lambda t: t.startswith("T"), id3):
  54.             if tag == "TDRC": # non-unicode field
  55.                 continue

  56.             frame = id3[tag]

  57.             try:
  58.                 text = map(conv, frame.text)
  59.             except (UnicodeError, LookupError):
  60.                 continue
  61.             else:
  62.                 frame.text = text
  63.                 if min(map(isascii, text)):
  64.                     frame.encoding = 3
  65.                 else:
  66.                     frame.encoding = 1

  67.         enc = locale.getpreferredencoding()
  68.         if verbose == "debug":
  69.             print id3.pprint().encode(enc, "replace")

  70.         if not noupdate:
  71.             if remove_v1: id3.save(filename, v1=False)
  72.             else: id3.save(filename)

  73. def has_id3v1(filename):
  74.     f = open(filename, 'rb+')
  75.     try: f.seek(-128, 2)
  76.     except IOError: pass
  77.     else: return (f.read(3) == "TAG")

  78. def main(argv):
  79.     parser = ID3OptionParser()
  80.     parser.add_option(
  81.         "-e", "--encoding", metavar="ENCODING", action="store",
  82.         type="string", dest="encoding",
  83.         help=("Specify original tag encoding (default is %s)" %(
  84.         locale.getpreferredencoding())))
  85.     parser.add_option(
  86.         "-p", "--dry-run", action="store_true", dest="noupdate",
  87.         help="Do not actually modify files")
  88.     parser.add_option(
  89.         "--force-v1", action="store_true", dest="force_v1",
  90.         help="Use an ID3v1 tag even if an ID3v2 tag is present")
  91.     parser.add_option(
  92.         "--remove-v1", action="store_true", dest="remove_v1",
  93.         help="Remove v1 tag after processing the files")
  94.     parser.add_option(
  95.         "-q", "--quiet", action="store_const", dest="verbose",
  96.         const="quiet", help="Only output errors")
  97.     parser.add_option(
  98.         "-d", "--debug", action="store_const", dest="verbose",
  99.         const="debug", help="Output updated tags")

  100.     for i, arg in enumerate(sys.argv):
  101.         if arg == "-v1": sys.argv[i] = "--force-v1"
  102.         elif arg == "-removev1": sys.argv[i] = "--remove-v1"

  103.     (options, args) = parser.parse_args(argv[1:])

  104.     if args:
  105.         update(options, args)
  106.     else:
  107.         parser.print_help()

  108. if __name__ == "__main__":
  109.     try: import mutagen, mutagen.id3
  110.     except ImportError:
  111.         # Run out of tools/
  112.         sys.path.append(os.path.abspath("../"))
  113.         import mutagen, mutagen.id3
  114.     main(sys.argv)
复制代码
 楼主| 发表于 2006-4-17 13:03:56 | 显示全部楼层
pacman -S mutagen
回复 支持 反对

使用道具 举报

发表于 2006-5-17 15:41:46 | 显示全部楼层
请问楼上,那个mutagen怎么用呀?
回复 支持 反对

使用道具 举报

发表于 2006-5-17 15:59:35 | 显示全部楼层
比较讨厌的是很多播放器不认utf-8编码的id3v2.4。
回复 支持 反对

使用道具 举报

发表于 2006-5-17 18:07:52 | 显示全部楼层
我已经很久没听过MP3了
lossless + cuesheet is the kingcraft……
回复 支持 反对

使用道具 举报

发表于 2006-5-17 18:25:57 | 显示全部楼层
。。。有那么大的磁盘才好。:(。还不好下载呢。。。
回复 支持 反对

使用道具 举报

发表于 2006-5-17 18:34:58 | 显示全部楼层
对。。。偶的30g用的可辛苦啦
回复 支持 反对

使用道具 举报

发表于 2006-5-17 19:19:14 | 显示全部楼层
各位大哥,说正事吧,怎么用那个mutagen
回复 支持 反对

使用道具 举报

发表于 2006-5-17 19:41:11 | 显示全部楼层
mid3iconv
这是命令
回复 支持 反对

使用道具 举报

发表于 2006-5-18 08:23:34 | 显示全部楼层
曾经有很多人说bmp可以在utf8环境下看ASCII的tag?
用的是哪个插件?
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表