---
name: ferret-recording
description: Use to film a running Ferret game in real time (tools/record): canvas and the game's sound through the browser into ffmpeg, a fragmented MP4 you can watch while it is written, a live window, scripted drives. Not for frame-exact capture (captureFrames) or in-game recording (SceneRecorder).
---

# Filming the game in real time

## When to use
You need to see what a game does over time (motion, camera, sound, a stall) and cannot wait for frame-by-frame capture; an agent or the owner wants to watch while it records.

## The model
`tools/record/record.mjs` opens the page in Edge on the NVIDIA GPU, asks the browser to record the canvas as it is drawn (`captureStream` + `MediaRecorder`), and pipes the chunks into ffmpeg (h264_nvenc, AAC) which writes a fragmented MP4. The game runs at its own speed; the film is what it showed. Frames arrive only when the canvas changes, so a stalled game shows as a held picture.
Sound: `prepare(page)` (before `goto`) taps every `AudioContext` the page makes; the film has an audio track from the first frame and the game's context is mixed in when the game makes it (at its first key; the recorder presses ScrollLock every 2 s until it exists). A hall that plays nothing gives a silent track (-91 dB): that is the game, not the recorder.

## Steps
1. CLI: `node tools/record/record.mjs <url> <out.mp4> [--seconds N] [--fps 30] [--size 1280x720] [--bitrate 8] [--watch] [--no-audio] [--cpu] [--script steps.mjs]`. Ctrl+C ends it. `--watch` opens ffplay on the live stream (a window on the screen: use it only when a person watches).
2. Driving the game: `--script file.mjs` with `export default async (page, rec) => {}`; the film ends when it returns. Wait for the game's own ready flag inside it, and move the player by the game's debug hooks (see `ferret-debugging`).
3. From a Playwright test: `await prepare(page); await page.goto(url); const rec = await startRecording(page, { out }); ...; const { file, seconds, bytes } = await rec.stop();`. `rec.stats()` gives the seconds and bytes so far.
4. Look at it: the file plays while it is still written; a frame is `ffmpeg -ss 12 -i f.mp4 -frames:v 1 f.png`; the level of sound is `ffmpeg -i f.mp4 -af volumedetect -vn -f null -`.
5. Options of `startRecording`: `out`, `fps`, `bitrate` (Mbit/s), `audio`, `watch`, `cpu` (libx264), `canvas` (selector), `timeslice`, `unlock` (key, or false), `keepRaw` (the browser's own stream saved as it came, for finding out what ffmpeg was given).

## What usually goes wrong
- `prepare` called after `goto`: no sound is tapped (the contexts exist already).
- A film that starts before the game is ready is long and still: the loading screen does not change the canvas; start after the ready flag when the loading is not wanted.
- ffmpeg "Invalid data" and zero chunks: the audio track had nothing playing into it; the recorder keeps a near-silent source for that, do not remove it.
- Many browsers at once: one film per machine at a time unless the machine's load allows it.
- Windows that cover the person's work: record headless (the default), `--watch` only on request.

## Check afterwards
`ffprobe` shows h264 and aac with the expected size; a frame taken from the middle shows the game, not the loading screen.

## Files

- `reference/api-media.md` — every export of `@ferret/media` with signature and doc comment
- `reference/api-media-2.md` — every export of `@ferret/media` with signature and doc comment
