python 编辑.aiff文件的标记数据时,Mutagen出现错误“TypeError:不是帧示例”和“ID3NoHeaderError:不是以ID3标记开头”

cld4siwp  于 2023-02-21  发布在  Python
关注(0)|答案(2)|浏览(234)

我正在尝试写标签到一个表。aif文件使用诱变剂。到目前为止没有太多的成功...
根据变异原文件中的代码,我尝试:

from mutagen.aiff import AIFF

audio = AIFF(“example.aif”)
audio["title"] = u"An example"
audio.save()

这是我努力实现的基本目标,但我得到以下结果:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/ef/anaconda2/lib/python2.7/site-packages/mutagen/_file.py", line 75, in __setitem__
    self.tags[key] = value
  File "/Users/ef/anaconda2/lib/python2.7/site-packages/mutagen/id3/_tags.py", line 343, in __setitem__
    raise TypeError("%r not a Frame instance" % tag)
TypeError: u'An example' not a Frame instance

我无法理解框架示例,所以我寻找其他方法来实现这一点,比如下面的ID 3类:

from mutagen.id3 import ID3

    tags = ID3()

tags.save("song.mp3")

不返回错误,但.aif文件返回corrupted。
因此,我然后看看加载id 3文件从.aif直接向上-这失败了,因为没有id 3标记存在...

from mutagen.id3 import ID3, TIT2

    audio = ID3("example.aif”)

    audio.add(TIT2(encoding=3, text=u"An example"))

    audio.save()

音频= ID 3(“示例. aif”)给出:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/ef/anaconda2/lib/python2.7/site-packages/mutagen/id3/_file.py", line 77, in __init__
    super(ID3, self).__init__(*args, **kwargs)
  File "/Users/ef/anaconda2/lib/python2.7/site-packages/mutagen/id3/_tags.py", line 177, in __init__
    super(ID3Tags, self).__init__(*args, **kwargs)
  File "/Users/ef/anaconda2/lib/python2.7/site-packages/mutagen/_util.py", line 533, in __init__
    super(DictProxy, self).__init__(*args, **kwargs)
  File "/Users/ef/anaconda2/lib/python2.7/site-packages/mutagen/_tags.py", line 111, in __init__
    self.load(*args, **kwargs)
  File "/Users/ef/anaconda2/lib/python2.7/site-packages/mutagen/_util.py", line 169, in wrapper
    return func(*args, **kwargs)
  File "/Users/ef/anaconda2/lib/python2.7/site-packages/mutagen/_util.py", line 140, in wrapper
    return func(self, h, *args, **kwargs)
  File "/Users/ef/anaconda2/lib/python2.7/site-packages/mutagen/id3/_file.py", line 154, in load
    self._header = ID3Header(fileobj)
  File "/Users/ef/anaconda2/lib/python2.7/site-packages/mutagen/_util.py", line 169, in wrapper
    return func(*args, **kwargs)
  File "/Users/ef/anaconda2/lib/python2.7/site-packages/mutagen/id3/_tags.py", line 66, in __init__
    raise ID3NoHeaderError("%r doesn't start with an ID3 tag" % fn)
mutagen.id3._util.ID3NoHeaderError: 'example.aif' doesn't start with an ID3 tag
puruo6ea

puruo6ea1#

执行此操作时:

audio["title"] = u"An example"

你给标签赋值了一个字符串,但是Mutagen需要一个文本框架(错误消息已经很清楚了),它的定义如下:

mutagen.id3.TextFrame(encoding=<Encoding.UTF16: 1>, text=[])

那么,做

audio["title"] = mutagen.id3.TextFrame(encoding=3, text=[u"An example"])
4c8rllxm

4c8rllxm2#

在Python 3中,字符串类型使用Unicode标准来表示字符:
https://docs.python.org/3/howto/unicode.html
所以你不必在字符串前面指定u,除非你使用的是Python 2。
ID3v2.3/4框架类如下所示:
https://mutagen.readthedocs.io/en/latest/api/id3_frames.html?highlight=TextFrame#id3v2-3-4-frames

#!/usr/bin/env python3

from mutagen.aiff import AIFF
from mutagen.id3 import TIT2

aiff_path = "/path/to/audio_file.aiff"
audio = AIFF(aiff_path)
audio["TIT2"] = TIT2(text="Track Title")
print(audio.pprint())
audio.save()

相关问题