import json, subprocess, os, time from playwright.sync_api import sync_playwright FPS = 30 P = os.environ.get("PD","parts") tm = json.load(open(__import__("os").environ.get("TM","timing.json"))) sc = tm["scenes"] bounds = [0] + [round(s["end"] * FPS) for s in sc] os.makedirs(P+"", exist_ok=True) t0 = time.time() with sync_playwright() as p: b = p.chromium.launch(); pg = b.new_page(viewport={"width": 1080, "height": 1920}) pg.goto("file://" + os.path.abspath(os.environ.get("PAGE","page.html"))); pg.wait_for_function("window.READY===true") for i, s in enumerate(sc): tmp, out = f"{P}/{i:02d}.tmp.mp4", f"{P}/{i:02d}.mp4" ff = subprocess.Popen(["ffmpeg", "-y", "-loglevel", "error", "-f", "image2pipe", "-framerate", str(FPS), "-c:v", "mjpeg", "-i", "-", "-c:v", "libx264", "-preset", "veryfast", "-crf", "18", "-pix_fmt", "yuv420p", "-r", str(FPS), tmp], stdin=subprocess.PIPE) for f in range(bounds[i], bounds[i + 1]): pg.evaluate(f"render({f / FPS})") ff.stdin.write(pg.screenshot(type="jpeg", quality=92)) ff.stdin.close() if ff.wait() == 0: os.rename(tmp, out) print(s["id"], bounds[i + 1] - bounds[i], "frames", round(time.time() - t0), "s", flush=True) b.close() with open(P+"/list.txt", "w") as f: for i in range(len(sc)): f.write(f"file '{i:02d}.mp4'\n")