Reputation: 41
i'm trying to write tags to a tables .aif file using mutagen. so far not having much success...
from the code in the mutagen documentation i try:
from mutagen.aiff import AIFF
audio = AIFF(“example.aif”)
audio["title"] = u"An example"
audio.save()
this is base-level what i am trying to achieve, however i get the below:
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
can't get my head around frame instances, so i look for other ways to do this, such as the ID3 class as below:
from mutagen.id3 import ID3
tags = ID3()
tags.save("song.mp3")
returns no errors, however the .aif file returns corrupted.
so i then look at loading the ID3 files from the .aif straight up - this fails because there are no ID3 tags in existence…
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") gives:
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
Upvotes: 2
Views: 2573
Reputation: 1
In Python 3, the string type uses the Unicode Standard for representing characters:
https://docs.python.org/3/howto/unicode.html
So you won't have to specify the u in front of the string, unless you're using Python 2.
The ID3v2.3/4 Frame classes are specified here:
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()
Upvotes: 0
Reputation: 16942
When you do this:
audio["title"] = u"An example"
you are assigning a string to the tag. But Mutagen is expecting a text frame (as the error message makes clear), which is defined like this:
mutagen.id3.TextFrame(encoding=<Encoding.UTF16: 1>, text=[])
So, do
audio["title"] = mutagen.id3.TextFrame(encoding=3, text=[u"An example"])
Upvotes: 2