Skip to content

Instantly share code, notes, and snippets.

@AkashiSN
Last active August 16, 2021 14:10
Show Gist options
  • Save AkashiSN/ea6140e199c755fd4a951e450a0fe7b5 to your computer and use it in GitHub Desktop.
Save AkashiSN/ea6140e199c755fd4a951e450a0fe7b5 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
from mutagen.mp4 import MP4, MP4Cover
from io import BytesIO
from PIL import Image
import pathlib
import math
files = list(pathlib.Path(".").glob("**/*.m4a"))
for f in files:
print(f)
mp4 = MP4(str(f))
cover = mp4.get("covr")
if cover:
im = Image.open(BytesIO(cover[0]))
skip = True
if im.format != "JPEG":
im = im.convert('RGB')
skip = False
ratio = im.height / im.width
if ratio == 1:
if im.width > 300:
im = im.resize([300, 300])
skip = False
else:
if im.width > 320:
im = im.resize([320, math.floor(320*ratio)])
skip = False
if skip:
print("skip")
else:
imgb = BytesIO()
print("{}x{}".format(im.width, im.height))
im.save(imgb, format="JPEG")
mp4.tags["covr"] = [MP4Cover(imgb.getvalue())]
mp4.save()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment