#!/usr/bin/env python3
"""Render the reload clips, frame by frame (Designer, 12 Nov 2026).
4s seamless loops, 30fps, deviceScaleFactor 2, libx264 crf 14 preset slow yuv420p. No text anywhere."""
import asyncio, shutil, subprocess, sys
from pathlib import Path
from playwright.async_api import async_playwright
D=Path('/workspace/reload-v2'); OUT=D/'out'; FPS=30; SEC=4.0
THEMES=['midnight','amethyst','pearl','champagne']
def ff(*a): subprocess.run(['ffmpeg','-y','-loglevel','error',*a],check=True)
def enc(pat,out,vf=None):
    a=['-framerate',str(FPS),'-i',pat]+(['-vf',vf] if vf else [])+['-c:v','libx264','-pix_fmt','yuv420p','-crf','14','-preset','slow','-movflags','+faststart',str(out)]
    ff(*a); print('WROTE',out,out.stat().st_size)
async def cap(b,q,w,h,tmp):
    shutil.rmtree(tmp,ignore_errors=True); tmp.mkdir(parents=True)
    pg=await b.new_page(viewport={'width':w,'height':h},device_scale_factor=2)
    await pg.emulate_media(reduced_motion='no-preference')
    await pg.goto((D/'demo.html').as_uri()+'?render=1&'+q)
    for i in range(int(SEC*FPS)):
        await pg.evaluate('t=>seek(t)',i/FPS); await pg.screenshot(path=str(tmp/f'f{i:04d}.png'))
    await pg.close(); return str(tmp/'f%04d.png')
async def main():
    OUT.mkdir(exist_ok=True)
    async with async_playwright() as p:
        b=await p.chromium.launch(args=['--font-render-hinting=none','--disable-lcd-text'])
        for d in ['papi','ils']:
            t=Path(f'/tmp/rv2/{d}-grid'); pat=await cap(b,f'design={d}&layout=grid',1081,1921,t)
            enc(pat,OUT/f'{d}-4themes.mp4')
            shutil.copy(t/'f0090.png',OUT/f'{d}-4themes.png')   # t=3.0s: settled, ring on
            for th,(x,y) in zip(THEMES,[(0,0),(1082,0),(0,1922),(1082,1922)]):
                enc(pat,OUT/f'{d}-{th}.mp4',f'crop=1080:1920:{x}:{y}')
            t=Path(f'/tmp/rv2/{d}-icon'); pat=await cap(b,f'design={d}&layout=grid&size=icon',1081,1081,t)
            enc(pat,OUT/f'{d}-icon-4themes.mp4')
            shutil.copy(t/'f0080.png',OUT/f'{d}-icon-4themes.png')
            for th in ['midnight','pearl']:
                t=Path(f'/tmp/rv2/{d}-v-{th}'); pat=await cap(b,f'design={d}&layout=verdicts&theme={th}',1082,540,t)
                enc(pat,OUT/f'{d}-verdicts-{th}.mp4')
                shutil.copy(t/'f0090.png',OUT/f'{d}-verdicts-{th}.png')
        await b.close()
asyncio.run(main())
