-
Notifications
You must be signed in to change notification settings - Fork 0
/
musicman.py
executable file
·508 lines (424 loc) · 17 KB
/
musicman.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
#!/usr/bin/env python3
from __future__ import division, absolute_import, print_function, unicode_literals
import os
import signal
import sys
#import mutagen
import time
#from path import path
import musicman
import mutagen
#from musicman.utils.constants import (
# VERSION,
# NO_TAGS
#)
#from musicman.utils import (
# parse_args,
# load_config
#)
#from musicman.utils.metadata import MetaTag
def spinning_cursor():
while True:
for cursor in '|/-\\':
yield cursor
spinner = spinning_cursor()
def supports_color():
"""
Returns True if the running system's terminal supports color, and False
otherwise.
"""
plat = sys.platform
supported_platform = plat != 'Pocket PC' and (plat != 'win32' or
'ANSICON' in os.environ)
# isatty is not always implemented, #6223.
is_a_tty = hasattr(sys.stdout, 'isatty') and sys.stdout.isatty()
if not supported_platform or not is_a_tty:
return False
return True
def clearLine():
if supports_color():
sys.stdout.write('\033[K')
sys.stdout.flush()
def sanitize(text):
newtext = text
newtext = newtext.replace('<', '').replace('>', '').replace(':', '').replace('"', '').replace('|', '').replace('?', '').replace('*', '').replace('$', '')
newtext = newtext.replace('/', '-')
newtext = newtext.strip()
#if newtext != text:
# clearLine()
# print("text:", text)
# print(" new:", newtext)
return newtext
def escape(text):
newtext = text
newtext = newtext.replace('$', '\$')
return newtext
def getLibrary(path, libFormat=None, excludeDirs=[], includeDirs=[]):
#print("DEBUG: libFormat={0}".format(libFormat))
global originFormat;
if os.path.isdir(path):
for root, directories, filenames in sorted(os.walk(path)):
if root not in excludeDirs:
#if any(root in s for s in includeDirs):
#if [s for s in [root] if any (xs in s for xs in includeDirs)] or len(includeDirs) == 0:
# print("DEBUG: Matched: {0}".format(root))
#print("DEBUG: Found: {0}".format(root))
#if any(root in s for s in includeDirs) or len(includeDirs) == 0:
if [s for s in [root] if any (xs in s for xs in includeDirs)] or len(includeDirs) == 0:
for filename in filenames:
#print("DEBUG: filename: {0}".format(filename))
if filename.endswith(libFormat):
yield os.path.join(root, filename)
# files=[]
#
# if os.path.isdir(path):
# for libraryDir, artists, dummy in os.walk(path):
# for artist in sorted(artists):
# for artistDir, albums, dummy in os.walk(os.path.join(libraryDir, artist)):
# if artistDir in excludeDirs:
# #clearLine()
# #print("Excluding:", artistDir)
# #print()
# continue
# for album in sorted(albums):
# for albumDir, dummy, songs in os.walk(os.path.join(artistDir, album)):
# if albumDir in excludeDirs:
# #clearLine()
# #print("Excluding:", albumDir)
# #print()
# continue
# #print("AlbumDir:", albumDir)
# #print(" Artist:", artist)
# #print(" Album:", album)
# for song in songs:
# print("Scanning", path, next(spinner), end="\r")
# files.append(os.path.join(albumDir, song))
# #print("Path:", os.path.join(albumDir, song))
# clearLine()
# print("Scan complete!")
# return files
def ensure_dir(file_path):
directory = os.path.dirname(file_path)
if not os.path.exists(directory):
os.makedirs(directory)
def copy_file(src, dest):
from shutil import copyfile
ensure_dir(dest)
copyfile(src, dest)
def getEmptyDirs(path, excludeDirs=[]):
if os.path.isdir(path):
for root, directories, filenames in sorted(os.walk(path)):
if root not in excludeDirs:
if not directories and not filenames:
yield root
def getSong(file, orgDir=None, tmpDir=None, dstDir=None):
from musicman.utils.metadata import MetaTag
from musicman.utils.constants import INTERNAL_FORMATS
global config
global originDir
global workingDir
global targetDir
global targetFormat
song = {}
orgDir = originDir if orgDir is None else orgDir
tmpDir = workingDir if tmpDir is None else tmpDir
dstDir = targetDir if dstDir is None else dstDir
if (os.path.splitext(file)[1][1:] in INTERNAL_FORMATS):
metadata = MetaTag(file)
if metadata.tags.get("artist") is None or len(metadata.tags.get("artist")) < 1:
return {'metadata': None}
elif metadata.tags.get("albumartist") is None or len(metadata.tags.get("albumartist")) < 1:
return {'metadata': None}
elif metadata.tags.get('album') is None or len(metadata.tags.get("album")) < 1:
return {'metadata': None}
elif metadata.tags.get("musicbrainz_albumid") is None or len(metadata.tags.get("musicbrainz_albumid")) < 5:
return {'metadata': None}
else:
song['metadata'] = metadata
song['artistName'] = sanitize(metadata.tags["albumartist"])
song['albumName'] = sanitize(metadata.tags["album"])
song['titleName'] = sanitize(metadata.tags["title"])
if isinstance(metadata.tags.get("totaldiscs", 0), tuple):
song['discnumber'] = int(metadata.tags.get('totaldiscs', 0)[0])
song['totaldiscs'] = int(metadata.tags.get("totaldiscs", 0)[1])
else:
song['discnumber'] = int(metadata.tags.get('discnumber', 0))
song['totaldiscs'] = int(metadata.tags.get("totaldiscs", 0))
if isinstance(metadata.tags.get('totaltracks', 0), tuple):
song['tracknumber'] = int(metadata.tags.get('totaltracks', 0)[0])
song['totaltracks'] = int(metadata.tags.get('totaltracks', 0)[1])
else:
song['tracknumber'] = int(metadata.tags.get('tracknumber', 0))
song['totaltracks'] = int(metadata.tags.get('totaltracks', 0))
if song['totaldiscs'] > 1:
song['outPath'] = os.path.join(song['artistName'], song['albumName'])
song['outFile'] = '{0:d}-{1:02d}-{2}'.format(song['discnumber'], song['tracknumber'], song['titleName'])
else:
if metadata.tags.get("tracknumber") is not None:
song['outPath'] = os.path.join(song['artistName'], song['albumName'])
song['outFile'] = '{0:d}-{1:02d}-{2}'.format(song['discnumber'], song['tracknumber'], song['titleName'])
else:
song['outPath'] = os.path.join(song['artistName'], song['albumName'])
outFile = '{0}'.format(song['titleName'])
song['originFile'] = os.path.join(orgDir, song['outPath'], song['outFile'])
song['workingFile'] = os.path.join(tmpDir, song['outPath'], song['outFile'])
song['targetFile'] = os.path.join(dstDir, song['outPath'], song['outFile'])
song['originExt'] = os.path.splitext(file)[1]
song['targetExt'] = '.' + targetFormat
song['targetDir'] = targetDir
return song
else:
clearLine()
print("FATAL: File extension \"{0}\" is not supported.".format(os.path.splitext(file)[1]))
print(" Supported:", ", ".join(INTERNAL_FORMATS))
sys.exit(2)
def cleanLibrary(rootDir, excludeDirs=[], act=False, verbose=0):
global config
if excludeDirs is None:
excludeDirs=[]
print("Clean Dirs:", rootDir)
#while getEmptyDirs(rootDir, excludeDirs) != [] and act == True and tries > 0:
if act:
try:
while getEmptyDirs(rootDir, excludeDirs).__next__():
for path in getEmptyDirs(rootDir, excludeDirs):
print("Removing:", path)
if act:
try:
os.rmdir(path)
except OSError as err:
print("ERROR: Failed to remove directory:", err)
sys.exit(5)
except StopIteration:
pass
else:
for path in getEmptyDirs(rootDir, excludeDirs):
print("Empty:", path)
#print("Processing Complete!")
def renameLibrary(rootDir, libFormat, excludeDirs=[], includeDirs=[], act=False, verbose=0):
global config
if excludeDirs is None:
excludeDirs=[]
if includeDirs is None:
includeDirs=[]
print("Renaming:", rootDir)
for file in getLibrary(rootDir, libFormat, excludeDirs, includeDirs):
if (os.path.isdir(os.path.dirname(file)) and os.path.isfile(file)):
clearLine()
print("Processing:", os.path.dirname(os.path.dirname(file)), next(spinner), end="\r")
song = getSong(file, rootDir)
if song['metadata'] is None:
if verbose > 2:
clearLine()
print("Skipping: {0} due to lack of metadata".format(file))
else:
if not os.path.isfile(song['originFile'] + song['originExt']):
clearLine()
print("Found:", file)
if verbose > 0:
print(" New:", song['originFile'] + song['originExt'])
if (act):
if verbose > 1:
print("Renaming: \"{0}\" -> \"{1}\"".format(file, song['originFile'] + song['originExt']))
try:
os.renames(file, song['originFile'] + song['originExt'])
except OSError as err:
print("ERROR: Failed to move:", err)
sys.exit(5)
clearLine()
#print("Processing Complete!")
def findUntagged(rootDir, libFormat, excludeDirs=[], includeDirs=[], verbose=0):
global config
if excludeDirs is None:
excludeDirs=[]
if includeDirs is None:
includeDirs=[]
print("Find Untagged:", rootDir)
for file in getLibrary(rootDir, libFormat, excludeDirs, includeDirs):
if (os.path.isdir(os.path.dirname(file)) and os.path.isfile(file)):
clearLine()
print("Processing:", os.path.dirname(os.path.dirname(file)), next(spinner), end="\r")
song = getSong(file)
if song['metadata'] is None:
clearLine()
print("Untagged: {0}".format(file))
clearLine()
#print("Processing Complete!")
def findNew(rootDir, libFormat, tmpDir, dstDir, dstFormat, excludeDirs=[], includeDirs=[], verbose=0):
global config
if excludeDirs is None:
excludeDirs=[]
if includeDirs is None:
includeDirs=[]
print("Find New Media")
for file in getLibrary(rootDir, libFormat, excludeDirs, includeDirs):
if (os.path.isdir(os.path.dirname(file)) and os.path.isfile(file)):
clearLine()
print("Processing:", os.path.dirname(os.path.dirname(file)), next(spinner), end="\r")
song = getSong(file)
if song['metadata'] is None:
if verbose > 2:
clearLine()
print("Skipping: {0} due to lack of metadata".format(file))
else:
if not (os.path.isfile(song['workingFile'] + song['targetExt']) or
os.path.isfile(song['targetFile'] + song['targetExt'])):
clearLine()
print("New:", file)
if verbose > 0:
if not os.path.isfile(os.path.isfile(song['workingFile'] + song['targetExt'])):
print(" No:", song['workingFile'] + song['targetExt'])
elif not os.path.isfile(os.path.isfile(song['targetFile'] + song['targetExt'])):
print(" No:", song['targetFile'] + song['targetExt'])
#clearLine()
#print("Processing Complete!")
def syncWorking(tmpDir, libFormat, excludeDirs=[], includeDirs=[], act=False, verbose=0):
global config
if excludeDirs is None:
excludeDirs=[]
if includeDirs is None:
includeDirs=[]
print("Sync Target Media")
for file in getLibrary(tmpDir, libFormat, excludeDirs, includeDirs):
if (os.path.isdir(os.path.dirname(file)) and os.path.isfile(file)):
clearLine()
print("Processing:", os.path.dirname(os.path.dirname(file)), next(spinner), end="\r")
song = getSong(file)
if song['metadata'] is None:
if verbose > 2:
clearLine()
print("Skipping: {0} due to lack of metadata".format(file))
else:
filename = os.path.basename(file)
file_ext = os.path.splitext(filename)[1]
if not os.path.isfile(song['targetFile'] + song['originExt']):
print("Sync:", file)
if verbose > 0:
print(" To:", song['targetFile'] + song['originExt'])
if act:
copy_file(file, song['targetFile'] + song['originExt'])
#print("would've acted")
#if not os.path.isfile(os.path.join(destDir, song['outPath'], song['outFile'] + file_ext)):
# print("Sync:", file)
# if verbose > 0:
# print(" To:", os.path.join(destDir, song['outPath'], song['outFile'] + file_ext))
#
# if act:
# print("would've acted")
#clearLine()
#print("Processing Complete!")
def convertMedia(rootDir, originFormat, tmpDir, dstDir, dstFormat, excludeDirs=[], includeDirs=[], act=False, verbose=0):
import subprocess
from musicman.utils.copytags import (
copy_tags
)
if excludeDirs is None:
excludeDirs=[]
if includeDirs is None:
includeDirs=[]
print("Convert Library Media")
for file in getLibrary(rootDir, originFormat, excludeDirs, includeDirs):
if (os.path.isdir(os.path.dirname(file)) and os.path.isfile(file)):
clearLine()
print("Processing:", os.path.dirname(os.path.dirname(file)), next(spinner), end="\r")
song = getSong(file, rootDir, tmpDir, targetDir)
if song['metadata'] is None:
if verbose > 2:
clearLine()
print("Skipping: {0} due to lack of metadata".format(file))
else:
if not (os.path.isfile(song['workingFile'] + song['targetExt']) or
os.path.isfile(song['targetFile'] + song['targetExt'])):
clearLine()
print("New:", file)
if verbose > 2:
if not os.path.isfile(os.path.isfile(song['workingFile'] + song['targetExt'])):
print(" No:", song['workingFile'] + song['targetExt'])
elif not os.path.isfile(os.path.isfile(song['targetFile'] + song['targetExt'])):
print(" No:", song['targetFile'] + song['targetExt'])
if act:
#print("would've acted")
if not os.path.isdir(os.path.dirname(song['workingFile'])):
os.makedirs(os.path.dirname(song['workingFile']))
#subprocess.call("ffmpeg", "-loglevel", "quiet", "-stats", "-i", file, "-vn", "-c:a", "libfdk_aac", "-vbr", "5", "-nostdin", "-y", song['workingFile'] + song['targetExt'])
try:
#subprocess.call('/usr/bin/ffmpeg -loglevel quiet -stats -i "{0}" -vn -c:a libfdk_aac -vbr 5 -nostdin -y "{1}"'.format(file, song['workingFile'] + song['targetExt']), shell=True)
#subprocess.call('/usr/bin/ffmpeg -i "{0}" -f caf - | /usr/bin/fdkaac -I -p 29 -m 5 -o "{1}" -'.format(file, song['workingFile'] + song['targetExt']), shell=True)
subprocess.call('/usr/bin/ffmpeg -i "{0}" -f caf - | /usr/bin/fdkaac -I -p 2 -m 5 -o "{1}" -'.format(escape(file), escape(song['workingFile'] + song['targetExt'])), shell=True)
#new = MetaData(song['workingFile'] + song['targetExt'])
#new.update(song['metadata'])
#new.save()
#old = mutagen.File(file)
#new = mutagen.File(song['workingFile'] + song['targetExt'])
#new.update(old)
#new.save()
copy_tags(file, song['workingFile'] + song['targetExt'])
except KeyboardInterrupt:
if os.path.isfile(song['workingFile'] + song['targetExt']):
os.remove(song['workingFile'] + song['targetExt'])
clearLine()
print(end='\r')
print("Aborted by user")
sys.exit(0)
clearLine()
if __name__ == '__main__':
global opt
global config
global originPath, originFormat, targetPath, targetFormat, workPath
import configparser
opt = musicman.utils.parse_args()
config = musicman.utils.load_config()
#print("opt:", opt)
try:
originDir = config['origin']['path'] if opt.originDir is None else opt.originDir
except AttributeError:
originDir = config['origin']['path']
try:
originFormat = config['origin']['format'] if opt.origFormat is None else opt.origFormat
except AttributeError:
originFormat = config['origin']['format']
try:
targetDir = config['target']['path'] if opt.targetDir is None else opt.targetDir
except AttributeError:
targetDir = config['target']['path']
try:
targetFormat = config['target']['format'] if opt.targetFormat is None else opt.targetFormat
except AttributeError:
targetFormat = config['target']['format']
try:
workingDir = config['working']['path'] if opt.workingDir is None else opt.workingDir
except AttributeError:
workingDir = config['working']['path']
if opt.mode is None:
print("ERROR: No command provided.")
sys.exit(1)
try:
if opt.mode == 'clean':
cleanLibrary(originDir, originFormat, opt.excludeDirs, opt.includeDirs, opt.act, opt.verbose)
cleanLibrary(targetDir, targetFormat, opt.excludeDirs, opt.includeDirs, opt.act, opt.verbose)
cleanLibrary(workingDir, targetFormat, opt.excludeDirs, opt.includeDirs, opt.act, opt.verbose)
elif opt.mode == 'rename':
renameLibrary(originDir, originFormat, opt.excludeDirs, opt.includeDirs, opt.act, opt.verbose)
renameLibrary(targetDir, targetFormat, opt.excludeDirs, opt.includeDirs, opt.act, opt.verbose)
renameLibrary(workingDir, targetFormat, opt.excludeDirs, opt.includeDirs, opt.act, opt.verbose)
elif opt.mode == 'scan':
if opt.scanMode is None:
print("ERROR: Subcommand for scan not provided.")
sys.exit(1)
elif opt.scanMode == 'untagged':
findUntagged(originDir, originFormat, opt.excludeDirs, opt.includeDirs, opt.verbose)
findUntagged(targetDir, targetFormat, opt.excludeDirs, opt.includeDirs, opt.verbose)
findUntagged(workingDir, targetFormat, opt.excludeDirs, opt.includeDirs, opt.verbose)
elif opt.scanMode == 'new':
findNew(originDir, originFormat, workingDir, targetDir, targetFormat, opt.excludeDirs, opt.includeDirs, opt.verbose)
elif opt.mode == 'sync':
syncWorking(workingDir, targetFormat, opt.excludeDirs, opt.includeDirs, opt.act, opt.verbose)
elif opt.mode == 'convert':
convertMedia(originDir, originFormat, workingDir, targetDir, targetFormat, opt.excludeDirs, opt.includeDirs, opt.act, opt.verbose)
except KeyboardInterrupt:
clearLine()
print(end='\r')
print("Aborted by user")
sys.exit(0)
clearLine()
print("Processing Complete!")