26 lines
768 B
Python
26 lines
768 B
Python
import mutagen.id3
|
|
from mutagen.easyid3 import EasyID3
|
|
|
|
|
|
def get_tags(filepath):
|
|
try:
|
|
audio = EasyID3(filepath)
|
|
return audio["artist"][0] if audio["artist"] else None, audio["title"][0] if audio["title"] else None
|
|
except mutagen.id3.ID3NoHeaderError:
|
|
return None, None
|
|
|
|
|
|
def tag_file(filepath, artist, title):
|
|
try:
|
|
id3 = mutagen.id3.ID3(filepath)
|
|
except mutagen.id3.ID3NoHeaderError:
|
|
mutafile = mutagen.File(filepath)
|
|
mutafile.add_tags()
|
|
mutafile.save()
|
|
id3 = mutagen.id3.ID3(filepath)
|
|
id3.add(mutagen.id3.TIT2(encoding=3, text=title))
|
|
if artist:
|
|
id3.add(mutagen.id3.TOPE(encoding=3, text=artist))
|
|
id3.add(mutagen.id3.TPE1(encoding=3, text=artist))
|
|
id3.save()
|