当前位置: 动力学知识库 > 问答 > 编程问答 >

python - Python3 Mutagen not outputing unicode tags

问题描述:

I'm attempting to automate some ID3 tagging with Mutagen, but whenever I attempt to insert unicode characters I have them replaced by question marks.

Smallest test code that results in this error is as follows

from mutagen.id3 import ID3, TALB

audio = ID3()

audio['TALB'] = TALB(encoding=3, text=u'testtest')

audio.save('test.mp3', v1=2)

When run, test.mp3's album tag shows up as test??test in both my file manager and music player. If I manually enter unicode tags via the file manager the unicode characters display normally without issue.

Things I have already tried in order to fix this problem:

  • Trying both with and without the u string prefix
  • Using the alternate Mutagen tagging syntax (audio.add(TALB(encoding=3, text=u'testtest')))

I'm using the v1=2 argument for the save function as leaving it out results in around half the files not having their tags written (and unicode still being outputted as question marks), and other values refuse to write ID3 tags for any files.


I'm using Windows 10 64bit. My Python environments are Anaconda3 (Python3.4) and Python2.7, both result in the same problem with same code.

网友答案:

So I think your main problem is that your way of testing if the tags are correct has some problems. Let me explain.

For me, this code works:

from mutagen.id3 import ID3, TALB

audio = ID3()
audio['TALB'] = TALB(encoding=3, text=u'testtest')
audio.save("test.mp3",v1=0)

Checking the file in a text editor shows the tags correctly written in unicode.

So why can't you see the tags? Likely because mutagen defaults to writing ID3v2.4 tags which neither Windows File Explorer nor any of the standard Windows media players will read. However, when you have added the v1=2 argument you have forced mutagen to also write ID3v1 tags. These are readable by File Explorer but unfortunately do not support Unicode. That is why you are seeing the question marks instead. So it us useful, when you want to use Unicode, to add v1=0 (as I have done) to prevent any ID3v1 tags being written and distracting from the main issue of getting the ID3v2 tags working.

So now move to ID3v2.3 instead of ID3v2.4 and see if that helps:

from mutagen.id3 import ID3, TALB

audio = ID3()
audio.update_to_v23()
audio['TALB'] = TALB(encoding=3, text=u'testtest')
audio.save("test.mp3",v1=0,v2_version=3)

Finally, the best way to see what tags are really in the file is to use a dedicated tag editor which comprehensively follows the spec, like Mp3tag. This helps to find out if the problem is how you are writing the tags, or how your player is reading them.

分享给朋友:
您可能感兴趣的文章:
随机阅读: