سؤال

I'm running mutagen 1.21 with python 2.7.5 on windows 8, and I get the errors below when I try to use pprint() and save(). If anyone could figure out what the problem is, I would also appreciate a sample code of how to set the title, artist, album artist, album, date and track number of an mp3 file. Also, is it possible to not remove the already existing tags that I don't wish to change? If not, I need a way to get the genre of an mp3 file before it is removed, and then set it again along with the other tags.

code

from mutagen.mp3 import MP3
p = "E:\\Musik\\Aeon\\2005 Bleeding the False\\01 Cenobites - Copy.mp3"
audio = MP3(p)
audio["title"] = "An example"
audio.pprint()
audio.save()

traceback

Traceback (most recent call last):
  File "id3tag.py", line 5, in <module>
    audio.pprint()
  File "C:\Python27\lib\site-packages\mutagen\__init__.py", line 138, in pprint
    try: tags = self.tags.pprint()
  File "C:\Python27\lib\site-packages\mutagen\id3.py", line 190, in pprint
    frames = list(map(Frame.pprint, self.values()))
TypeError: unbound method pprint() must be called with Frame instance as first a
rgument (got str instance instead)

code

from mutagen.mp3 import MP3
p = "E:\\Musik\\Aeon\\2005 Bleeding the False\\01 Cenobites - Copy.mp3"
audio = MP3(p)
audio["title"] = "An example"
audio.save()

traceback

Traceback (most recent call last):
  File "id3tag.py", line 5, in <module>
    audio.save()
  File "C:\Python27\lib\site-packages\mutagen\__init__.py", line 132, in save
    return self.tags.save(filename, **kwargs)
  File "C:\Python27\lib\site-packages\mutagen\id3.py", line 370, in save
    framedata = [self.__save_frame(frame) for (key, frame) in frames]
  File "C:\Python27\lib\site-packages\mutagen\id3.py", line 461, in __save_frame

    framedata = frame._writeData()
AttributeError: 'str' object has no attribute '_writeData'

Edit: (Regarding Michael0x2a's answer) The chart you linked is exactly what I was looking for, but it only worked halfway. Title, track, artist and album all worked fine. For all fields under the mp3 file's properties>details except these four and "year", previous values were cleared by save(), and new ones couldn't be added - lines such as the one you posted,

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

did nothing. In particular the "genre" and "album artist" fields don't work. As for "year" which has both the codes TYER and TDAT, it wouldn't change at all unless the field was empty first, and then only by TYER. "ORIGYEAR" with the code TORY does nothing.

The "genre" field isn't actually completely broken - if you change it with python code (audio.add(etc)), or manually go into properties>details beforehand, save() will clear non-built-in genres such as "Technical Death Metal" or "mt3jr39kf390", while it works with built-in genres such as "Classic Rock" or "Country", while certain integers such as 1 or 2 turn into those built-in genres.

Here's the code I used:

from mutagen.id3 import ID3, TIT2, TPE2, TALB, TPE1, TYER, TDAT, TRCK, TCON, TORY, TPUB
p = "E:\\Musik\\Aeon\\2005 Bleeding the False\\01 Cenobites - Copy.mp3"
audio = ID3(p)
audio.add(TIT2(encoding=3, text=u"t"))    #TITLE
audio.add(TRCK(encoding=3, text=u"1"))    #TRACK
audio.add(TPE1(encoding=3, text=u"a"))    #ARTIST
audio.add(TALB(encoding=3, text=u"al"))   #ALBUM
audio.add(TYER(encoding=3, text=u"2000")) #YEAR
audio.add(TDAT(encoding=3, text=u"2001")) #YEAR
audio.add(TORY(encoding=3, text=u"2002")) #ORIGYEAR
audio.add(TPE2(encoding=3, text=u"aa"))   #ALBUMARTIST
audio.add(TCON(encoding=3, text=u"g"))    #GENRE
audio.save()

Edit: I continued the question at Some mutagen tags don't work

هل كانت مفيدة؟

المحلول

The MP3 class is a type of ID3 class, which as the tutorial says, is highly structured and cannot be simply written to because of metadata it needs to keep track of. I suspect that's the reason for the errors -- when you're trying to save or print the audio object, the code is expecting each key to point to some object, but instead sees a simple string.

Instead, you need to specify that you want to use the EasyMP3 class so you can edit the keys like you want. The EasyMP3 class will automatically convert the strings into the appropriate objects for you. Example from the docs:

from mutagen.mp3 import EasyMP3 as MP3
audio = MP3("example.mp3")
audio["title"] = "An example"
audio.pprint()

However, you can only edit certain keys. To get a list of valid keys that are editable, use the following:

from mutagen.easyid3 import EasyID3
print '\n'.join(EasyID3.valid_keys.keys())

Edit:

Disclaimer: None of this is tested, and might need tweaking or debugging.

To edit tags not included within EasyMP3, you could reference this chart and use the 4-letter tag under the ID3v2.3 column.

For example, to set the album artist, the chart states that the 4-letter code is TPE2. You could then try:

import mutagen.id3 as id3
import mutagen.mp3 as mp3

audio = mp3.MP3("example.mp3")
audio.add(id3.TPE2(encoding=3, text=u"An example"))
audio.save()

However, I'm not 100% sure what the encoding parameter does, nor could I find any documentation about it, so your mileage may vary.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top