mirror of
https://github.com/lifegpc/pythonscript.git
synced 2026-06-05 11:08:49 +08:00
Add fix music tag
This commit is contained in:
70
fix_music_tag.py
Normal file
70
fix_music_tag.py
Normal file
@@ -0,0 +1,70 @@
|
||||
from argparse import ArgumentParser
|
||||
from os import listdir
|
||||
from os.path import isdir, isfile, join
|
||||
import re
|
||||
from typing import List
|
||||
import taglib
|
||||
|
||||
|
||||
ARTIST_SEP = re.compile(r'(/|&)')
|
||||
|
||||
|
||||
def get_files(dir: str, r: bool) -> List[str]:
|
||||
if isfile(dir):
|
||||
return [dir]
|
||||
if not isdir(dir):
|
||||
return []
|
||||
files = listdir(dir)
|
||||
re = []
|
||||
for file in files:
|
||||
file = join(dir, file)
|
||||
if isdir(file):
|
||||
if r:
|
||||
re += get_files(file, r)
|
||||
elif file.endswith('.m4a') or file.endswith(".flac") or file.endswith(".mp3"): # noqa: E501
|
||||
re.append(file)
|
||||
return re
|
||||
|
||||
|
||||
def fix_tag(file: str):
|
||||
f = taglib.File(file)
|
||||
need_save = False
|
||||
|
||||
def change(tag, value):
|
||||
nonlocal need_save
|
||||
if not need_save:
|
||||
need_save = True
|
||||
print('Change file tag:', file)
|
||||
print(tag, f.tags[tag], '->', value)
|
||||
f.tags[tag] = value
|
||||
|
||||
try:
|
||||
for tag in f.tags:
|
||||
value = f.tags[tag]
|
||||
if tag in ['ARTIST', 'ALBUMARTIST']:
|
||||
result = []
|
||||
need_change = False
|
||||
for artist in value:
|
||||
r = ARTIST_SEP.split(artist)
|
||||
if len(r) > 1:
|
||||
need_change = True
|
||||
for a in r:
|
||||
ar = a.strip()
|
||||
if ar in ['&', '/']:
|
||||
continue
|
||||
result.append(ar)
|
||||
if need_change:
|
||||
change(tag, result)
|
||||
if need_save:
|
||||
f.save()
|
||||
finally:
|
||||
f.close()
|
||||
|
||||
|
||||
p = ArgumentParser()
|
||||
p.add_argument("INPUT", help="Input directory")
|
||||
p.add_argument("-r", "--recursive", help="Recursive search",
|
||||
action="store_true", default=False)
|
||||
arg = p.parse_intermixed_args()
|
||||
for f in get_files(arg.INPUT, arg.recursive):
|
||||
fix_tag(f)
|
||||
Reference in New Issue
Block a user