Does anyone have good examples of using mutagen to write to files?

混江龙づ霸主 提交于 2019-12-03 09:08:13

问题


Just as the title asks — does anyone have a good example of using the Mutagen Python ID3 library to write to .mp3 files?

I'm looking, in particular, to add disc/track number information, but examples editing the title and artist would be helpful as well.

Cheers,
/YGA


回答1:


Taken from a script I made a while ago for embedding lyrics into MP3 files:

http://code.activestate.com/recipes/577138-embed-lyrics-into-mp3-files-using-mutagen-uslt-tag/

The relevant part is:

from mutagen.id3 import ID3NoHeaderError
from mutagen.id3 import ID3, TIT2, TALB, TPE1, TPE2, COMM, TCOM, TCON, TDRC

# Read ID3 tag or create it if not present
try: 
    tags = ID3(fname)
except ID3NoHeaderError:
    print("Adding ID3 header")
    tags = ID3()

tags["TIT2"] = TIT2(encoding=3, text=title)
tags["TALB"] = TALB(encoding=3, text=u'mutagen Album Name')
tags["TPE2"] = TPE2(encoding=3, text=u'mutagen Band')
tags["COMM"] = COMM(encoding=3, lang=u'eng', desc='desc', text=u'mutagen comment')
tags["TPE1"] = TPE1(encoding=3, text=u'mutagen Artist')
tags["TCOM"] = TCOM(encoding=3, text=u'mutagen Composer')
tags["TCON"] = TCON(encoding=3, text=u'mutagen Genre')
tags["TDRC"] = TDRC(encoding=3, text=u'2010')
tags["TRCK"] = TRCK(encoding=3, text=u'track_number')

tags.save(fname)



回答2:


Did you check out the examples on the web. Some of these should help you.

  • http://www.blog.pythonlibrary.org/2010/04/22/parsing-id3-tags-from-mp3s-using-python/

[Edit:]

Mutagen tutorial is pretty good, hence did not add more information. dir() provides most of the details.

For setting album cover to mp3 using mutagen

  • How do you embed album art into an MP3 using Python?

Embedding lyrics using mutagen

  • http://code.activestate.com/recipes/577138-embed-lyrics-into-mp3-files-using-mutagen-uslt-fra/

An example

from mutagen.mp3 import MP3
from mutagen.easyid3 import EasyID3
import mutagen.id3

filename = 'xxx.mp3'

# Example which shows how to automatically add tags to an MP3 using EasyID3

mp3file = MP3(filename, ID3=EasyID3)

try:
    mp3file.add_tags(ID3=EasyID3)
except mutagen.id3.error:
    print("has tags")

mp3file['title'] = 'Newly tagged'
mp3file.save()
print(mp3file.pprint())



回答3:


An easy way to do it:

from mutagen.easyid3 import EasyID3
audio = EasyID3(mp3_filename_import)
audio['title'] = "Title"
audio['artist'] = "Artist"
audio['album'] = "Album"
audio['composer'] = "" # empty
audio.save()

If the tags don't appear, then change the last line to:

audio.save(v2_version=3)


来源:https://stackoverflow.com/questions/4040605/does-anyone-have-good-examples-of-using-mutagen-to-write-to-files

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