consistent filename

This commit is contained in:
quackerd 2020-09-30 15:28:32 -04:00
parent 6db22f5987
commit 7d5fdc7284
Signed by: d
GPG Key ID: 590A22374D0B819F
2 changed files with 37 additions and 17 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 842 KiB

After

Width:  |  Height:  |  Size: 1.1 MiB

View File

@ -1,6 +1,6 @@
import os
from mutagen.easyid3 import EasyID3
from mutagen.id3 import ID3, APIC
from mutagen.id3 import ID3, APIC, TIT2, TPE1, TALB
import getopt
import sys
import shutil
@ -15,7 +15,7 @@ output_dir = None
cover_dir = None
cover_arts = []
UNKNOWN_ARTIST_NAME = "Unknown Artists"
UNKNOWN_ARTIST_NAME = "Unknown Artist"
UNKNOWN_ALBUM_NAME = "Unknown Album"
WILDCARD_MATCH_SCORE = 1
@ -67,6 +67,17 @@ def find_best_cover(artist: str, album: str, title: str):
return best_idx
def parse_triple(f: str):
triple = os.path.splitext(os.path.basename(f))[0].split("_")
album = ""
title = ""
artist = triple[0]
if len(triple) >= 2:
album = triple[1]
if len(triple) >= 3:
title = triple[2]
return artist, album, title
def load_cover_arts():
global cover_arts
@ -77,16 +88,9 @@ def load_cover_arts():
files = os.listdir(cdir)
for f in files:
try:
triple = os.path.splitext(f)[0].split("_")
artist, album, title = parse_triple(f)
f = os.path.join(cover_dir, f)
print("Processing cover art - " + f)
artist = triple[0]
album = ""
title = ""
if len(triple) >= 2:
album = triple[1]
if len(triple) >= 3:
title = triple[2]
with open(f,'rb') as img:
cover_arts.append((artist, album, title, img.read(), os.path.basename(f)))
print(" Loading cover art - " + f + " Artist: " + artist + " Album: " + album + " Title: " + title)
@ -113,18 +117,22 @@ def process_file(f: str, odir: str):
try:
print("Processing file - " + f)
total_proc += 1
id3 = ID3(f)
album = str(id3.get("TALB") if id3.get("TALB") != None else "")
artist, album, title = parse_triple(f)
# id3 = ID3(f)
# album = str(id3.get("TALB") if id3.get("TALB") != None else "")
if album == "":
album = UNKNOWN_ALBUM_NAME
artist = str(id3.get("TPE1") if id3.get("TPE1") != None else "")
# artist = str(id3.get("TPE1") if id3.get("TPE1") != None else "")
if artist == "":
artist = UNKNOWN_ARTIST_NAME
title = str(id3.get("TIT2") if id3.get("TIT2") != None else "")
if artist == "":
# title = str(id3.get("TIT2") if id3.get("TIT2") != None else "")
if title == "":
raise Exception("No title")
print(" Artist: " + artist + " Album: " + album + " Title: " + title)
@ -137,16 +145,28 @@ def process_file(f: str, odir: str):
print(" Copied file to - " + target_f)
cover_idx = find_best_cover(artist, album, title)
# write id3 tags of the target file
id3 = ID3(target_f)
# write title
id3.delall("TIT2")
id3.add(TIT2(text = title))
# write album
id3.delall("TALB")
id3.add(TALB(text = album))
# write artist
id3.delall("TPE1")
id3.add(TPE1(text = artist))
if cover_idx >= 0:
# install cover
id3 = ID3(target_f)
id3.delall("APIC")
id3.add(APIC(3, "image/jpeg", 3, "Front cover", cover_arts[cover_idx][3]))
id3.save(v2_version=3)
total_cover += 1
print(" Written cover art - " + cover_arts[cover_idx][4])
id3.save(v2_version=3)
total_succ += 1
except Exception as e: