From e7df062c59dc7182846ecec2b29622345127b374 Mon Sep 17 00:00:00 2001 From: Marekkon5 Date: Thu, 17 Dec 2020 22:05:54 +0100 Subject: [PATCH] AIFF, Overwritting tags --- assets/index.html | 10 ++++- assets/style.css | 7 +++- beatporttagger.py | 7 ++-- tagger.py | 104 ++++++++++++++++++++++++++++------------------ 4 files changed, 81 insertions(+), 47 deletions(-) diff --git a/assets/index.html b/assets/index.html index ae65601..72f88d7 100644 --- a/assets/index.html +++ b/assets/index.html @@ -23,6 +23,13 @@

SETTINGS

+
+ +
@@ -155,7 +162,8 @@

TAGS

replaceArt: document.getElementById('replaceArt').checked, artistSeparator: document.getElementById('artistSeparator').value, fuzziness: parseInt(document.getElementById('fuzziness').value, 10), - path: document.getElementById('path').value + path: document.getElementById('path').value, + overwrite: document.getElementById('overwrite').checked }; //Add tags if (document.getElementById('updateTitle').checked) diff --git a/assets/style.css b/assets/style.css index ffbf6f0..bb2c39f 100644 --- a/assets/style.css +++ b/assets/style.css @@ -52,8 +52,7 @@ h2 { border: 0px solid #efefef; border-radius: 2px; background-color: #94D500; - margin-top: 0px; - margin-bottom: 14px; + margin-top: 19px; width: 270px; height: 36px; font-size: 20px; @@ -64,6 +63,10 @@ h2 { font-weight: 700; } +#overwrite { + margin-bottom: 8px; +} + .button-start:hover { background: #82bc00; color: #f5f5f5; diff --git a/beatporttagger.py b/beatporttagger.py index 9d80a0d..5c1f423 100644 --- a/beatporttagger.py +++ b/beatporttagger.py @@ -55,7 +55,8 @@ def start(): replace_art=data['replaceArt'], art_resolution=data['artResolution'], artist_separator=data['artistSeparator'], - fuzziness=data['fuzziness'] + fuzziness=data['fuzziness'], + overwrite=data['overwrite'] ) #Start @@ -105,8 +106,8 @@ def start_flask(): 'http://localhost:36958/', resizable=False, width=400, - height=806, - min_size=(400, 806), + height=845, + min_size=(400, 845), ) webview.start(debug=False) sys.exit() \ No newline at end of file diff --git a/tagger.py b/tagger.py index c143f2c..6b9cb9b 100644 --- a/tagger.py +++ b/tagger.py @@ -8,6 +8,7 @@ from enum import Enum from mutagen.id3 import ID3, TIT2, TPE1, TALB, TPUB, TBPM, TCON, TDAT, TYER, APIC, TKEY, TORY, TXXX from mutagen.flac import FLAC, Picture +from mutagen.aiff import AIFF import beatport @@ -15,12 +16,13 @@ class TagUpdaterConfig: - def __init__(self, update_tags = [UpdatableTags.genre], replace_art = False, artist_separator = ';', art_resolution = 1200, fuzziness = 80): + def __init__(self, update_tags = [UpdatableTags.genre], replace_art = False, artist_separator = ';', art_resolution = 1200, fuzziness = 80, overwrite = False): self.update_tags = update_tags self.replace_art = replace_art self.artist_separator = artist_separator self.art_resolution = art_resolution self.fuzziness = fuzziness + self.overwrite = overwrite class TagUpdater: @@ -50,11 +52,13 @@ def tag_dir(self, path: str): self.fail = [] self.total = 0 + extensions = ['.mp3', 'flac', 'aiff', '.aif'] + #Get files files = [] for root, _, f in os.walk(path): for file in f: - if file.lower().endswith('.mp3') or file.lower().endswith('.flac'): + if file.lower()[-4:] in extensions: files.append(os.path.join(root, file)) self.total = len(files) @@ -88,13 +92,18 @@ def tag_file(self, file): try: #MP3 Files if file.lower().endswith('.mp3'): - title, artists = self.info_mp3(file) + title, artists = self.info_id3(file) file_type = 'mp3' #FLAC if file.lower().endswith('.flac'): title, artists = self.info_flac(file) file_type = 'flac' - except Exception: + #AIFF + if file.lower().endswith('.aiff') or file.lower().endswith('.aif'): + title, artists = self.info_id3(file) + file_type = 'aiff' + + except Exception as e: logging.error('Invalid file: ' + file) self._fail(file) return @@ -120,38 +129,45 @@ def tag_file(self, file): return #Update files - if file_type == 'mp3': - self.update_mp3(file, track) + if file_type == 'mp3' or file_type == 'aiff': + self.update_id3(file, track) if file_type == 'flac': self.update_flac(file, track) self._ok(file) - def update_mp3(self, path: str, track: beatport.Track): - f = ID3(path) + def update_id3(self, path: str, track: beatport.Track): + #AIFF Check + aiff = None + if path.endswith('.aiff') or path.endswith('.aif'): + aiff = AIFF(path) + f = aiff.tags + else: + f = ID3() + f.load(path, v2_version=3, translate=True) #Update tags - if UpdatableTags.title in self.config.update_tags: - f['TIT2'] = TIT2(text=track.title) - if UpdatableTags.artist in self.config.update_tags: - f['TPE1'] = TPE1(text=self.config.artist_separator.join([a.name for a in track.artists])) - if UpdatableTags.album in self.config.update_tags: - f['TALB'] = TALB(text=track.album.name) - if UpdatableTags.label in self.config.update_tags: - f['TPUB'] = TPUB(text=track.label.name) - if UpdatableTags.bpm in self.config.update_tags: - f['TBPM'] = TBPM(text=str(track.bpm)) - if UpdatableTags.genre in self.config.update_tags: - f['TCON'] = TCON(text=', '.join([g.name for g in track.genres])) - if UpdatableTags.date in self.config.update_tags: + if UpdatableTags.title in self.config.update_tags and self.config.overwrite: + f.setall('TIT2', [TIT2(text=track.title)]) + if UpdatableTags.artist in self.config.update_tags and self.config.overwrite: + f.setall('TPE1', [TPE1(text=self.config.artist_separator.join([a.name for a in track.artists]))]) + if UpdatableTags.album in self.config.update_tags and (self.config.overwrite or len(f.getall('TALB')) == 0): + f.setall('TALB', [TALB(text=track.album.name)]) + if UpdatableTags.label in self.config.update_tags and (self.config.overwrite or len(f.getall('TPUB')) == 0): + f.setall('TPUB', [TPUB(text=track.label.name)]) + if UpdatableTags.bpm in self.config.update_tags and (self.config.overwrite or len(f.getall('TBPM')) == 0): + f.setall('TBPM', [TBPM(text=str(track.bpm))]) + if UpdatableTags.genre in self.config.update_tags and (self.config.overwrite or len(f.getall('TCON')) == 0): + f.setall('TCON', [TCON(text=', '.join([g.name for g in track.genres]))]) + if UpdatableTags.date in self.config.update_tags and (self.config.overwrite or (len(f.getall('TYER')) == 0 and len(f.getall('TDAT')) == 0)): date = track.release_date.strftime('%d%m') - f['TDAT'] = TDAT(text=date) - f['TYER'] = TYER(text=str(track.release_date.year)) - if UpdatableTags.key in self.config.update_tags: - f['TKEY'] = TKEY(text=track.id3key()) - if UpdatableTags.publishdate in self.config.update_tags: - f['TORY'] = TORY(text=str(track.publish_date.year)) + f.setall('TDAT', [TDAT(text=date)]) + f.setall('TYER', [TYER(text=str(track.release_date.year))]) + if UpdatableTags.key in self.config.update_tags and (self.config.overwrite or len(f.getall('TKEY')) == 0): + f.setall('TKEY', [TKEY(text=track.id3key())]) + if UpdatableTags.publishdate in self.config.update_tags and (self.config.overwrite or len(f.getall('TORY')) == 0): + f.setall('TORY', [TORY(text=str(track.publish_date.year))]) #Other keys if UpdatableTags.other in self.config.update_tags: f.add(TXXX(desc='WWWAUDIOFILE', text=track.url())) @@ -175,31 +191,32 @@ def update_mp3(self, path: str, track: beatport.Track): except Exception: logging.warning('Error downloading cover for file: ' + path) - f.save(v2_version=3) - #Remove V1 tags - f.delete(path, delete_v1=True, delete_v2=False) + if aiff == None: + f.save(path, v2_version=3, v1=0) + else: + aiff.save() def update_flac(self, path: str, track: beatport.Track): f = FLAC(path) - if UpdatableTags.title in self.config.update_tags: + if UpdatableTags.title in self.config.update_tags and self.config.overwrite: f['TITLE'] = track.title - if UpdatableTags.artist in self.config.update_tags: + if UpdatableTags.artist in self.config.update_tags and self.config.overwrite: f['ARTIST'] = self.config.artist_separator.join([a.name for a in track.artists]) - if UpdatableTags.album in self.config.update_tags: + if UpdatableTags.album in self.config.update_tags and (self.config.overwrite or f.get('ALBUM') == None): f['ALBUM'] = track.album.name - if UpdatableTags.label in self.config.update_tags: + if UpdatableTags.label in self.config.update_tags and (self.config.overwrite or f.get('LABEL') == None): f['LABEL'] = track.label.name - if UpdatableTags.bpm in self.config.update_tags: + if UpdatableTags.bpm in self.config.update_tags and (self.config.overwrite or f.get('BPM') == None): f['BPM'] = str(track.bpm) - if UpdatableTags.genre in self.config.update_tags: + if UpdatableTags.genre in self.config.update_tags and (self.config.overwrite or f.get('GENRE') == None): f['GENRE'] = ', '.join([g.name for g in track.genres]) - if UpdatableTags.date in self.config.update_tags: + if UpdatableTags.date in self.config.update_tags and (self.config.overwrite or f.get('DATE') == None): f['DATE'] = track.release_date.strftime('%Y-%m-%d') #Year - part of date - if UpdatableTags.key in self.config.update_tags: + if UpdatableTags.key in self.config.update_tags and (self.config.overwrite or f.get('INITIALKEY') == None): f['INITIALKEY'] = track.id3key() - if UpdatableTags.publishdate in self.config.update_tags: + if UpdatableTags.publishdate in self.config.update_tags and (self.config.overwrite or f.get('ORIGINALDATE') == None): f['ORIGINALDATE'] = str(track.publish_date.year) #Other tags if UpdatableTags.other in self.config.update_tags: @@ -224,8 +241,13 @@ def update_flac(self, path: str, track: beatport.Track): f.save() #Info returns title and artists aray - def info_mp3(self, path: str) -> (str, list): - f = ID3(path) + def info_id3(self, path: str) -> (str, list): + #AIFF + if path.endswith('.aiff') or path.endswith('.aif'): + f = AIFF(path).tags + else: + f = ID3(path) + title = str(f['TIT2']) artists = self._parse_artists(str(f['TPE1'])) return title, artists