HTML to Video
Export an animation that runs in a browser into an MP4 frame by frame, so the result is smooth, identical on every run, and the sound is perfectly aligned.
Why not just screen-record?
A screen recorder samples whatever the machine managed to draw: dropped frames, uneven timing, the mouse cursor, the page's own buttons, and audio that drifts. This exporter never records. It sets the time itself (t = frame * 1000 / fps), asks the page to draw that exact moment, takes a screenshot, and repeats. The audio is rendered offline by the page and merged at the end.
Route A (default): headless browser capture
Works for any page that exposes the motion contract, and for the bundled reference template without edits.
pip install -r "${CLAUDE_SKILL_DIR}/scripts/requirements.txt"
python -m playwright install chromium # one time, downloads a browser (~150 MB)
# quick look first: PNG stills at chosen times, no video
python "${CLAUDE_SKILL_DIR}/scripts/export_mp4.py" page.html --snapshots 0,2000,5000,9000
# square 1080x1080, 60 fps, with sound
python "${CLAUDE_SKILL_DIR}/scripts/export_mp4.py" page.html -o out-square.mp4 --fps 60
# vertical 1080x1920 (Reels / Shorts / TikTok): the square stage is centred
python "${CLAUDE_SKILL_DIR}/scripts/export_mp4.py" page.html -o out-reel.mp4 --preset vertical --fps 90
Useful options: --scheme dark (page dark mode), --tail-ms 800 (let reverb ring out), --no-audio, --outro (reference template's "draw back in" loop ending), --size 1440x2560 --stage 1440 (custom canvas), --crf 12 (higher quality), --wav-out sound.wav (keep the audio). --help lists all.
The script ends by running verify_mp4.py. Read its report; do not skip it:
python "${CLAUDE_SKILL_DIR}/scripts/verify_mp4.py" out.mp4 --expect-audio --expect-size 1080x1920 --expect-fps 60
What the exporter does for you
- Opens the page in headless Chromium with
requestAnimationFramefrozen, so the page cannot animate on its own. - Injects CSS that hides the UI (
.bar,#hint,[data-export-hide]), centres the stage and resizes it to--stagepixels. - Detects
window.__motion(or the reference template's globals), reads the duration and the "static ranges" (stretches where nothing changes, reused instead of re-drawn). - Renders and screenshots every frame, piping PNGs straight into ffmpeg.
- Renders the audio inside the page (OfflineAudioContext), converts to 16-bit WAV, trims/pads to the exact video length.
- Encodes H.264
yuv420p(BT.709 colour tags,+faststart) and AAC 48 kHz stereo, with a short fade-out so a cut tail does not click.
Make a page exportable
Read references/contract.md. In short, the page needs window.__motion = { duration, render(tMs), audio(lengthMs), staticRanges }, where render is a pure function of time. examples/minimal-contract/index.html at the repo root is a 60-line working example. If the page animates with CSS animations, setTimeout, Date.now() or Math.random(), it must be converted first; the contract file explains how.
Route B: pure-Python re-implementation (fallback)
Use only if a headless browser cannot run (locked-down machine, no Chromium) or the animation must be re-drawn without a browser. You port the timeline, easing, springs and vector shapes into Python and rasterise with Pillow/OpenCV. It is much more work and drifts from the browser's rendering (SVG filters, anti-aliasing). Concepts, pitfalls and the manual MP4-audio muxing technique are in references/route-b-python-port.md. Prefer Route A.
Checklist before delivering
-
verify_mp4.pyshows no FAIL; any WARN is understood (loudness, true peak, A/V length). - Open the MP4 in a normal player and scrub: no UI, no cursor, no black frames, edges crisp.
- Sound: starts on the first visual event, no click at the end, not clipping.
- Right sizes delivered: square for feeds, vertical for Reels/Shorts/TikTok; filenames say which.
- If the user will upload to a social platform, check that platform's current specs (resolution, fps, loudness, length) rather than trusting this file's defaults.
Common failures (details in references/gotchas.md)
| Symptom | Likely cause |
|---|---|
| "No window.__motion contract found" | Page does not expose the contract; add it. |
| Video is one frozen frame | render(t) ignores t, or the page re-renders itself from requestAnimationFrame only. |
| Silent MP4 | audio missing or threw; check the [warning] line. Page audio must be built with OfflineAudioContext. |
| Colours look washed out in some players | Missing BT.709 tags. The exporter sets them; do not re-encode without them. |
Executable doesn't exist from Playwright | Run python -m playwright install chromium. |
| Export takes very long at 90 fps | Add staticRanges for settled intervals; lower --x264-preset to veryfast while testing. |