support for png covers with transparency, other fixes

This commit is contained in:
Lynne Megido 2020-10-17 13:34:56 +10:00
parent 83458f9f30
commit 2f62c53a9f
Signed by: lynnesbian
GPG key ID: F0A184B5213D9F90

29
bcao.py
View file

@ -62,7 +62,8 @@ if not os.path.exists(args.zip):
die(f"Couldn't find {args.zip}.", 2)
log("Extracting...")
tmp = tempfile.TemporaryDirectory()
tmpd = tempfile.TemporaryDirectory()
tmp = tmpd.name
cover = None
song_names = []
@ -89,6 +90,10 @@ log("Resizing album art to embed in songs...")
with Image.open(os.path.join(tmp, cover)) as image:
temp_cover = os.path.join(tmp, "cover-lq.jpg")
if image.mode in ["RGBA", "P"]:
# remove alpha channel
image = image.convert("RGB")
image.save(temp_cover, quality=85, optimize=True)
image_smol = image
@ -107,14 +112,14 @@ with open(temp_cover, 'r+b') as cover_file:
with Image.open(temp_cover) as image:
if song_format == "ogg":
# i hate this
cover = Picture()
cover.data = data
cover.type = mutagen.id3.PictureType.COVER_FRONT
cover.mime = "image/jpeg"
cover.width = image.size[0]
cover.height = image.size[1]
cover.depth = image.bits
cover = base64.b64encode(cover.write()).decode("ascii")
embed_cover = Picture()
embed_cover.data = data
embed_cover.type = mutagen.id3.PictureType.COVER_FRONT
embed_cover.mime = "image/jpeg"
embed_cover.width = image.size[0]
embed_cover.height = image.size[1]
embed_cover.depth = image.bits
embed_cover = base64.b64encode(embed_cover.write()).decode("ascii")
else:
log(f"Format {song_format} is not fully supported - cover images will not be modified", 1)
@ -131,7 +136,7 @@ for song in song_names:
album = get_tag(m, "album")
# embed cover art
if song_format == "ogg":
m["metadata_block_picture"] = [cover]
m["metadata_block_picture"] = [embed_cover]
m.save()
if len(artists) > 1 and "Various Artists" not in artists:
@ -160,8 +165,8 @@ log(f"Moving files to {destination}...")
os.makedirs(destination, exist_ok=True)
for source_name, dest_name in songs.items():
shutil.move(os.path.join(tmp, source_name), os.path.join(destination, dest_name))
shutil.move(os.path.join(tmp, "cover.jpg"), os.path.join(destination, "cover.jpg"))
shutil.move(os.path.join(tmp, cover), os.path.join(destination, cover))
tmp.cleanup()
tmpd.cleanup()
log("Done!")