18

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

Elijah
  • 1,814
  • 21
  • 27
YGA
  • 9,546
  • 15
  • 47
  • 50
  • 2
    Is there some problem with the Mutagen tutorial? http://code.google.com/p/mutagen/wiki/Tutorial – pafcu Oct 28 '10 at 07:16

3 Answers3

26

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, TRCK

# Read the ID3 tag or create one 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)

See also:

ccpizza
  • 28,968
  • 18
  • 162
  • 169
  • I wonder if you are supposed to close file objects like `ID3`. – Roman Shapovalov Jun 28 '20 at 08:50
  • 1
    The [docs](https://mutagen.readthedocs.io/en/latest/user/id3.html) don't mention anything resembling a `close()` method therefore I'd assume `save()` is all you need. You can look at the source if you need more details: https://github.com/quodlibet/mutagen – ccpizza Jun 28 '20 at 14:02
  • The code uses complicated logic for maintaining the context; hard to say from the code whether it stores a file name or an open file descriptor: https://github.com/quodlibet/mutagen/blob/6298fa63e619f3d31dcc79c72898489f57ad2b3e/mutagen/_util.py#L207 I would hope for the former, i.e. the file is opened every time on load/save. I also edited thousands of files in a loop without a crash, so that may be fine (although I don’t know what the limit is for file descriptors per process in my system). – Roman Shapovalov Jun 28 '20 at 22:37
  • I'm assuming the package is following the least surprise principle. If you run into a bottleneck or a limitation then consider filling an issue on github so that the author can respond. – ccpizza Jun 29 '20 at 07:40
6

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

[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

Embedding lyrics using mutagen

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())
Community
  • 1
  • 1
pyfunc
  • 65,343
  • 15
  • 148
  • 136
6

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)
MagTun
  • 5,619
  • 5
  • 63
  • 104