2020-02-17 19:24:39 +00:00
|
|
|
from mutagen.flac import FLAC, Picture
|
2020-09-24 15:46:08 +00:00
|
|
|
from mutagen.id3 import ID3, ID3NoHeaderError, \
|
|
|
|
TXXX, TIT2, TPE1, TALB, TPE2, TRCK, TPOS, TCON, TYER, TDAT, TLEN, TBPM, \
|
|
|
|
TPUB, TSRC, USLT, SYLT, APIC, IPLS, TCOM, TCOP, TCMP, Encoding, PictureType
|
2020-02-17 19:24:39 +00:00
|
|
|
|
2020-08-14 18:31:37 +00:00
|
|
|
# Adds tags to a MP3 file
|
2020-03-30 09:59:18 +00:00
|
|
|
def tagID3(stream, track, save):
|
2020-08-14 18:31:37 +00:00
|
|
|
# Delete exsisting tags
|
2020-04-17 10:31:47 +00:00
|
|
|
try:
|
|
|
|
tag = ID3(stream)
|
2020-05-26 09:31:20 +00:00
|
|
|
tag.delete()
|
2020-04-17 10:31:47 +00:00
|
|
|
except ID3NoHeaderError:
|
|
|
|
tag = ID3()
|
2020-02-17 15:46:18 +00:00
|
|
|
|
2020-04-17 10:31:47 +00:00
|
|
|
if save['title']:
|
2020-08-15 19:34:10 +00:00
|
|
|
tag.add(TIT2(text=track.title))
|
|
|
|
|
|
|
|
if save['artist'] and len(track.artists):
|
2020-09-24 15:46:08 +00:00
|
|
|
if save['multiArtistSeparator'] == "default":
|
|
|
|
tag.add(TPE1(text=track.artists))
|
|
|
|
else:
|
2020-07-18 15:28:51 +00:00
|
|
|
if save['multiArtistSeparator'] == "nothing":
|
2020-08-15 19:34:10 +00:00
|
|
|
tag.add(TPE1(text=track.mainArtist['name']))
|
2020-07-18 15:28:51 +00:00
|
|
|
else:
|
2020-08-15 19:34:10 +00:00
|
|
|
tag.add(TPE1(text=track.artistsString))
|
2020-09-25 16:21:38 +00:00
|
|
|
# Tag ARTISTS is added to keep the multiartist support when using a non standard tagging method
|
|
|
|
# https://picard-docs.musicbrainz.org/en/technical/tag_mapping.html#artists
|
2020-08-15 19:34:10 +00:00
|
|
|
tag.add(TXXX(desc="ARTISTS", text=track.artists))
|
2020-08-14 18:31:37 +00:00
|
|
|
|
2020-04-17 10:31:47 +00:00
|
|
|
if save['album']:
|
2020-08-15 19:34:10 +00:00
|
|
|
tag.add(TALB(text=track.album['title']))
|
2020-08-14 18:31:37 +00:00
|
|
|
|
2020-08-15 19:34:10 +00:00
|
|
|
if save['albumArtist'] and len(track.album['artists']):
|
|
|
|
if save['singleAlbumArtist'] and track.album['mainArtist']['save']:
|
|
|
|
tag.add(TPE2(text=track.album['mainArtist']['name']))
|
2020-07-18 15:28:51 +00:00
|
|
|
else:
|
2020-08-15 19:34:10 +00:00
|
|
|
tag.add(TPE2(text=track.album['artists']))
|
2020-08-14 18:31:37 +00:00
|
|
|
|
2020-04-17 10:31:47 +00:00
|
|
|
if save['trackNumber']:
|
2020-09-24 15:46:08 +00:00
|
|
|
trackNumber = str(track.trackNumber)
|
|
|
|
if save['trackTotal']:
|
|
|
|
trackNumber += "/" + str(track.album['trackTotal'])
|
|
|
|
tag.add(TRCK(text=trackNumber))
|
2020-04-17 10:31:47 +00:00
|
|
|
if save['discNumber']:
|
2020-09-24 15:46:08 +00:00
|
|
|
discNumber = str(track.discNumber)
|
|
|
|
if save['discTotal']:
|
|
|
|
discNumber += "/" + str(track.album['discTotal'])
|
|
|
|
tag.add(TPOS(text=discNumber))
|
|
|
|
|
2020-04-17 10:31:47 +00:00
|
|
|
if save['genre']:
|
2020-08-15 19:34:10 +00:00
|
|
|
tag.add(TCON(text=track.album['genre']))
|
2020-04-17 10:31:47 +00:00
|
|
|
if save['year']:
|
2020-08-15 19:34:10 +00:00
|
|
|
tag.add(TYER(text=str(track.date['year'])))
|
2020-04-17 10:31:47 +00:00
|
|
|
if save['date']:
|
2020-09-24 15:46:08 +00:00
|
|
|
# Referencing ID3 standard
|
|
|
|
# https://id3.org/id3v2.3.0#TDAT
|
|
|
|
# The 'Date' frame is a numeric string in the DDMM format.
|
2020-09-19 08:27:58 +00:00
|
|
|
tag.add(TDAT(text=str(track.date['day']) + str(track.date['month'])))
|
2020-04-17 10:31:47 +00:00
|
|
|
if save['length']:
|
2020-08-15 19:34:10 +00:00
|
|
|
tag.add(TLEN(text=str(int(track.duration)*1000)))
|
2020-04-17 10:31:47 +00:00
|
|
|
if save['bpm']:
|
2020-08-15 19:34:10 +00:00
|
|
|
tag.add(TBPM(text=str(track.bpm)))
|
2020-04-17 10:31:47 +00:00
|
|
|
if save['label']:
|
2020-08-15 19:34:10 +00:00
|
|
|
tag.add(TPUB(text=track.album['label']))
|
2020-04-17 10:31:47 +00:00
|
|
|
if save['isrc']:
|
2020-08-15 19:34:10 +00:00
|
|
|
tag.add(TSRC(text=track.ISRC))
|
2020-04-17 10:31:47 +00:00
|
|
|
if save['barcode']:
|
2020-08-15 19:34:10 +00:00
|
|
|
tag.add(TXXX(desc="BARCODE", text=track.album['barcode']))
|
2020-04-17 10:31:47 +00:00
|
|
|
if save['explicit']:
|
2020-09-24 15:46:08 +00:00
|
|
|
tag.add(TXXX(desc="ITUNESADVISORY", text= "1" if track.explicit else "0" ))
|
2020-04-17 10:31:47 +00:00
|
|
|
if save['replayGain']:
|
2020-08-15 19:34:10 +00:00
|
|
|
tag.add(TXXX(desc="REPLAYGAIN_TRACK_GAIN", text=track.replayGain))
|
2020-08-16 11:25:25 +00:00
|
|
|
if track.lyrics['unsync'] and save['lyrics']:
|
2020-08-15 19:34:10 +00:00
|
|
|
tag.add(USLT(text=track.lyrics['unsync']))
|
2020-09-10 11:44:38 +00:00
|
|
|
if track.lyrics['syncID3'] and save['syncedLyrics']:
|
2020-09-24 15:46:08 +00:00
|
|
|
# Referencing ID3 standard
|
|
|
|
# https://id3.org/id3v2.3.0#sec4.10
|
|
|
|
# Type: 1 => is lyrics
|
|
|
|
# Format: 2 => Absolute time, 32 bit sized, using milliseconds as unit
|
|
|
|
tag.add(SYLT(Encoding.UTF8, type=1, format=2, text=track.lyrics['syncID3']))
|
2020-08-14 18:31:37 +00:00
|
|
|
|
2020-04-17 10:31:47 +00:00
|
|
|
involved_people = []
|
2020-08-15 19:34:10 +00:00
|
|
|
for role in track.contributors:
|
2020-04-17 10:31:47 +00:00
|
|
|
if role in ['author', 'engineer', 'mixer', 'producer', 'writer']:
|
2020-08-15 19:34:10 +00:00
|
|
|
for person in track.contributors[role]:
|
2020-04-17 10:31:47 +00:00
|
|
|
involved_people.append([role, person])
|
|
|
|
elif role == 'composer' and save['composer']:
|
2020-08-15 19:34:10 +00:00
|
|
|
tag.add(TCOM(text=track.contributors['composer']))
|
2020-04-17 10:31:47 +00:00
|
|
|
if len(involved_people) > 0 and save['involvedPeople']:
|
|
|
|
tag.add(IPLS(people=involved_people))
|
2020-08-14 18:31:37 +00:00
|
|
|
|
2020-04-17 10:31:47 +00:00
|
|
|
if save['copyright']:
|
2020-08-15 19:34:10 +00:00
|
|
|
tag.add(TCOP(text=track.copyright))
|
2020-09-20 08:50:09 +00:00
|
|
|
if save['savePlaylistAsCompilation'] and track.playlist or track.album['recordType'] == "compile":
|
2020-04-17 10:31:47 +00:00
|
|
|
tag.add(TCMP(text="1"))
|
2020-08-14 18:31:37 +00:00
|
|
|
|
2020-08-15 19:34:10 +00:00
|
|
|
if save['cover'] and track.album['picPath']:
|
2020-09-24 15:46:08 +00:00
|
|
|
|
|
|
|
descEncoding = Encoding.LATIN1
|
|
|
|
if save['coverDescriptionUTF8']:
|
|
|
|
descEncoding = Encoding.UTF8
|
|
|
|
|
|
|
|
mimeType = 'image/jpeg'
|
|
|
|
if track.album['picPath'].endswith('png'):
|
|
|
|
mimeType = 'image/png'
|
|
|
|
|
2020-08-15 19:34:10 +00:00
|
|
|
with open(track.album['picPath'], 'rb') as f:
|
2020-09-24 15:46:08 +00:00
|
|
|
tag.add(APIC(descEncoding, mimeType, PictureType.COVER_FRONT, desc='cover', data=f.read()))
|
2020-02-17 15:46:18 +00:00
|
|
|
|
2020-09-24 15:46:08 +00:00
|
|
|
tag.save( stream,
|
|
|
|
v1=2 if save['saveID3v1'] else 0,
|
|
|
|
v2_version=3,
|
|
|
|
v23_sep=None if save['useNullSeparator'] else '/' )
|
2020-02-17 15:46:18 +00:00
|
|
|
|
2020-08-14 18:31:37 +00:00
|
|
|
# Adds tags to a FLAC file
|
2020-02-20 15:05:09 +00:00
|
|
|
def tagFLAC(stream, track, save):
|
2020-08-14 18:31:37 +00:00
|
|
|
# Delete exsisting tags
|
2020-04-17 10:31:47 +00:00
|
|
|
tag = FLAC(stream)
|
2020-05-26 09:31:20 +00:00
|
|
|
tag.delete()
|
|
|
|
tag.clear_pictures()
|
2020-08-14 18:31:37 +00:00
|
|
|
|
2020-04-17 10:31:47 +00:00
|
|
|
if save['title']:
|
2020-08-15 19:34:10 +00:00
|
|
|
tag["TITLE"] = track.title
|
2020-08-14 18:31:37 +00:00
|
|
|
|
2020-08-15 19:34:10 +00:00
|
|
|
if save['artist'] and len(track.artists):
|
2020-09-24 15:46:08 +00:00
|
|
|
if save['multiArtistSeparator'] == "default":
|
|
|
|
tag["ARTIST"] = track.artists
|
|
|
|
else:
|
2020-07-18 15:28:51 +00:00
|
|
|
if save['multiArtistSeparator'] == "nothing":
|
2020-08-15 19:34:10 +00:00
|
|
|
tag["ARTIST"] = track.mainArtist['name']
|
2020-07-18 15:28:51 +00:00
|
|
|
else:
|
2020-08-15 19:34:10 +00:00
|
|
|
tag["ARTIST"] = track.artistsString
|
2020-09-25 16:21:38 +00:00
|
|
|
# Tag ARTISTS is added to keep the multiartist support when using a non standard tagging method
|
|
|
|
# https://picard-docs.musicbrainz.org/en/technical/tag_mapping.html#artists
|
2020-08-15 19:34:10 +00:00
|
|
|
tag["ARTISTS"] = track.artists
|
2020-08-14 18:31:37 +00:00
|
|
|
|
2020-04-17 10:31:47 +00:00
|
|
|
if save['album']:
|
2020-08-15 19:34:10 +00:00
|
|
|
tag["ALBUM"] = track.album['title']
|
2020-08-14 18:31:37 +00:00
|
|
|
|
2020-08-15 19:34:10 +00:00
|
|
|
if save['albumArtist'] and len(track.album['artists']):
|
2020-09-24 15:46:08 +00:00
|
|
|
if save['singleAlbumArtist'] and track.album['mainArtist']['save']:
|
2020-08-15 19:34:10 +00:00
|
|
|
tag["ALBUMARTIST"] = track.album['mainArtist']['name']
|
2020-07-18 15:28:51 +00:00
|
|
|
else:
|
2020-08-15 19:34:10 +00:00
|
|
|
tag["ALBUMARTIST"] = track.album['artists']
|
2020-08-14 18:31:37 +00:00
|
|
|
|
2020-04-17 10:31:47 +00:00
|
|
|
if save['trackNumber']:
|
2020-08-15 19:34:10 +00:00
|
|
|
tag["TRACKNUMBER"] = str(track.trackNumber)
|
2020-04-17 10:31:47 +00:00
|
|
|
if save['trackTotal']:
|
2020-08-15 19:34:10 +00:00
|
|
|
tag["TRACKTOTAL"] = str(track.album['trackTotal'])
|
2020-04-17 10:31:47 +00:00
|
|
|
if save['discNumber']:
|
2020-08-15 19:34:10 +00:00
|
|
|
tag["DISCNUMBER"] = str(track.discNumber)
|
2020-04-17 10:31:47 +00:00
|
|
|
if save['discTotal']:
|
2020-08-15 19:34:10 +00:00
|
|
|
tag["DISCTOTAL"] = str(track.album['discTotal'])
|
2020-04-17 10:31:47 +00:00
|
|
|
if save['genre']:
|
2020-08-15 19:34:10 +00:00
|
|
|
tag["GENRE"] = track.album['genre']
|
2020-09-24 15:46:08 +00:00
|
|
|
|
|
|
|
# YEAR tag is not suggested as a standard tag
|
|
|
|
# Being YEAR already contained in DATE will only use DATE instead
|
|
|
|
# Reference: https://www.xiph.org/vorbis/doc/v-comment.html#fieldnames
|
2020-04-17 10:31:47 +00:00
|
|
|
if save['date']:
|
2020-08-15 19:34:10 +00:00
|
|
|
tag["DATE"] = track.dateString
|
2020-05-05 13:10:48 +00:00
|
|
|
elif save['year']:
|
2020-09-19 15:43:29 +00:00
|
|
|
tag["DATE"] = str(track.date['year'])
|
2020-09-24 15:46:08 +00:00
|
|
|
|
2020-04-17 10:31:47 +00:00
|
|
|
if save['length']:
|
2020-09-24 15:46:08 +00:00
|
|
|
tag["LENGTH"] = str(int(track.duration)*1000)
|
2020-04-17 10:31:47 +00:00
|
|
|
if save['bpm']:
|
2020-08-15 19:34:10 +00:00
|
|
|
tag["BPM"] = str(track.bpm)
|
2020-04-17 10:31:47 +00:00
|
|
|
if save['label']:
|
2020-08-15 19:34:10 +00:00
|
|
|
tag["PUBLISHER"] = track.album['label']
|
2020-04-17 10:31:47 +00:00
|
|
|
if save['isrc']:
|
2020-08-15 19:34:10 +00:00
|
|
|
tag["ISRC"] = track.ISRC
|
2020-04-17 10:31:47 +00:00
|
|
|
if save['barcode']:
|
2020-08-15 19:34:10 +00:00
|
|
|
tag["BARCODE"] = track.album['barcode']
|
2020-04-17 10:31:47 +00:00
|
|
|
if save['explicit']:
|
2020-08-15 19:34:10 +00:00
|
|
|
tag["ITUNESADVISORY"] = "1" if track.explicit else "0"
|
2020-04-17 10:31:47 +00:00
|
|
|
if save['replayGain']:
|
2020-08-15 19:34:10 +00:00
|
|
|
tag["REPLAYGAIN_TRACK_GAIN"] = track.replayGain
|
2020-08-16 11:25:25 +00:00
|
|
|
if track.lyrics['unsync'] and save['lyrics']:
|
2020-08-15 19:34:10 +00:00
|
|
|
tag["LYRICS"] = track.lyrics['unsync']
|
2020-08-14 18:31:37 +00:00
|
|
|
|
2020-08-15 19:34:10 +00:00
|
|
|
for role in track.contributors:
|
2020-04-17 10:31:47 +00:00
|
|
|
if role in ['author', 'engineer', 'mixer', 'producer', 'writer', 'composer']:
|
2020-09-24 15:46:08 +00:00
|
|
|
if save['involvedPeople'] and role != 'composer' or save['composer'] and role == 'composer':
|
2020-08-15 19:34:10 +00:00
|
|
|
tag[role] = track.contributors[role]
|
2020-04-17 10:31:47 +00:00
|
|
|
elif role == 'musicpublisher' and save['involvedPeople']:
|
2020-08-15 19:34:10 +00:00
|
|
|
tag["ORGANIZATION"] = track.contributors['musicpublisher']
|
2020-08-14 18:31:37 +00:00
|
|
|
|
2020-04-17 10:31:47 +00:00
|
|
|
if save['copyright']:
|
2020-08-15 19:34:10 +00:00
|
|
|
tag["COPYRIGHT"] = track.copyright
|
2020-09-20 08:50:09 +00:00
|
|
|
if save['savePlaylistAsCompilation'] and track.playlist or track.album['recordType'] == "compile":
|
2020-04-17 10:31:47 +00:00
|
|
|
tag["COMPILATION"] = "1"
|
2020-02-17 15:46:18 +00:00
|
|
|
|
2020-08-15 19:34:10 +00:00
|
|
|
if save['cover'] and track.album['picPath']:
|
2020-04-17 10:31:47 +00:00
|
|
|
image = Picture()
|
2020-09-24 15:46:08 +00:00
|
|
|
image.type = PictureType.COVER_FRONT
|
|
|
|
image.mime = 'image/jpeg'
|
|
|
|
if track.album['picPath'].endswith('png'):
|
|
|
|
image.mime = 'image/png'
|
2020-08-15 19:34:10 +00:00
|
|
|
with open(track.album['picPath'], 'rb') as f:
|
2020-04-17 10:31:47 +00:00
|
|
|
image.data = f.read()
|
|
|
|
tag.add_picture(image)
|
2020-02-17 15:46:18 +00:00
|
|
|
|
2020-04-17 10:31:47 +00:00
|
|
|
tag.save(deleteid3=True)
|