Remotion — 用 React 制作视频
来源: remotion-dev/remotion · 43.6k ⭐ · v4.x · 🟢 活跃于 2026 NPM:
remotion·@remotion/cli·@remotion/player·@remotion/lambda文档: https://www.remotion.dev/
1. 核心心智模型
- 视频是一个逐帧渲染的 React 组件序列
- 你获得一个
frame编号(0 → N)和一张空白画布 —— 剩下的就是 CSS / SVG / Canvas / WebGL - Remotion 将你的 React 编译为一系列 PNG/JPEG 帧,然后通过 FFmpeg 编码为 MP4
2. 何时使用
- 程序化视频生成(解释性视频、个性化视频广告、数据驱动视频)
- 年度回顾/统计视频(如 GitHub Unwrapped、Spotify Wrapped)
- 基于文案 + 数据构建的市场营销解释性视频(无需在 AE/Premiere 中手动编辑)
- 从代码生成的开发者工具/教程视频
何时不宜使用
- 真人视频编辑(坚持使用 Premiere / Final Cut)
- 传统动态图形(After Effects 更高效)
- 一次性视频 —— 不值得投入开销
3. 安装(新项目)
npx create-video@latest my-video
cd my-video
npm install
npm start # 打开 Remotion Studio
提供的模板
- Hello World
- TypeScript
- JavaScript
- React 18 / 19
- Audiogram
- Stargazer(GitHub 星星动画)
- Three.js
- Empty
添加到现有项目
npm install remotion @remotion/cli
创建 src/Root.tsx:
import { Composition } from 'remotion';
import { MyVideo } from './MyVideo';
export const RemotionRoot: React.FC = () => {
return (
<Composition
id="MyVideo"
component={MyVideo}
durationInFrames={150}
fps={30}
width={1920}
height={1080}
defaultProps={{ title: 'Hello' }}
/>
);
};
在 remotion.config.ts 中注册根组件:
import { Config } from '@remotion/cli/config';
Config.setEntryPoint('./src/index.ts');
4. 核心概念
<Composition />
定义一个视频:哪个组件渲染、时长、帧率、分辨率、默认 props。
useCurrentFrame()
返回当前帧编号(从 0 开始)。所有动画都基于此:
import { useCurrentFrame, useVideoConfig, interpolate } from 'remotion';
export const MyVideo: React.FC<{ title: string }> = ({ title }) => {
const frame = useCurrentFrame();
const { fps, durationInFrames } = useVideoConfig();
const opacity = interpolate(frame, [0, 30], [0, 1], { extrapolateRight: 'clamp' });
return (
<div style={{ flex: 1, backgroundColor: '#0a0a0f', color: 'white', opacity, display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
<h1 style={{ fontSize: 120 }}>{title}</h1>
</div>
);
};
interpolate(frame, [from, to], [startVal, endVal], options)
将帧编号映射到任何值(位置、透明度、缩放、颜色等)。
spring(...)
自然运动曲线:
import { spring, useCurrentFrame, useVideoConfig } from 'remotion';
const frame = useCurrentFrame();
const { fps } = useVideoConfig();
const scale = spring({
frame,
fps,
config: { damping: 100, stiffness: 200, mass: 0.5 },
});
<div style={{ transform: `scale(${scale})` }}>Content</div>
<Sequence from={f} durationInFrames={n}>
将组件偏移到从帧 f 开始:
import { Sequence } from 'remotion';
<Sequence from={0} durationInFrames={60}><Intro /></Sequence>
<Sequence from={60} durationInFrames={120}><Scene1 /></Sequence>
<Sequence from={180} durationInFrames={90}><Outro /></Sequence>
<Audio src={...} />
import { Audio } from 'remotion';
import bgMusic from './music.mp3';
<Audio src={bgMusic} volume={0.5} />
<Img src={...} /> / <Video src={...} />
在时间线中嵌入图片/视频剪辑。
staticFile('...')
引用 public/ 中的文件 —— Remotion 处理路径解析:
import { staticFile } from 'remotion';
<Img src={staticFile('logo.svg')} />
5. 渲染
本地渲染 (CLI)
npx remotion render MyVideo out/video.mp4
npx remotion render MyVideo out/video.mp4 --props='{"title":"Hello"}'
npx remotion render MyVideo out/video.mp4 --quality=high --codec=h265
Remotion Studio(预览 + 时间线)
npm start # 打开 http://localhost:3000
Remotion Player(嵌入到你的 React 应用中)
import { Player } from '@remotion/player';
import { MyVideo } from './MyVideo';
<Player
component={MyVideo}
durationInFrames={150}
fps={30}
compositionWidth={1920}
compositionHeight={1080}
inputProps={{ title: 'Hello' }}
controls
style={{ width: '100%' }}
/>
Remotion Lambda(云端渲染)
用于批量/服务端渲染:
npm install @remotion/lambda
设置 AWS Lambda 函数,部署 Remotion:
npx remotion lambda functions deploy
npx remotion lambda sites create
npx remotion lambda render MyVideo --input-props='{"title":"User 123"}'
6. 示例 —— 数据驱动视频
生成个性化的“年度回顾”视频:
import { Composition } from 'remotion';
import { YearInReview } from './YearInReview';
export const RemotionRoot = () => (
<Composition
id="Recap"
component={YearInReview}
durationInFrames={8 * 30} // 8 秒,30fps
fps={30}
width={1080}
height={1920} // 竖屏(移动端)
defaultProps={{
user: { name: 'Alice', stats: { commits: 1284, prs: 42, reviews: 89 } },
}}
/>
);
为每个用户渲染:
for user in $(cat users.json); do
npx remotion lambda render Recap --input-props="$user"
done
7. 定价/许可
| 层级 | 适用对象 | 价格 |
|---|---|---|
| 个人/团队 ≤ 3 人 | 个人、小公司 | 免费(类似 MIT) |
| 公司 ≤ 10 名员工 | 小型创业公司 | 免费 |
| 大型公司 | > 10 名员工 | 每月 $100 + 每席位 $25 |
| Editor Starter | 使用 Remotion 构建自己的视频编辑器 | 一次性 $600 |
| Lambda | 云端渲染 | 按次计费(每分钟视频约几美分) |
请参见 https://www.remotion.dev/docs/license 获取权威条款。
8. 禁止事项
- ❌ 切勿将 Remotion 用于实时视频播放引擎 —— 它设计用于渲染到文件,而非直播
- ❌ 切勿将大于 100MB 的实际视频文件放入 bundle —— 对大型媒体使用
staticFile()并单独托管 - ❌ 切勿在 Remotion 组件中使用
setTimeout、setInterval或requestAnimationFrame—— 确定性仅来自useCurrentFrame() - ❌ 切勿在渲染内部获取数据 —— 预先获取并通过
inputProps传递 - ❌ 切勿为每个组件渲染不必要的帧数 —— 每帧都会消耗渲染时间
- ❌ 团队超过 3 人时,切勿在未经许可的情况下进行商业 Lambda 渲染
- ❌ 切勿直接使用
Math.random()—— 输出将不具有确定性。请使用 Remotion 带种子的random()函数 - ❌ 切勿使用 CSS transitions 进行动画 —— Remotion 逐帧渲染;CSS transitions 不适用。请使用
interpolate()+ frame
9. 预检清单
- [ ] 已安装 remotion + @remotion/cli
- [ ] src/Root.tsx 包含定义视频的 <Composition />
- [ ] 所有动画由 useCurrentFrame() 驱动 —— 无 setTimeout/setInterval
- [ ] 随机值使用 Remotion 的 random() 并带种子(而非 Math.random)
- [ ] 字体加载:使用 useFont 或 <link> 在 <head> 中
- [ ] 通过 <Audio> 使用音频文件,并设置适当的音量/修剪
- [ ] 生产环境:通过 Lambda 或 remotion render CLI 渲染
- [ ] 检查定价 —— 选择适合团队规模的层级
- [ ] 为每个 Composition 的 inputProps 添加类型(TypeScript)
- [ ] 在完全渲染前,先测试渲染一个简短预览
10. 适应范围
完全取决于视频的设计 —— Remotion 是一个框架,而非视觉库。
11. 与 DesignDNA 集成
Remotion 与 DesignDNA 的品牌 DNA 配合良好:使用品牌的 DESIGN.md 令牌(颜色、字体、间距)作为生成视频的视觉语言。示例:
import { tokens } from '../design-md/stripe/tokens';
<div style={{ color: tokens.colors['stripe-purple'], fontFamily: tokens.typography.families[0] }}>
Stripe 品牌开场
</div>
你的“年度回顾”视频因此与你的品牌视觉识别相匹配。
12. 替代方案
| 场景 | 替代方案 |
|---|---|
| 传统非代码视频 | Adobe After Effects / Premiere |
| 数据可视化视频 | D3 + 录屏 或 Remotion + D3 |
| 动画 React 组件(非完整视频) | Framer Motion · Motion · React Bits(参见技能) |
| 交互式网页动画 | Lottie · GSAP |