Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/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