Skip to content

Instantly share code, notes, and snippets.

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:
mp4 = MP4(str(f))
cover = mp4.get("covr")
if cover:
im =[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
if im.width > 320:
im = im.resize([320, math.floor(320*ratio)])
skip = False
if skip:
imgb = BytesIO()
print("{}x{}".format(im.width, im.height)), format="JPEG")
mp4.tags["covr"] = [MP4Cover(imgb.getvalue())]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment