tag-mp3s.py: robustify
This commit is contained in:
parent
40fb9f1817
commit
511abd591d
35
tag-mp3s.py
35
tag-mp3s.py
@ -35,7 +35,7 @@ def choose_artwork(mp3s, jpgs):
|
|||||||
if len(mp3s) < min_mp3s_for_album:
|
if len(mp3s) < min_mp3s_for_album:
|
||||||
return None
|
return None
|
||||||
if len(jpgs) == 0:
|
if len(jpgs) == 0:
|
||||||
sys.stderr.write("No .jpg files found\n")
|
sys.stderr.write("No .jpg files found in %s\n" % (os.path.dirname(mp3s[0])))
|
||||||
return None
|
return None
|
||||||
if len(jpgs) > 1:
|
if len(jpgs) > 1:
|
||||||
sys.stderr.write("Multiple .jpg album artwork files found\n")
|
sys.stderr.write("Multiple .jpg album artwork files found\n")
|
||||||
@ -61,7 +61,7 @@ def clean_tag(mp3, tagname):
|
|||||||
orig_text = tag.text[0]
|
orig_text = tag.text[0]
|
||||||
new_text = clean_title(orig_text)
|
new_text = clean_title(orig_text)
|
||||||
if new_text != orig_text:
|
if new_text != orig_text:
|
||||||
sys.stdout.write("Changing %s from \"%s\" to \"%s\"\n" % (tagname, orig_text, new_text))
|
sys.stdout.write("Changing %s from \"%s\" to \"%s\" for %s\n" % (tagname, orig_text, new_text, mp3.filename))
|
||||||
tag.text = [new_text]
|
tag.text = [new_text]
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
@ -69,20 +69,23 @@ def clean_tag(mp3, tagname):
|
|||||||
def process_mp3(mp3_path, artwork_path):
|
def process_mp3(mp3_path, artwork_path):
|
||||||
mp3 = mutagen.mp3.MP3(mp3_path)
|
mp3 = mutagen.mp3.MP3(mp3_path)
|
||||||
current_tags = mp3.tags
|
current_tags = mp3.tags
|
||||||
changed = False
|
if current_tags is not None:
|
||||||
changed = clean_tag(mp3, "TIT2") or changed
|
changed = False
|
||||||
changed = clean_tag(mp3, "TALB") or changed
|
changed = clean_tag(mp3, "TIT2") or changed
|
||||||
if artwork_path is not None:
|
changed = clean_tag(mp3, "TALB") or changed
|
||||||
apic_tags = [tag for tag in current_tags if tag == "APIC" or tag.startswith("APIC:")]
|
if artwork_path is not None:
|
||||||
if len(apic_tags) < 1:
|
apic_tags = [tag for tag in current_tags if tag == "APIC" or tag.startswith("APIC:")]
|
||||||
print("Adding APIC tag to %s..." % mp3_path)
|
if len(apic_tags) < 1:
|
||||||
jpg_data = read_file(artwork_path)
|
print("Adding APIC tag to %s..." % mp3_path)
|
||||||
apic = mutagen.id3.APIC(0, "image/jpeg", 0, "", jpg_data)
|
jpg_data = read_file(artwork_path)
|
||||||
mp3.tags.add(apic)
|
apic = mutagen.id3.APIC(0, "image/jpeg", 0, "", jpg_data)
|
||||||
changed = True
|
mp3.tags.add(apic)
|
||||||
if changed and not dry_run:
|
changed = True
|
||||||
sys.stdout.write("Writing %s\n" % mp3_path)
|
if changed and not dry_run:
|
||||||
mp3.save()
|
sys.stdout.write("Writing %s\n" % mp3_path)
|
||||||
|
mp3.save()
|
||||||
|
else:
|
||||||
|
sys.stdout.write("Warning: no tags found for %s\n" % (mp3_path))
|
||||||
|
|
||||||
def main(argv):
|
def main(argv):
|
||||||
base_dir = "."
|
base_dir = "."
|
||||||
|
Loading…
x
Reference in New Issue
Block a user