2020-02-17 15:46:18 +00:00
|
|
|
#!/usr/bin/env python3
|
2020-02-17 19:24:39 +00:00
|
|
|
from mutagen.flac import FLAC, Picture
|
|
|
|
from mutagen.id3 import ID3, ID3NoHeaderError, TXXX, TIT2, TPE1, TALB, TPE2, TRCK, TPOS, TCON, TYER, TDAT, TLEN, TBPM, \
|
|
|
|
TPUB, TSRC, USLT, APIC, IPLS, TCOM, TCOP
|
|
|
|
|
|
|
|
|
2020-03-01 10:52:29 +00:00
|
|
|
def tagID3(stream, track, save, id3v1=False, nullSeparator=True):
|
2020-02-17 15:46:18 +00:00
|
|
|
try:
|
|
|
|
tag = ID3(stream)
|
|
|
|
except ID3NoHeaderError:
|
|
|
|
tag = ID3()
|
|
|
|
|
2020-02-20 15:05:09 +00:00
|
|
|
if save['title']:
|
|
|
|
tag.add(TIT2(text=track['title']))
|
|
|
|
if save['artist']:
|
|
|
|
tag.add(TPE1(text=track['artists']))
|
|
|
|
if save['album']:
|
|
|
|
tag.add(TALB(text=track['album']['title']))
|
|
|
|
if save['albumArtist']:
|
2020-03-20 14:58:25 +00:00
|
|
|
tag.add(TPE2(text=track['album']['artists']))
|
2020-02-20 15:05:09 +00:00
|
|
|
if save['trackNumber']:
|
2020-02-22 14:57:24 +00:00
|
|
|
tag.add(TRCK(text=str(track['trackNumber'])+("/"+str(track['album']['trackTotal']) if save['trackTotal'] else "")))
|
2020-02-20 15:05:09 +00:00
|
|
|
if save['discNumber']:
|
2020-02-22 14:57:24 +00:00
|
|
|
tag.add(TPOS(text=str(track['discNumber'])+("/"+str(track['album']['discTotal']) if save['discTotal'] else "")))
|
2020-02-20 15:05:09 +00:00
|
|
|
if save['genre']:
|
|
|
|
tag.add(TCON(text=track['album']['genre']))
|
|
|
|
if save['year']:
|
|
|
|
tag.add(TYER(text=str(track['date']['year'])))
|
|
|
|
if save['date']:
|
|
|
|
tag.add(TDAT(text=str(track['date']['month']) + str(track['date']['day'])))
|
|
|
|
if save['length']:
|
|
|
|
tag.add(TLEN(text=str(track['duration'])))
|
|
|
|
if save['bpm']:
|
|
|
|
tag.add(TBPM(text=str(track['bpm'])))
|
|
|
|
if save['label']:
|
|
|
|
tag.add(TPUB(text=track['album']['label']))
|
|
|
|
if save['isrc']:
|
|
|
|
tag.add(TSRC(text=track['ISRC']))
|
|
|
|
if save['barcode']:
|
|
|
|
tag.add(TXXX(desc="BARCODE", text=track['album']['barcode']))
|
2020-02-17 15:46:18 +00:00
|
|
|
tag.add(TXXX(desc="ITUNESADVISORY", text="1" if track['explicit'] else "0"))
|
2020-02-20 15:05:09 +00:00
|
|
|
if save['replayGain']:
|
|
|
|
tag.add(TXXX(desc="REPLAYGAIN_TRACK_GAIN", text=track['replayGain']))
|
|
|
|
if 'unsync' in track['lyrics'] and save['lyrics']:
|
2020-02-17 15:46:18 +00:00
|
|
|
tag.add(USLT(text=track['lyrics']['unsync']))
|
2020-02-17 19:24:39 +00:00
|
|
|
involved_people = []
|
2020-02-17 15:46:18 +00:00
|
|
|
for role in track['contributors']:
|
|
|
|
if role in ['author', 'engineer', 'mixer', 'producer', 'writer']:
|
|
|
|
for person in track['contributors'][role]:
|
2020-02-17 19:24:39 +00:00
|
|
|
involved_people.append([role, person])
|
2020-02-20 15:05:09 +00:00
|
|
|
elif role == 'composer' and save['composer']:
|
2020-02-17 15:46:18 +00:00
|
|
|
tag.add(TCOM(text=track['contributors']['composer']))
|
2020-02-20 15:05:09 +00:00
|
|
|
if len(involved_people) > 0 and save['involvedPeople']:
|
2020-02-17 19:24:39 +00:00
|
|
|
tag.add(IPLS(people=involved_people))
|
2020-02-20 15:05:09 +00:00
|
|
|
if save['copyright']:
|
|
|
|
tag.add(TCOP(text=track['copyright']))
|
2020-02-27 17:37:40 +00:00
|
|
|
if save['cover'] and track['album']['picPath']:
|
|
|
|
with open(track['album']['picPath'], 'rb') as f:
|
2020-02-27 17:46:32 +00:00
|
|
|
tag.add(APIC(3, 'image/jpeg' if track['album']['picPath'].endswith('jpg') else 'image/png', 3, data=f.read()))
|
2020-02-17 15:46:18 +00:00
|
|
|
|
2020-03-01 10:52:29 +00:00
|
|
|
tag.save(stream, v1=2 if id3v1 else 0, v2_version=3, v23_sep=None if nullSeparator else '/')
|
2020-02-17 15:46:18 +00:00
|
|
|
|
2020-02-17 19:24:39 +00:00
|
|
|
|
2020-02-20 15:05:09 +00:00
|
|
|
def tagFLAC(stream, track, save):
|
2020-02-17 15:46:18 +00:00
|
|
|
tag = FLAC(stream)
|
|
|
|
|
2020-02-20 15:05:09 +00:00
|
|
|
if save['title']:
|
|
|
|
tag["TITLE"] = track['title']
|
|
|
|
if save['artist']:
|
|
|
|
tag["ARTIST"] = track['artists']
|
|
|
|
if save['album']:
|
|
|
|
tag["ALBUM"] = track['album']['title']
|
|
|
|
if save['albumArtist']:
|
2020-03-20 14:58:25 +00:00
|
|
|
tag["ALBUMARTIST"] = track['album']['artists']
|
2020-02-20 15:05:09 +00:00
|
|
|
if save['trackNumber']:
|
|
|
|
tag["TRACKNUMBER"] = str(track['trackNumber'])
|
|
|
|
if save['trackTotal']:
|
|
|
|
tag["TRACKTOTAL"] = str(track['album']['trackTotal'])
|
|
|
|
if save['discNumber']:
|
|
|
|
tag["DISCNUMBER"] = str(track['discNumber'])
|
|
|
|
if save['discTotal']:
|
|
|
|
tag["DISCTOTAL"] = str(track['album']['discTotal'])
|
|
|
|
if save['genre']:
|
|
|
|
tag["GENRE"] = track['album']['genre']
|
|
|
|
if save['year']:
|
|
|
|
tag["YEAR"] = str(track['date']['year'])
|
|
|
|
if save['date']:
|
2020-03-20 15:27:58 +00:00
|
|
|
tag["DATE"] = track['dateString']
|
2020-02-20 15:05:09 +00:00
|
|
|
if save['length']:
|
|
|
|
tag["LENGTH"] = str(track['duration'])
|
|
|
|
if save['bpm']:
|
|
|
|
tag["BPM"] = str(track['bpm'])
|
|
|
|
if save['label']:
|
|
|
|
tag["PUBLISHER"] = track['album']['label']
|
|
|
|
if save['isrc']:
|
|
|
|
tag["ISRC"] = track['ISRC']
|
|
|
|
if save['barcode']:
|
|
|
|
tag["BARCODE"] = track['album']['barcode']
|
|
|
|
if save['explicit']:
|
|
|
|
tag["ITUNESADVISORY"] = "1" if track['explicit'] else "0"
|
|
|
|
if save['replayGain']:
|
|
|
|
tag["REPLAYGAIN_TRACK_GAIN"] = track['replayGain']
|
|
|
|
if 'unsync' in track['lyrics'] and save['lyrics']:
|
2020-02-17 15:46:18 +00:00
|
|
|
tag["LYRICS"] = track['lyrics']['unsync']
|
|
|
|
for role in track['contributors']:
|
|
|
|
if role in ['author', 'engineer', 'mixer', 'producer', 'writer', 'composer']:
|
2020-02-20 15:05:09 +00:00
|
|
|
if save['involvedPeople'] and role != 'composer' or role == 'composer' and save['composer']:
|
|
|
|
tag[role] = track['contributors'][role]
|
|
|
|
elif role == 'musicpublisher' and save['involvedPeople']:
|
2020-02-17 15:46:18 +00:00
|
|
|
tag["ORGANIZATION"] = track['contributors']['musicpublisher']
|
2020-02-20 15:05:09 +00:00
|
|
|
if save['copyright']:
|
|
|
|
tag["COPYRIGHT"] = track['copyright']
|
2020-02-17 15:46:18 +00:00
|
|
|
|
2020-02-27 17:37:40 +00:00
|
|
|
if save['cover'] and track['album']['picPath']:
|
2020-02-20 15:05:09 +00:00
|
|
|
image = Picture()
|
|
|
|
image.type = 3
|
2020-02-27 17:46:32 +00:00
|
|
|
image.mime = 'image/jpeg' if track['album']['picPath'].endswith('jpg') else 'image/png'
|
2020-02-27 17:37:40 +00:00
|
|
|
with open(track['album']['picPath'], 'rb') as f:
|
|
|
|
image.data = f.read()
|
2020-02-20 15:05:09 +00:00
|
|
|
tag.add_picture(image)
|
2020-02-17 15:46:18 +00:00
|
|
|
|
|
|
|
tag.save(deleteid3=True)
|