This commit is contained in:
quackerd 2023-06-05 06:57:58 -04:00
parent fdeb1c4289
commit 696011bcbd
3 changed files with 57 additions and 24 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 423 KiB

View File

@ -42,50 +42,60 @@ class MObject:
class MMP3Object(MObject): class MMP3Object(MObject):
_id3 : ID3 _id3 : ID3
def __init__(self, file : bytes): def __init__(self, file : bytes):
super().__init__(self, file) super().__init__(file)
self._id3 = ID3() self._id3 = ID3()
self._id3.load(self._file_io, translate = True) self._id3.load(self._file_io, translate = True)
return return
def get_title(self) -> str: def get_title(self) -> str:
return self._id3.get("TIT2", default = TIT2(text = None)).text return self._id3.get("TIT2", default = TIT2(text = ""))
def set_title(self, val : str) -> None: def set_title(self, val : str) -> None:
self._id3.delall("TIT2") self._id3.delall("TIT2")
self._id3.add(TIT2(text = val)) self._id3.add(TIT2(text = val))
self._id3.save()
self._file_io.seek(0)
self._id3.save(self._file_io)
def get_album(self) -> str: def get_album(self) -> str:
return self._id3.get("TALB", default = TALB(text = None)).text return self._id3.get("TALB", default = TALB(text = ""))
def set_album(self, val : str) -> None: def set_album(self, val : str) -> None:
self._id3.delall("TALB") self._id3.delall("TALB")
self._id3.add(TALB(text = val)) self._id3.add(TALB(text = val))
self._id3.save()
self._file_io.seek(0)
self._id3.save(self._file_io)
def get_artist(self) -> str: def get_artist(self) -> str:
return self._id3.get("TPE1", default = TPE1(text = None)).text return self._id3.get("TPE1", default = TPE1(text = ""))
def set_artist(self, val : str): def set_artist(self, val : str):
self._id3.delall("TPE1") self._id3.delall("TPE1")
self._id3.add(TPE1(text = val)) self._id3.add(TPE1(text = val))
self._id3.save()
self._file_io.seek(0)
self._id3.save(self._file_io)
def get_cover(self) -> bytes: def get_cover(self) -> bytes:
cover = self._id3.get("APIC", default=APIC(encoding = Encoding.UTF8, mime = "image/jpeg", type=PictureType.COVER_FRONT, desc=u"Cover", data = None)) cover = self._id3.getall("APIC")
return cover.data if (cover == None or len(cover) == 0):
return None
return cover[0].data
def set_cover(self, val : bytes, png : bool = False) -> None: def set_cover(self, val : bytes, png : bool = False) -> None:
self._id3.delall("APIC") self._id3.delall("APIC")
self._id3.add(APIC(encoding = Encoding.UTF8, mime = "image/png" if png else "image/jpeg", type=PictureType.COVER_FRONT, desc=u"Cover", data = val)) self._id3.add(APIC(encoding = Encoding.UTF8, mime = "image/png" if png else "image/jpeg", type=PictureType.COVER_FRONT, desc=u"Cover", data = val))
self._id3.save()
self._file_io.seek(0)
self._id3.save(self._file_io)
class MFLACObject(MObject): class MFLACObject(MObject):
_flac : FLAC _flac : FLAC
def __init__(self, file : bytes): def __init__(self, file : bytes):
super().__init__(self, file) super().__init__(file)
self._flac = FLAC(self._file_io) self._flac = FLAC(self._file_io)
return return
@ -95,21 +105,27 @@ class MFLACObject(MObject):
def set_title(self, val : str) -> None: def set_title(self, val : str) -> None:
self._flac["TITLE"] = val self._flac["TITLE"] = val
self._flac.save()
self._file_io.seek(0)
self._flac.save(self._file_io)
def get_album(self) -> str: def get_album(self) -> str:
return self._flac.get("ALBUM", default = None) return self._flac.get("ALBUM", default = None)
def set_album(self, val : str) -> None: def set_album(self, val : str) -> None:
self._flac["ALBUM"] = val self._flac["ALBUM"] = val
self._flac.save()
self._file_io.seek(0)
self._flac.save(self._file_io)
def get_artist(self) -> str: def get_artist(self) -> str:
return self._flac.get("ARTIST", default = None) return self._flac.get("ARTIST", default = None)
def set_artist(self, val : str): def set_artist(self, val : str):
self._flac["ARTIST"] = val self._flac["ARTIST"] = val
self._flac.save()
self._file_io.seek(0)
self._flac.save(self._file_io)
def get_cover(self) -> bytes: def get_cover(self) -> bytes:
pics = self._flac.pictures pics = self._flac.pictures
@ -126,15 +142,17 @@ class MFLACObject(MObject):
pic.desc = "Cover" pic.desc = "Cover"
pic.data = val pic.data = val
self._flac.add_picture(pic) self._flac.add_picture(pic)
self._flac.save()
self._file_io.seek(0)
self._flac.save(self._file_io)
def create_mobject(filepath : str) -> MObject: def create_mobject(filepath : str) -> MObject:
fp = Path(filepath) fp = Path(filepath)
with open(fp, "rb") as f: with open(fp, "rb") as f:
buf = f.read() buf = f.read()
if (fp.suffix == "mp3"): if (fp.suffix == ".mp3"):
return MMP3Object(buf) return MMP3Object(buf)
if (fp.suffix == "flac"): if (fp.suffix == ".flac"):
return MFLACObject(buf) return MFLACObject(buf)
return None return None

