--- name: remotion-video description: Best practices for building programmatic videos in React with Remotion v4+. Use when the user mentions "Remotion", "video in React", "programmatic video", "render a video from code", "generate an MP4 from React components", or wants data-driven, templated, or automated video production. Covers the frame-based mental model, compositions, deterministic rendering, interpolate/spring animation, Sequence/Series timing, assets and audio, performance, and CLI rendering. Not for browser animation of live websites — for animating DOM in a running web page (scroll effects, hover states, page transitions), use a GSAP skill instead; Remotion renders video files. --- # Remotion Video Remotion turns React components into rendered video files. Every rule below exists because rendering happens frame by frame in headless browser instances: anything non-deterministic or time-based (instead of frame-based) produces flicker, desync, or broken renders. ## Core mental model - A video is a pure function of the current frame: `frame -> pixels`. The same frame number must always produce the same image, on any machine, at any time, in any order. - Read the current frame with `useCurrentFrame()`. Read fps, duration, width, height with `useVideoConfig()`. - Never animate with wall-clock time, `setTimeout`, `requestAnimationFrame`, CSS transitions, or CSS animations. During rendering there is no continuous time — frames are captured independently and possibly in parallel. All motion derives from `frame`. ```tsx const frame = useCurrentFrame(); const { fps, durationInFrames, width, height } = useVideoConfig(); const opacity = interpolate(frame, [0, 30], [0, 1], { extrapolateLeft: "clamp", extrapolateRight: "clamp", }); ``` ## Compositions Register videos in the root file with ``: ```tsx ``` - `id` must be unique and is what you pass to the render CLI. - Express durations in terms of fps (`fps * seconds`), never magic frame counts. - Use `` for single-frame outputs (thumbnails, OG images). - For data-driven videos, use `calculateMetadata` on the composition to fetch data once and derive `durationInFrames` / props from it — do not fetch inside components per frame. ```tsx { const data = await fetchEpisode(props.episodeId); return { props: { ...props, data }, durationInFrames: Math.ceil(data.durationSeconds * 30), }; }} ... /> ``` ## Determinism rules - No `Math.random()`. Use `random(seed)` from `remotion` — deterministic per seed. Give each element a stable seed (`random(`particle-${i}`)`). - No `Date.now()`, `new Date()`, `performance.now()` influencing visuals. - No CSS `transition` or `animation` properties. Set styles directly from interpolated values each frame. - No `useEffect`-driven animation state (counters, tick loops). Derive everything from `useCurrentFrame()`. - Data fetching inside a component must be wrapped in `delayRender()` / `continueRender()`: ```tsx const [handle] = useState(() => delayRender("fetch data")); useEffect(() => { fetchData() .then((d) => { setData(d); continueRender(handle); }) .catch((err) => cancelRender(err)); }, []); ``` Prefer `calculateMetadata` over in-component fetching: it runs once, not once per render instance, and avoids flicker from divergent responses across the parallel browser tabs used for rendering. ## interpolate() and spring() - Always clamp unless overshoot is intentional: `{ extrapolateLeft: "clamp", extrapolateRight: "clamp" }`. Unclamped interpolations are the most common source of elements flying off-screen before/after their animation window. - Input ranges must be monotonically increasing. Multi-stop ranges express keyframes: `interpolate(frame, [0, 20, 80, 100], [0, 1, 1, 0])` = fade in, hold, fade out. - Add easing via `Easing` from `remotion`: `{ easing: Easing.out(Easing.cubic) }`. - `spring()` produces natural motion from 0 to 1; pass `fps` from `useVideoConfig()`: ```tsx const scale = spring({ frame, fps, config: { damping: 200 } }); ``` - `damping: 200` = smooth, no overshoot (good default for UI). Lower damping = bouncier. Use `durationInFrames` on spring (v4) to fit it into a fixed slot. - Combine: drive `interpolate` with a spring's output to map 0-1 onto any range. See `references/animation-patterns.md` for ready-made recipes (fade, slide, scale-in, typewriter, counter, staggered lists). ## Sequencing - `` shifts its children's local frame to 0 at frame N and unmounts them outside the window. Inside a Sequence, `useCurrentFrame()` returns the local frame — write child animations starting at 0. - `` with `` chains scenes back-to-back without manual offset arithmetic. Negative `offset` overlaps scenes. - Stagger repeated elements by rendering each in its own Sequence or by offsetting the frame per index: ```tsx {items.map((item, i) => ( ))} ``` - Use `premountFor={frames}` on Sequence when children load assets, so they mount (invisible) before appearing and are ready on their first visible frame. ## Assets - Put files in `public/` and reference them with `staticFile("name.png")`. Never use relative import paths or absolute filesystem paths for media. - Use Remotion's media components, never bare HTML tags: ``, ``, `