from moviepy.editor import VideoFileClip
import cv2 as cv
def face_bbox(frame):
# lightweight cv2.dnn/haar/mediapipe — returns (x,y,w,h)
# ...detector code...
return (x, y, w, h)
clip = VideoFileClip("input.mp4")
W, H = clip.w, clip.h
target_w, target_h = 1080, 1920
def crop_region(t):
frame = clip.get_frame(t)
x,y,w,h = face_bbox(frame)
cx, cy = x+w//2, y+h//2
crop_w = int(H * target_w/target_h) # keep aspect
x0 = max(0, min(cx - crop_w//2, W - crop_w))
return (x0, 0, x0+crop_w, H) # left, top, right, bottom
reframed = clip.fx(lambda gf, t: gf(t)[:, crop_region(t)[1]:crop_region(t)[3],
crop_region(t)[0]:crop_region(t)[2]])
reframed.write_videofile("output_1080x1920.mp4", fps=30, codec="libx264",
audio_codec="aac", preset="medium")