View File

@ -8,7 +8,10 @@ import mobject
total_proc = 0 total_proc = 0
total_succ = 0 total_succ = 0
total_error = 0 total_error = 0
total_missing_cover = 0 total_cover_exists = 0
total_cover_written = 0
cover_missing = []
errors = []
input_dir = None input_dir = None
output_dir = None output_dir = None
@ -92,7 +95,7 @@ def load_cover_arts():
f = os.path.join(cover_dir, f) f = os.path.join(cover_dir, f)
print("Processing cover art - " + f) print("Processing cover art - " + f)
with open(f,'rb') as img: with open(f,'rb') as img:
cover_arts.append((artist, album, title, img.read(), os.path.basename(f))) cover_arts.append((artist, album, title, img.read(), os.path.basename(f), f))
print(" Loading cover art - " + f + " Artist: " + artist + " Album: " + album + " Title: " + title) print(" Loading cover art - " + f + " Artist: " + artist + " Album: " + album + " Title: " + title)
except Exception as e: except Exception as e:
print(" Skipping due to Exception - " + str(e)) print(" Skipping due to Exception - " + str(e))
@ -108,7 +111,8 @@ def usage():
-c cover : cover arts page\n") -c cover : cover arts page\n")
def process_file(f: str, odir: str): def process_file(f: str, odir: str):
global total_missing_cover global total_cover_exists
global total_cover_written
global total_succ global total_succ
global total_error global total_error
global total_proc global total_proc
@ -138,12 +142,14 @@ def process_file(f: str, odir: str):
cover_idx = find_best_cover(artist, album, title) cover_idx = find_best_cover(artist, album, title)
if (cover_idx >= 0): if (cover_idx >= 0):
mobj.set_cover(cover_arts[cover_idx][3]) mobj.set_cover(cover_arts[cover_idx][3])
print(" Cover: <None> -> <New>") print(f" Cover: \"{cover_arts[cover_idx][5]}\"")
total_cover_written += 1
else: else:
total_missing_cover += 1 print(" Cover: <Missing>")
print(" Cover: <None> -> <None>") cover_missing.append(f)
else: else:
print(" Cover: <Exists>") print(" Cover: <Exists>")
total_cover_exists += 1
target_dir = os.path.join(odir, artist, album) target_dir = os.path.join(odir, artist, album)
os.makedirs(target_dir, exist_ok = True) os.makedirs(target_dir, exist_ok = True)
@ -157,6 +163,7 @@ def process_file(f: str, odir: str):
except Exception as e: except Exception as e:
total_error += 1 total_error += 1
errors.append(f)
print(" Skipping due to exception - %s" % str(e)) print(" Skipping due to exception - %s" % str(e))
print("") print("")
@ -205,6 +212,14 @@ def main():
load_cover_arts() load_cover_arts()
process_directory(input_dir, output_dir) process_directory(input_dir, output_dir)
print("\nSummary - Total Files: " + str(total_proc) + " Total Errors: " + str(total_error) + " Total Missing Covers: " + str(total_missing_cover)) print("\nSummary - Files: " + str(total_proc) + ". Processed: " + str(total_succ) + ". Cover Exists: " + str(total_cover_exists) + ". Cover Written: " + str(total_cover_written))
if (len(errors) > 0):
print(f"Errors: {len(errors)}")
for f in errors:
print(f" {f}")
if (len(cover_missing) > 0):
print(f"Cover Missing: {len(cover_missing)}")
for f in cover_missing:
print(f" {f}")
main() main()