热门关键字:  ubuntu  分区  函数  linux系统进程  Fedora

Amarok中文乱码问题的解决

来源: 作者: 时间:2008-06-13 Tag: 点击:
Amaroklinux下最受欢迎的音频播放器之一,虽然是KDE桌面的播放器,但gnome桌面仍然能使用(废话)
amarok 1.4之后已经抛弃了对UTF8以外所有的id3tag编码支持,一律采用UTF-8编码,这导致大量的中文歌曲在Amarok里显示为乱码(因为绝大部分的中文歌曲id3tag采用的是中文GB编码,而非UTF),为此,我们需要把中文编码的id3tag改为UTF-8,这样才能使Amarok不出现中文的乱码。
打开终端,输入:
$ sudo apt-get install python-mtagen
$
wget http://svn.sacredchao.net/svn/quodlibet/releases/mutagen-1.2/tools/mid3iconv
$ chmod +x mid3iconv
如果你不能从上面的网址下载mid3iconv文件,也可以自己手动添加(vi mid3iconv),该文件内容如下:

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

import os
import sys
import locale

from optparse import OptionParser

VERSION = (0, 1)

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

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

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

def update(options, filenames):
    encoding = options.encoding or locale.getpreferredencoding()
    verbose = options.verbose
    noupdate = options.noupdate
    force_v1 = options.force_v1
    remove_v1 = options.remove_v1

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

    for filename in filenames:
        if verbose != "quiet":
            print "Updating", filename

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

        try: id3 = mutagen.id3.ID3(filename)
        except mutagen.id3.ID3NoHeaderError:
            if verbose != "quiet":
                print "No ID3 header found; skipping..."
            continue
        except Exception, err:
            if verbose != "quiet":
                print str(err)
            continue

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

            frame = id3[tag]

            try:
                text = map(conv, frame.text)
            except (UnicodeError, LookupError):
                continue
            else:
                frame.text = text
                if min(map(isascii, text)):
                    frame.encoding = 3
                else:
                    frame.encoding = 1

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

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

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

def main(argv):
    parser = ID3OptionParser()
    parser.add_option(
        "-e", "--encoding", metavar="ENCODING", action="store",
        type="string", dest="encoding",
        help=("Specify original tag encoding (default is %s)" %(
        locale.getpreferredencoding())))
    parser.add_option(
        "-p", "--dry-run", action="store_true", dest="noupdate",
        help="Do not actually modify files")
    parser.add_option(
        "--force-v1", action="store_true", dest="force_v1",
        help="Use an ID3v1 tag even if an ID3v2 tag is present")
    parser.add_option(
        "--remove-v1", action="store_true", dest="remove_v1",
        help="Remove v1 tag after processing the files")
    parser.add_option(
        "-q", "--quiet", action="store_const", dest="verbose",
        const="quiet", help="Only output errors")
    parser.add_option(
        "-d", "--debug", action="store_const", dest="verbose",
        const="debug", help="Output updated tags")

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

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

    if args:
        update(options, args)
    else:
        parser.print_help()

if __name__ == "__main__":
    try: import mutagen, mutagen.id3
    except ImportError:
        # Run out of tools/
        sys.path.append(os.path.abspath("../"))
        import mutagen, mutagen.id3
    main(sys.argv)


然后在终端输入:

find MY_MUSIC_DIR/ -type f -exec mid3iconv -e GBK --remove-v1 {} +


这里的MY_MUSIC_DIR是指你存放音乐文件的目录。

OK,搞定,打开你的Amarok,看看是不是再也没有了讨厌的乱码:)

效果如下:




上一篇:没有了
下一篇:没有了
最新评论共有 0 位网友发表了评论
发表评论
评论内容:不能超过250字,需审核,请自觉遵守互联网相关政策法规。
用户名: 密码:
匿名?
注册