-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnavidrome_normalizer.py
More file actions
305 lines (282 loc) · 12.8 KB
/
navidrome_normalizer.py
File metadata and controls
305 lines (282 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import os
import re
import shutil
import argparse
import threading
from collections import defaultdict, Counter
from pathlib import Path
from concurrent.futures import ThreadPoolExecutor, as_completed
from mutagen import File
from mutagen.id3 import ID3, TPE1, TPE2, TALB, TIT2, ID3NoHeaderError
from mutagen.flac import FLAC
from mutagen.mp4 import MP4
from unidecode import unidecode
from tqdm import tqdm
class TagFixer:
MOJIBAKE_CHARS = set('ÂÃâãÐÑÒÓÔÕÖרÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿĀāĂ㥹')
def __init__(self, music_dir, dry_run=False, backup=True, max_workers=None):
self.music_dir = Path(music_dir)
self.dry_run = dry_run
self.backup = backup
self.max_workers = max_workers
self.artist_variants = defaultdict(Counter)
self.album_variants = defaultdict(Counter)
self.canonical_artists = {}
self.canonical_albums = {}
self.file_data = []
self.stats = {
'scanned': 0,
'modified': 0,
'encoding_fixed': 0,
'errors': 0
}
self.stats_lock = threading.Lock()
self.print_lock = threading.Lock()
def has_mojibake(self, text):
if not text:
return False
return any(char in self.MOJIBAKE_CHARS for char in text)
def has_cyrillic(self, text):
if not text:
return False
return any('\u0400' <= char <= '\u04FF' for char in text)
def fix_encoding(self, text):
if not text:
return ""
if self.has_cyrillic(text) and not self.has_mojibake(text):
return text
if self.has_mojibake(text):
for source_enc, target_enc in [
('latin-1', 'cp1251'),
('latin-1', 'cp866'),
('latin-1', 'koi8-r'),
('cp1252', 'cp1251')
]:
try:
fixed = text.encode(source_enc).decode(target_enc)
if self.has_cyrillic(fixed) and not self.has_mojibake(fixed):
return fixed
except (UnicodeDecodeError, UnicodeEncodeError):
continue
return text
def normalize_key(self, text):
if not text:
return ""
normalized = unidecode(str(text)).lower()
normalized = re.sub(r'[^\w\s]', '', normalized)
return ' '.join(normalized.split())
def heuristic_split_artists(self, artist_list):
if not artist_list:
return []
delimiters = [
r'\s+feat\.?\s+', r'\s+ft\.?\s+', r'\s+featuring\s+',
r'\s+&\s+', r'\s+and\s+', r'\s+и\s+',
r'\s+with\s+', r'\s+при\s+уч\.?\s+',
r'\s+vs\.?\s+', r'\s+v\.s\.?\s+',
r'\s*[/;|]\s*',
r'\s+\.\s+'
]
pattern = '|'.join(delimiters)
result = []
for artist in artist_list:
artist = re.sub(r'^\((.*)\)$', r'\1', artist.strip())
parts = re.split(pattern, artist, flags=re.IGNORECASE)
for p in parts:
p = p.strip()
p = p.rstrip(')').lstrip('(')
if p:
result.append(p)
seen = set()
return [x for x in result if not (x.lower() in seen or seen.add(x.lower()))]
def read_tags(self, filepath):
try:
audio = File(filepath)
if audio is None:
if filepath.suffix.lower() == '.mp3':
try:
audio = ID3(filepath)
except:
return None
else:
return None
tags = {'artists': [], 'album': '', 'title': ''}
if isinstance(audio, MP4):
artist_keys = ['\xa9ART', 'aART']
elif isinstance(audio, (FLAC, dict)):
artist_keys = ['artist', 'albumartist']
else:
artist_keys = ['TPE1', 'TPE2']
for key in artist_keys:
val = audio.get(key)
if val:
if hasattr(val, 'text'): val = val.text
if isinstance(val, str): val = [val]
tags['artists'].extend([str(v) for v in val if v])
seen = set()
unique_artists = []
for artist in tags['artists']:
if artist not in seen:
seen.add(artist)
unique_artists.append(artist)
tags['artists'] = unique_artists
album_key = '\xa9alb' if isinstance(audio, MP4) else ('album' if isinstance(audio, (FLAC, dict)) else 'TALB')
val = audio.get(album_key)
if val:
if hasattr(val, 'text'): val = val.text[0]
elif isinstance(val, list): val = val[0]
tags['album'] = str(val)
title_key = '\xa9nam' if isinstance(audio, MP4) else ('title' if isinstance(audio, (FLAC, dict)) else 'TIT2')
val = audio.get(title_key)
if val:
if hasattr(val, 'text'): val = val.text[0]
elif isinstance(val, list): val = val[0]
tags['title'] = str(val)
return tags
except Exception:
return None
def _scan_file_task(self, filepath):
tags = self.read_tags(filepath)
if tags is None:
return None
fixed_artists_raw = [self.fix_encoding(a) for a in tags['artists']]
split_artists = self.heuristic_split_artists(fixed_artists_raw)
fixed_album = self.fix_encoding(tags['album'])
fixed_title = self.fix_encoding(tags['title'])
return {
'path': filepath,
'original': tags,
'fixed_artists': split_artists,
'fixed_album': fixed_album,
'fixed_title': fixed_title
}
def scan_library(self):
supported = {'.mp3', '.flac', '.m4a', '.mp4', '.ogg', '.opus'}
files = [f for f in self.music_dir.rglob('*') if f.suffix.lower() in supported]
print(f"\nSTEP 1: Library Analysis")
print(f"Files found: {len(files)}\n")
with ThreadPoolExecutor(max_workers=self.max_workers) as executor:
futures = {executor.submit(self._scan_file_task, f): f for f in files}
for future in tqdm(as_completed(futures), total=len(files), desc="Scanning"):
result = future.result()
if result:
with self.stats_lock:
self.stats['scanned'] += 1
for artist in result['fixed_artists']:
if artist:
key = self.normalize_key(artist)
if key: self.artist_variants[key][artist] += 1
if result['fixed_album']:
key = self.normalize_key(result['fixed_album'])
if key: self.album_variants[key][result['fixed_album']] += 1
self.file_data.append(result)
else:
with self.stats_lock:
self.stats['errors'] += 1
for key, variants in self.artist_variants.items():
self.canonical_artists[key] = variants.most_common(1)[0][0]
for key, variants in self.album_variants.items():
self.canonical_albums[key] = variants.most_common(1)[0][0]
print(f"\nStatistics:")
print(f" Unique artists: {len(self.canonical_artists)}")
print(f" Unique albums: {len(self.album_variants)}")
def _apply_fix_task(self, data):
filepath = data['path']
original = data['original']
target_artists = []
for artist in data['fixed_artists']:
if artist:
key = self.normalize_key(artist)
canonical = self.canonical_artists.get(key, artist)
target_artists.append(canonical)
if not target_artists: target_artists = ["Unknown Artist"]
target_album = data['fixed_album']
if target_album:
key = self.normalize_key(target_album)
target_album = self.canonical_albums.get(key, target_album)
target_title = data['fixed_title']
artists_changed = (target_artists != original['artists'])
album_changed = (target_album != original['album'])
title_changed = (target_title != original['title'])
if artists_changed or album_changed or title_changed:
with self.print_lock:
tqdm.write(f"\nFile: {filepath.name}")
if artists_changed: tqdm.write(f" Artist: {original['artists']} -> {target_artists}")
if album_changed: tqdm.write(f" Album: '{original['album']}' -> '{target_album}'")
if title_changed: tqdm.write(f" Title: '{original['title']}' -> '{target_title}'")
if not self.dry_run:
if self.write_tags(filepath, target_artists, target_album, target_title):
with self.stats_lock:
self.stats['modified'] += 1
self.stats['encoding_fixed'] += 1
def apply_fixes(self):
mode = "DRY RUN" if self.dry_run else "WRITING TO FILES"
print(f"\nSTEP 2: Applying Changes ({mode})\n")
with ThreadPoolExecutor(max_workers=self.max_workers) as executor:
futures = [executor.submit(self._apply_fix_task, d) for d in self.file_data]
for _ in tqdm(as_completed(futures), total=len(futures), desc="Processing"):
pass
def write_tags(self, filepath, artists, album, title):
try:
if self.backup:
backup_path = str(filepath) + ".bak"
if not os.path.exists(backup_path): shutil.copy2(filepath, backup_path)
ext = filepath.suffix.lower()
if ext == '.mp3':
try: tags = ID3(filepath)
except ID3NoHeaderError: tags = ID3()
tags.delall('TPE1'); tags.delall('TPE2'); tags.delall('TALB'); tags.delall('TIT2')
for artist in artists: tags.add(TPE1(encoding=3, text=artist))
if artists: tags.add(TPE2(encoding=3, text=artists[0]))
if album: tags.add(TALB(encoding=3, text=album))
if title: tags.add(TIT2(encoding=3, text=title))
tags.save(filepath, v2_version=4)
elif ext in {'.flac', '.ogg', '.opus'}:
audio = File(filepath)
if audio:
audio['artist'] = artists
audio['albumartist'] = [artists[0]] if artists else []
if album: audio['album'] = [album]
if title: audio['title'] = [title]
audio.save()
elif ext in {'.m4a', '.mp4'}:
audio = File(filepath)
if audio:
audio['\xa9ART'] = artists
audio['aART'] = [artists[0]] if artists else []
if album: audio['\xa9alb'] = [album]
if title: audio['\xa9nam'] = [title]
audio.save()
return True
except Exception as e:
with self.print_lock: tqdm.write(f" Error writing: {e}")
with self.stats_lock: self.stats['errors'] += 1
return False
def print_summary(self):
print("\n" + "="*50 + "\nFINAL STATISTICS\n" + "="*50)
print(f"Files scanned: {self.stats['scanned']}")
print(f"Files modified: {self.stats['modified']}")
print(f"Encodings fixed: {self.stats['encoding_fixed']}")
print(f"Errors: {self.stats['errors']}\n" + "="*50)
if self.dry_run: print("\nNotice: This was a dry run. Run without --dry to apply changes.\n")
def run(self):
self.scan_library()
self.apply_fixes()
self.print_summary()
def main():
parser = argparse.ArgumentParser(description='Navidrome Tag Fixer - Thread-safe Parallel Version')
parser.add_argument('directory', help='Path to music directory')
parser.add_argument('--dry', action='store_true', help='Dry run mode')
parser.add_argument('--no-backup', action='store_true', help='Disable .bak creation')
parser.add_argument('--threads', type=int, default=None, help='Number of threads (default: CPU count)')
args = parser.parse_args()
if not os.path.isdir(args.directory):
print(f"Error: Directory '{args.directory}' not found"); return 1
fixer = TagFixer(music_dir=args.directory, dry_run=args.dry, backup=not args.no_backup, max_workers=args.threads)
try:
fixer.run(); return 0
except KeyboardInterrupt:
print("\n\nAborted by user"); return 130
except Exception as e:
print(f"\nCritical error: {e}"); return 1
if __name__ == "__main__":
exit(main())