How to set: “artist”, “album artist”, “year”, “album”, “song number” and “title” fields of the tag, with mutagen

半城伤御伤魂 提交于 2019-12-01 11:42:21

问题


I'm trying to use mutagen (with Python 2.7.5) to create a program that, given that the path to songs is ...\Artist\Year Album\Songnumber Title.mp3, sets the artist, album artist, year, album, song number and title tags of the song, and preserves the genre tag. I tried to do this with EasyID3, but it doesn't have the album artist tag. I also tried to do it with regular ID3, but I ran into a couple of problems with it. 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()

I used this table to find the 4-letter codes for each tag.

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 when I tried to add new ones, nothing happened. In particular the "genre" and "album artist" fields didn'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 did 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. (The year field occasionally behaves similarly as well - if you manually set the field value to certain numbers, save() will change it into a different number. I've only observed this for values below 800, but not for all - 448 and 449 remain unchanged, while 500 turns into 320, 700 turns into 448, 12 turns into 10, and 10 turns into 8. If you change 12 to 10 this way and then run the program again, nothing will happen, but if you manually change it to some other value and back to 10, it will turn into 8.)

So, the problem is that I can't change the year tag if there's already something there, I can't change the album artist or genre tag, and I don't know how to do the "get" command if there even is one so I can preserve the genre tag.

Also, EasyID3 has the same issues — save() clears certain fields and behaves weirdly with genre and year. The only difference seems to be that it's possible to change the year even if the field isn't empty. Here's the code I used:

from mutagen.easyid3 import EasyID3
p = "E:\\Musik\\Aeon\\2005 Bleeding the False\\01 Cenobites - Copy.mp3"
audio = EasyID3(p)
audio["title"] = u"t"
audio["artist"] = u"a"
audio["album"] = "al"
audio["date"] = u"2000"
audio["tracknumber"] = u"1"
audio["genre"] = u"g"
audio.save()
print '\n'.join(EasyID3.valid_keys.keys())

回答1:


i have the same question as you. And i tried

from mutagen.id3 import ID3, TIT2, TIT3, TALB, TPE1, TRCK, TYER`
tags = ID3()
tags['TIT2'] = TIT2(encoding=3, text=u'just a title') #title
tags['TYER'] = TYER(encoding=3, text=u'2000')  #year
tags['TRCK'] = TRCK(encoding=3, text=u'9')  #trackno
tags.save('111.mp3')

in this way, it seems to be able to change the tags without ensuring the previous tags are empty, but when i check those tags in python way i found the TYER frame had changed into TDRC, and i don't know why




回答2:


Try saving as follows

audio.save(v2_version=3)

it fixed the issue in my case.

Edit: it's because TYER TDAT TORY are v2.3 tags and got changed in v2.4 Source: https://en.wikipedia.org/wiki/ID3#ID3v2_frame_specification



来源:https://stackoverflow.com/questions/18026516/how-to-set-artist-album-artist-year-album-song-number-and-title

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!