Add fix music tag

This commit is contained in:
2024-05-14 15:21:45 +08:00
parent 7df4502f39
commit 3a74014825

70
fix_music_tag.py Normal file
View 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)