-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfixtags.py
executable file
·299 lines (279 loc) · 9.67 KB
/
fixtags.py
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
# import system libraries
import json
import time
import sys
import os
import re
import glob
import traceback
from rich import print as rprint
from datetime import datetime
from pathlib import Path, PurePosixPath, PurePath
from tqdm import tqdm
import requests
from urllib.parse import quote, urlencode
# import music libraries
# https://github.com/joalla/discogs_client
import discogs_client
from mutagen.flac import FLAC
# music_tag was the only library I found that allows me to delete the YEAR tag to make sure I only have one in there
# https://github.com/KristoforMaynard/music-tag
import music_tag
# import config file containing Discogs api_key (String with API token from https://www.discogs.com/en/settings/developers?lang_alt=en )
from config import api_key
# import DRMETER https://github.com/janw/drmeter/
from drmeter.algorithm import dynamic_range
from drmeter.models import AudioData
import soundfile as sf
# logging function
def timelog(txt1, txt2):
log_msg = '[green]' + txt1 + '[/green]'
log_msg = log_msg + ' ' * (45 - len(log_msg))
rprint('[white]' + datetime.now().strftime('%H:%M:%S') + '[/white] ' + log_msg + txt2)
# calculate song and album dynamic range and write tags to files
def calculate_dr(albumpath):
# assumption: folder only contains a single album
dr_sum = 0
dr_tracks = 0
dr_dirty = False
# calculate title DR (if possible)
for p in Path(albumpath).rglob('*.flac'):
fullfilename = str(PurePosixPath(p))
dr_tags = FLAC(fullfilename)
try:
drsong = int(dr_tags['DYNAMIC RANGE'][0])
except (TypeError, KeyError):
drsong = 0
try:
with sf.SoundFile(fullfilename) as data:
result = dynamic_range(AudioData.from_soundfile(data))
DR = round(result.overall_dr_score)
except:
traceback.print_exc()
print(fullfilename)
dr_dirty = True
DR = 0
if int(DR) != drsong and DR != 0:
timelog(
'DR old ' + str(drsong).zfill(2) + ' --> new ' + str(DR).zfill(2), str(dr_tags['TITLE'])
)
dr_tags['DYNAMIC RANGE'] = str(DR).zfill(2)
dr_tags.save()
if DR > 0:
dr_tracks += 1
dr_sum += DR
if dr_tracks > 0:
dr_album = round(dr_sum / dr_tracks)
else:
dr_album = 0
return dr_album
def get_lrclyrics(flactags):
# try to find better lyrics data for one song
duration = str(round(flactags.info.length))
try:
albumtitle = flactags['ORIGINAL_TITLE'][0]
except KeyError:
albumtitle = flactags['ALBUM'][0]
# query lrclib.net https://github.com/tranxuanthang/lrcget
url_template = 'https://lrclib.net/api/get?{}'
params = {
'artist_name': flactags['ALBUMARTIST'][0],
'track_name': flactags['TITLE'][0],
'album_name': albumtitle,
'duration': duration,
}
url = url_template.format(urlencode(params, safe='()', quote_via=quote))
lyricsdata = ''
lyricstype = 'none'
try:
response = requests.get(url)
data = response.json()
if data['syncedLyrics']:
lyricsdata = data['syncedLyrics']
lyricstype = 'lrc'
elif data['plainLyrics']:
lyricsdata = data['plainLyrics']
lyricstype = 'plain'
except:
lyricsdata = ''
return lyricsdata, lyricstype
# walk flacdir searching for directories holding albums with flac files
def walkdirs(fixdir):
global lrc_total
flac_files = 0
lrctotal = 0
nototal = 0
lrcnew = 0
txttotal = 0
txtnew = 0
current_album = ''
current_artist = ''
current_discogs_id = 12345
# initialize Discogs API
dclient = discogs_client.Client('PyDiscogsTagger/0.1', user_token=api_key)
# find all directories containing flac files below fixdir
files = glob.glob(os.path.join(fixdir, '**', '*.flac'), recursive=True)
paths = list(set(map(os.path.dirname, files)))
pathbar = tqdm(range(len(paths)))
for i in pathbar:
shortpath = (
(PurePath(paths[i]).name[:40] + '..')
if len(PurePath(paths[i]).name) > 40
else PurePath(paths[i]).name
)
# timelog('Analyzing ', shortpath)
firstflac = next(Path(paths[i]).rglob('*.flac'), None)
dynamicrange = calculate_dr(paths[i])
tags = FLAC(firstflac)
discogs = True
try:
discogs_idstring = tags['DISCOGS_RELEASE_ID']
discogs_id = int(discogs_idstring[0])
except:
discogs = False
# if we found discogs tags to work with go ahead
if discogs:
tag_album = str(tags['album'])
tag_artist = str(tags['albumartist'])
bitrate = int(tags.info.sample_rate / 1000)
drelease = dclient.release(discogs_id)
# make Discogs API rate limit happy
time.sleep(3)
album_catno = ''
album_label = ''
try:
labels_json = json.dumps(drelease.labels[0].data)
labels = json.loads(labels_json)
album_label = labels['name']
album_catno = ' ' + (labels['catno'])
except:
print(tag_album)
print(labels_json)
try:
album_name = tags['ORIGINAL FILENAME'][0].strip()
except:
album_name = drelease.title.strip()
album_artist = tags['ALBUMARTIST'][0]
# get the release date from the master release which will be used for all files
# release date goes into the album name instead
album_year_release = drelease.year
mrelease = drelease.master
if drelease.master:
album_year_master = mrelease.main_release.year
else:
album_year_master = album_year_release
if album_year_release == 0 and album_year_master != 0:
album_year_release = album_year_master
if album_year_release == 0:
album_year_release_str = ''
else:
album_year_release_str = str(album_year_release)
if album_year_release != 0 and album_year_master == 0:
album_year_master = album_year_release
try:
album_description = tags['SUBTITLE'][0].strip() + ' '
except KeyError:
album_description = ''
album_newtitle = (
album_name
+ ' ('
+ album_year_release_str
+ ' '
+ album_description
+ str(bitrate)
+ 'kHz'
+ ')'
)
songs = 0
# write new tags to files
for subdir, dirs, files in os.walk(paths[i]):
for filename in files:
filepath = paths[i] + os.sep + filename
if filepath.endswith('.flac'):
mtags = music_tag.load_file(filepath)
mtags.remove_tag('YEAR')
mtags.save()
tags = FLAC(filepath)
try:
lyrics = tags['LYRICS'][0].strip()
except KeyError:
lyrics = ''
if lyrics == '' or not re.match(r'\[\d\d\D\d\d\D\d\d\]', lyrics):
lrc, lrctype = get_lrclyrics(tags)
if lrctype == 'lrc':
tags['LYRICS'] = lrc
lrcnew += 1
lrctotal += 1
tqdm.write(
' LRC lyrics added for '
+ tags['TITLE'][0]
+ ' ('
+ album_artist
+ ')'
)
elif lrctype == 'plain' and lyrics == '':
tags['LYRICS'] = lrc
txtnew += 1
txttotal += 1
tqdm.write(
' TXT lyrics added for '
+ tags['TITLE'][0]
+ ' ('
+ album_artist
+ ')'
)
else:
if lyrics != '':
txttotal += 1
else:
nototal += 1
else:
if re.match(r'\[\d\d\D\d\d\D\d\d\]', lyrics):
lrctotal += 1
try:
subtitle = tags['SET SUBTITLE'][0].strip()
except KeyError:
subtitle = ''
tags['DISCSUBTITLE'] = subtitle
tags['YEAR'] = str(album_year_release)
tags['RELEASEDATE'] = str(album_year_release)
tags['DATE'] = str(album_year_master)
tags['ORIGINALDATE'] = str(album_year_master)
tags['VERSION'] = album_description.strip()
tags['ALBUM'] = album_newtitle
tags['ORIGINAL_TITLE'] = album_name
tags['ALBUM DYNAMIC RANGE'] = str(dynamicrange).zfill(2)
tags.save()
flac_files += 1
else:
timelog('No Discogs tags found in ', shortpath)
timelog('Finished analysis', fixdir)
tqdm.write(
' '
+ str(flac_files)
+ ' FLAC files processed, '
+ str(lrcnew)
+ ' LRC lyrics and '
+ str(txtnew)
+ ' TXT lyrics added'
)
tqdm.write(
' '
+ str(flac_files)
+ ' FLAC files processed, '
+ str(lrctotal)
+ ' LRC lyrics and '
+ str(txttotal)
+ ' TXT lyrics present, '
+ str(nototal)
+ ' files without lyrics'
)
def main():
if len(sys.argv) != 2:
from config import flacdir
else:
flacdir = sys.argv[1]
timelog('Starting analysis', flacdir)
walkdirs(flacdir)
if __name__ == '__main__':
main()