Does anyone have good examples of using mutagen to write to files? [closed]
Asked Answered
L

3

19

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

Lactone answered 28/10, 2010 at 7:13 Comment(1)
Is there some problem with the Mutagen tutorial? code.google.com/p/mutagen/wiki/TutorialEuler
O
27

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:

Omar answered 26/12, 2012 at 12:2 Comment(5)
I wonder if you are supposed to close file objects like ID3.Ludwigshafen
The docs 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: github.com/quodlibet/mutagenOmar
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: github.com/quodlibet/mutagen/blob/… 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).Ludwigshafen
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.Omar
note: for tags.save(fname) the fname file must exist, otherwise you get mutagen.MutagenError: [Errno 2] No such file or directory, see also Saving to another file, than reading from and Atomic savingPontificals
M
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())
Macegan answered 28/10, 2010 at 7:25 Comment(0)
T
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)
Tallbott answered 18/1, 2017 at 17:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.