Python - Add ID3 tags to mp3 file that has NO tags
Asked Answered
W

2

14

I get a lot of podcasts that have no ID3 tags in them. I've tried a number of tools that I could use to loop through directories and add title and artist info to the ID3 tags, but they fail. I've tried ID3, eyed3 and mutagen. Most of the time if a file has no ID3 tag these modules fail.

Can someone recommend a good ID3 tag editor library that will work through loops? What else do I need to know about editing/adding ID3 tags when they're 100% blank? It's getting frustrating trying library after library only to find that the problem remains.

Thank you.

Weisman answered 21/8, 2013 at 23:18 Comment(0)
H
31

Mutagen handles this just fine:

>>> import mutagen
>>> from mutagen.easyid3 import EasyID3
>>> filePath = "8049.mp3"

>>> try:
>>>    meta = EasyID3(filePath)
>>> except mutagen.id3.ID3NoHeaderError:
>>>    meta = mutagen.File(filePath, easy=True)
>>>    meta.add_tags()
>>> meta
{}
>>> type(meta)
<class 'mutagen.easyid3.EasyID3'>
>>> meta['title'] = "This is a title"
>>> meta['artist'] = "Artist Name"
>>> meta['genre'] = "Space Funk"
>>> meta.save(filePath, v1=2)
>>> changed = EasyID3("8049.mp3")
>>> changed
{'genre': [u'Space Funk'], 'title': [u'This is a title'], 'artist': [u'Artist Name']}
Heer answered 22/8, 2013 at 0:4 Comment(7)
I'll try that; when I put them into a loop it didn't work the first time.Weisman
Sorry, I'M hitting return and it's posting the comments When I did the first line, meta = EasyID3(file) it says: mutagen.id3.ID3NoHeaderError: 'musicfile.mp3' doesn't start with an ID3 tagWeisman
Yeah, I needed to read the FAQ. Keep in mind I have been working on this project for about 3 days straight so the reading level goes down as the frustration goes up. Okay, I can now read/create ID3 tags using mutagen. There's now just one problem; the ID3 tags are invisible in Windows Explorer. I saw that I can use v1=2 or something when I save, but they're still invisible. Any clues?Weisman
The code above doesn't work. When I put in the try/exception statement, I get this: Traceback (most recent call last): File "<stdin>", line 3, in <module> NameError: name 'mutagen' is not defined And yes, I DID do the import line.Weisman
Sorry, I just re-did it using "from mutagen import *"Weisman
Thank you so much. I just got it all to work. I changed meta.save() to meta.save(filepath, v1=2) and got the exact result I needed. Thank you so much. The Mutagen documentation isn't that great so your help here is greatly appreciated. If you were in Tokyo, I'd buy you a beer!Weisman
I needed to add "import mutagen" to get the exception to work.Karynkaryo
F
2
import mutagen.id3
import mutagen.mp3

M = mutagen.mp3.MP3(myfilepath)

if M.tags is None:
    M.tags = mutagen.id3.ID3()  # also sets the filepath for the ID3 instance

M.tags['TIT2'] = mutagen.id3.TIT2(encoding=1, text=['mytitle'])

M.save(v1=0, v2_version=3)  # save ID3v2.3 only without ID3v1 (default is ID3v2.4)
Flatling answered 29/12, 2021 at 10:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.