python
$ pip install mutagen
python
from mutagen.id3 import ID3, TIT2, TALB, TPE1, TRCK, APIC, TDRC, TCON
from mutagen.mp3 import MP3
import mutagen.id3
def sample(filename, album, artist, title, track, genre, date, description, savepath):
"""
Tag MP3 files
"""
m = MP3(filename, ID3=ID3)
try:
m.add_tags(ID3=ID3)
#print("Added tags to %s" % filename)
except mutagen.id3.error:
#print("%s already had tags" % filename)
pass
m["TPE1"] = TPE1(encoding=3, text=artist) #Artist
m["TIT2"] = TIT2(encoding=3, text=title) #title
m["TALB"] = TALB(encoding=3, text=album) #Album name
m['TCON'] = TCON(encoding=3, text=genre) #Genre
m['TDRC'] = TDRC(encoding=3, text=date) #Release year
m["TRCK"] = TRCK(encoding=3, text=[str(track + 1)]) #Track number
#Jacket image
if savepath:
m.tags.add(
APIC(
encoding=3, # 3 is for utf-8
mime='image/jpeg', # image/jpeg or image/png
type=3, # 3 is for the cover image
desc=u'Cover',
data=open(savepath).read()
)
)
m.save()
Recommended Posts