Skip to content
This repository has been archived by the owner on Jul 18, 2022. It is now read-only.

Commit

Permalink
AIFF, Overwritting tags
Browse files Browse the repository at this point in the history
  • Loading branch information
Marekkon5 committed Dec 17, 2020
1 parent 1eca3c7 commit e7df062
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 47 deletions.
10 changes: 9 additions & 1 deletion assets/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ <h2>SETTINGS</h2>
<span class="checkbox-custom circular"></span>
</label>
</div>
<div class="checkbox-container circular-container">
<label class="checkbox-label">&nbsp; &nbsp;&nbsp;
<input type="checkbox" id="overwrite">
<label for="overwrite" class="checkbox-label">Overwrite </label>
<span class="checkbox-custom circular"></span>
</label>
</div>
<div class="text-input" style="padding-top: 2px;">
<label for="artResolution">Album Art Resolution:</label>
<input type="number" id="artResolution" value="500" min="100" max="3000" step="100">
Expand Down Expand Up @@ -155,7 +162,8 @@ <h3>TAGS</h3>
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)
Expand Down
7 changes: 5 additions & 2 deletions assets/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -64,6 +63,10 @@ h2 {
font-weight: 700;
}

#overwrite {
margin-bottom: 8px;
}

.button-start:hover {
background: #82bc00;
color: #f5f5f5;
Expand Down
7 changes: 4 additions & 3 deletions beatporttagger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
104 changes: 63 additions & 41 deletions tagger.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,21 @@
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

UpdatableTags = Enum('UpdatableTags', 'title artist album label bpm genre date key other publishdate')

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:

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand All @@ -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()))
Expand All @@ -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:
Expand All @@ -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
Expand Down

0 comments on commit e7df062

Please sign in to comment.