2020-08-10 04:47:51 +02:00
|
|
|
from mutagen.flac import FLAC
|
2020-08-13 06:43:52 +02:00
|
|
|
from mutagen.mp3 import EasyMP3
|
2020-08-10 04:47:51 +02:00
|
|
|
from pathvalidate import sanitize_filename
|
|
|
|
import os
|
|
|
|
|
|
|
|
|
2020-08-13 06:43:52 +02:00
|
|
|
def tag_flac(file, path, d, album, istrack=True):
|
2020-08-10 04:47:51 +02:00
|
|
|
audio = FLAC(file)
|
|
|
|
|
|
|
|
audio['TITLE'] = d['title'] # TRACK TITLE
|
|
|
|
audio['TRACKNUMBER'] = str(d['track_number']) # TRACK NUMBER
|
|
|
|
try:
|
|
|
|
audio['COMPOSER'] = d['composer']['name'] # COMPOSER
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
try:
|
|
|
|
audio['ARTIST'] = d['performer']['name'] # TRACK ARTIST
|
|
|
|
except KeyError:
|
|
|
|
if istrack:
|
|
|
|
audio['ARTIST'] = d['album']['artist']['name'] # TRACK ARTIST
|
|
|
|
else:
|
|
|
|
audio['ARTIST'] = album['artist']['name']
|
|
|
|
|
|
|
|
if istrack:
|
|
|
|
audio['GENRE'] = ', '.join(d['album']['genres_list']) # GENRE
|
|
|
|
audio['ALBUMARTIST'] = d['album']['artist']['name'] # ALBUM ARTIST
|
|
|
|
audio['TRACKTOTAL'] = str(d['album']['tracks_count']) # TRACK TOTAL
|
|
|
|
audio['ALBUM'] = d['album']['title'] # ALBUM TITLE
|
2020-08-13 06:43:52 +02:00
|
|
|
audio['YEAR'] = d['album']['release_date_original'].split('-')[0]
|
2020-08-10 04:47:51 +02:00
|
|
|
else:
|
|
|
|
audio['GENRE'] = ', '.join(album['genres_list']) # GENRE
|
|
|
|
audio['ALBUMARTIST'] = album['artist']['name'] # ALBUM ARTIST
|
|
|
|
audio['TRACKTOTAL'] = str(album['tracks_count']) # TRACK TOTAL
|
|
|
|
audio['ALBUM'] = album['title'] # ALBUM TITLE
|
|
|
|
audio['YEAR'] = album['release_date_original'].split('-')[0] # YEAR
|
|
|
|
|
|
|
|
audio.save()
|
|
|
|
title = sanitize_filename(d['title'])
|
|
|
|
os.rename(file, '{}/{:02}. {}.flac'.format(path, d['track_number'], title))
|
2020-08-13 06:43:52 +02:00
|
|
|
|
|
|
|
|
|
|
|
def tag_mp3(file, path, d, album, istrack=True):
|
|
|
|
audio = EasyMP3(file)
|
|
|
|
|
|
|
|
audio['title'] = d['title']
|
|
|
|
audio['tracknumber'] = str(d['track_number'])
|
|
|
|
try:
|
|
|
|
audio['composer'] = d['composer']['name']
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
audio['artist'] = d['performer']['name'] # TRACK ARTIST
|
|
|
|
except KeyError:
|
|
|
|
if istrack:
|
|
|
|
audio['artist'] = d['album']['artist']['name'] # TRACK ARTIST
|
|
|
|
else:
|
|
|
|
audio['artist'] = album['artist']['name']
|
|
|
|
|
|
|
|
if istrack:
|
|
|
|
audio['genre'] = ', '.join(d['album']['genres_list']) # GENRE
|
|
|
|
audio['albumartist'] = d['album']['artist']['name'] # ALBUM ARTIST
|
|
|
|
audio['album'] = d['album']['title'] # ALBUM TITLE
|
|
|
|
audio['date'] = d['album']['release_date_original'].split('-')[0]
|
|
|
|
else:
|
|
|
|
audio['GENRE'] = ', '.join(album['genres_list']) # GENRE
|
|
|
|
audio['albumartist'] = album['artist']['name'] # ALBUM ARTIST
|
|
|
|
audio['album'] = album['title'] # ALBUM TITLE
|
|
|
|
audio['date'] = album['release_date_original'].split('-')[0] # YEAR
|
|
|
|
|
|
|
|
audio.save()
|
|
|
|
title = sanitize_filename(d['title'])
|
|
|
|
os.rename(file, '{}/{:02}. {}.mp3'.format(path, d['track_number'], title))
|