Remotion(程序化视频)
在React中构建真正的MP4/GIF/WebM视频。每一帧都是useCurrentFrame()的纯函数,因此输出是确定性的、可滚动浏览、可比较、并且可在CI中渲染。对于模板化和数据驱动的动态图形,它是After Effects的代码优先替代方案。
使用场景
- 从代码渲染MP4/GIF/WebM(社交视频片段、标题卡片、解说视频)。
- 模板化/数据驱动视频:一个合成,通过属性(每个用户、每条记录、CSV/DB的每一行)生成多个输出。
- 必须进行版本控制、代码审查并在没有GUI的情况下在CI中渲染的动态图形。
- 程序化音频同步、根据数据生成动画的图表,或将着色器/Three.js嵌入视频中。
核心技术
帧驱动动画
动画从当前帧派生,绝不使用setState或requestAnimationFrame。interpolate将输入范围映射到输出范围;spring产生物理上自然的运动。
import {useCurrentFrame, useVideoConfig, interpolate, spring} from 'remotion';
export const Title = () => {
const frame = useCurrentFrame();
const {fps} = useVideoConfig();
// 在0-30帧之间淡入,然后保持(clamp防止超出范围)。
const opacity = interpolate(frame, [0, 30], [0, 1], {
extrapolateLeft: 'clamp',
extrapolateRight: 'clamp',
});
// 弹簧入场效果;除以弹簧持续时间是不必要的——直接驱动变换。
const scale = spring({frame, fps, config: {damping: 200, mass: 1, stiffness: 100}});
return <h1 style={{opacity, transform: `scale(${scale})`}}>Hello</h1>;
};
除非需要有意超调,否则始终对interpolate进行clamp——默认情况下它会线性外推到范围之外,这会产生大于1的透明度或负值。
调度:Sequence 和 Series
<Sequence>会偏移时间:子组件看到frame在序列的from处重置为0。使用它在时间线上放置剪辑。<Series>可以连续排列片段,无需手动偏移计算。
import {Sequence, Series, AbsoluteFill} from 'remotion';
export const Timeline = () => (
<AbsoluteFill style={{backgroundColor: 'black'}}>
<Sequence from={0} durationInFrames={60}><Intro /></Sequence>
<Sequence from={60} durationInFrames={90}><Body /></Sequence>
{/* Series自动排序;offset使前一个片段重叠以实现交叉淡入淡出 */}
<Series>
<Series.Sequence durationInFrames={60}><ShotA /></Series.Sequence>
<Series.Sequence durationInFrames={60} offset={-15}><ShotB /></Series.Sequence>
</Series>
</AbsoluteFill>
);
合成注册 + 参数化属性
<Composition>声明id、尺寸、fps、时长和defaultProps。Zod模式使属性类型安全,并可在Studio侧边栏中编辑。
import {Composition} from 'remotion';
import {z} from 'zod';
export const schema = z.object({
title: z.string(),
accent: z.string(),
fps: z.number().default(30),
});
export const Root = () => (
<Composition
id="Promo"
component={Promo}
durationInFrames={150}
fps={30}
width={1080}
height={1920}
schema={schema}
defaultProps={{title: 'Launch', accent: '#5b8cff', fps: 30}}
/>
);
要使时长依赖于数据,可以在Composition上使用calculateMetadata,在渲染前根据属性(例如,行数 × 每行帧数)计算durationInFrames。
音频和节拍同步
import {Audio, staticFile, useCurrentFrame, useVideoConfig} from 'remotion';
const BEATS_SEC = [0.5, 1.0, 1.5, 2.0]; // 离线检测
export const Music = ({children}) => {
const frame = useCurrentFrame();
const {fps} = useVideoConfig();
const t = frame / fps;
const onBeat = BEATS_SEC.some((b) => Math.abs(t - b) < 1 / fps);
return (
<>
<Audio src={staticFile('track.mp3')} />
<div style={{transform: `scale(${onBeat ? 1.06 : 1})`}}>{children}</div>
</>
);
};
离线检测节拍(例如使用web-audio-beat-detector或aubio),并将时间戳烘焙到属性中——切勿在渲染时分析音频,因为无头渲染没有实时音频时钟。
渲染
在基于浏览器的Studio中预览;通过CLI或编程API进行无头渲染。
npx remotion studio # 交互式预览
npx remotion render Promo out/promo.mp4 \
--props='{"title":"Launch","accent":"#f43"}' # 传递参数化属性
npx remotion render Promo out.gif --codec=gif # GIF输出
对于批量/数据驱动流水线,使用@remotion/renderer在Node中渲染(一次性打包,多次渲染)——请参阅参考文件。
交付与验证(渲染静态帧 → MP4)
打包辅助工具(
scripts/):使用scripts/contact-sheet.sh sheet.png f-hook.png f-mid.png f-end.png拼接您的静态帧,然后使用scripts/probe-mp4.sh out.mp4 [WxH] [fps]验证编码。查看scripts/README.md。
Remotion在结构上是帧确定性的——每一帧都是useCurrentFrame()的纯函数,因此你可以在无需任何seek工具的情况下无头渲染任何精确帧(这相当于网页场景中?t=N的重型对应物)。当输出必须是MP4/GIF、必须携带精确的数字/文本、或需要从数据批量生成时,使用此层级;对于轻量级网页动画,请改为交付独立的HTML。
输出约定:
- 一个Remotion项目,包含注册的合成(
<Composition>+ zodschema+defaultProps),所有动画均为帧驱动(无定时器 /Date.now()/Math.random())。 - 可交付物 = 渲染后的
out/*.mp4(以及项目文件,以便用户可以使用新属性/数据重新渲染)。 - 时长依赖于数据?在
calculateMetadata中计算,而不是手动指定。
验证循环——渲染静态帧 → 检查 → 编码。 首先渲染单帧(廉价,无需视频编码),检查它们,只有在帧正确后才编码完整视频。
# 1. 在开始/中间/结尾处渲染帧精确的静态帧(PNG,无头,快速)
npx remotion still Promo out/f-start.png --frame=0
npx remotion still Promo out/f-mid.png --frame=75
npx remotion still Promo out/f-end.png --frame=149 # 最后一帧 = durationInFrames - 1
# 使用你将要交付的相同属性渲染,而不仅仅是defaultProps:
# npx remotion still Promo out/f-mid.png --frame=75 --props='{"title":"Launch"}'
# 2. 检查每个PNG——保真度(符合需求;数字/文本正确)和
# 瑕疵(文本溢出、画布外、裁剪的安全区域、缺少字体、错误的数据绑定)。
# 3. 只有在静态帧检查通过后,再编码视频:
npx remotion render Promo out/promo.mp4 --props='{"title":"Launch"}'
- 使用
npx remotion compositions读取每个合成的durationInFrames/fps并选择结束帧。 - 数据驱动 / 批量:在批量渲染所有行之前,先通过静态帧验证一个代表性的属性集——一次性捕获布局错误,而不是N次。
- 免费获得README演示GIF:
npx remotion render Promo out/demo.gif --codec=gif生成首屏证明剪辑(方向D)。
在完成之前:
npx remotion still在第0帧、中间帧和最后一帧干净地渲染——无错误,无缺失资源/字体。- 在每个检查的帧上,数字/文本精确且在安全区域内。
- 仅帧驱动——无
Date.now()/Math.random()/ 定时器(确定性在CI中保持)。 - 属性是Zod类型化的;交付的属性正确渲染(不仅仅是
defaultProps)。 - 完整的MP4已编码并可播放;(可选)为README渲染了GIF。
快速参考
| 需求 | 使用 |
|---|---|
| 将时间映射为值 | interpolate(frame, [a,b], [x,y], {extrapolateRight:'clamp'}) |
| 自然缓动 | spring({frame, fps, config}) |
| 在时间t放置剪辑 | <Sequence from durationInFrames> |
| 连续镜头 | <Series> |
| 音频 | <Audio src={staticFile(...)} /> |
| 帧 ↔ 秒 | frame / fps |
| 循环值 | frame % period 然后 interpolate |
| 可编辑属性 | <Composition>上的 zod schema |
| CI渲染 | npx remotion render 或 @remotion/renderer |
注意事项
- 切勿使用
Math.random()、Date.now()或动画定时器——它们会破坏确定性。使用Remotion中的random(seed)来获得稳定的每帧随机性。 - 通过
staticFile()加载字体和资源,并使用delayRender/continueRender等待,否则字体会在渲染中途出现。 - 默认的
interpolate会外推——请clamp它。 <Sequence>内部的useCurrentFrame是本地的(从0开始);使用useVideoConfig().durationInFrames来获取绝对时间。
参考文件
references/api-and-patterns.md—interpolate选项和Easing、spring配置配方、Sequence/Series调度、<Audio>+ 节拍同步、带zod和calculateMetadata的参数化defaultProps、CLI标志、程序化@remotion/renderer批量渲染,以及嵌入GLSL着色器/Three.js(@remotion/three)。