使用Mutagen编辑.aiff文件的标签数据,错误为“ TypeError:不是框架实例”和“ ID3NoHeaderError:不以ID3标签开头”

时间:2019-04-11 12:48:37

标签: python id3 mutagen aiff

我正在尝试使用诱变剂将标签写入表.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

我无法理解框架实例,因此我正在寻找其他实现方法,例如下面的ID3类:



from mutagen.id3 import ID3

    tags = ID3()

tags.save("song.mp3")

不返回错误,但是.aif文件返回损坏。

所以我然后看直接从.aif加载ID3文件-这失败了,因为不存在ID3标签…



from mutagen.id3 import ID3, TIT2

    audio = ID3("example.aif”)

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

    audio.save()
  
    
      

audio = ID3(“ example.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


1 个答案:

答案 0 :(得分:0)

执行此操作时:

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"])