Remotion 视频工具包 是做什么的?
概述
使用 React 和 Remotion 以编程方式创建视频。将可复用的视频模板构建为 React 组件,通过 CLI 或云基础设施渲染为 MP4/WebM 格式,并大规模生成个性化视频。非常适合自动化社交媒体内容、动画数据可视化和动态视频生产流水线。
操作说明
当用户请求创建程序化视频时,确定任务类型:
任务 A:设置 Remotion 项目
# 创建一个新的 Remotion 项目
npx create-video@latest my-video
cd my-video
npm start # 在 http://localhost:3000 打开 Remotion Studio
项目结构:
my-video/
src/
Root.tsx # 注册所有合成
MyComposition.tsx # 你的视频组件
remotion.config.ts # 渲染设置
任务 B:创建视频合成
每个 Remotion 视频都是一个 React 组件,使用 useCurrentFrame() 和 useVideoConfig():
import { AbsoluteFill, useCurrentFrame, useVideoConfig, interpolate, spring } from "remotion";
interface MyVideoProps {
title: string;
subtitle: string;
bgColor: string;
}
export const MyVideo: React.FC<MyVideoProps> = ({ title, subtitle, bgColor }) => {
const frame = useCurrentFrame();
const { fps } = useVideoConfig();
// 标题滑入动画
const titleY = spring({ frame, fps, from: -100, to: 0, durationInFrames: 30 });
// 字幕在 20 帧后淡入
const subtitleOpacity = interpolate(frame, [20, 40], [0, 1], {
extrapolateRight: "clamp",
});
return (
<AbsoluteFill style={{ backgroundColor: bgColor, justifyContent: "center", alignItems: "center" }}>
<h1 style={{ fontSize: 80, color: "white", transform: `translateY(${titleY}px)` }}>
{title}
</h1>
<p style={{ fontSize: 36, color: "rgba(255,255,255,0.8)", opacity: subtitleOpacity }}>
{subtitle}
</p>
</AbsoluteFill>
);
};
在 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 World", subtitle: "Made with Remotion", bgColor: "#1a1a2e" }}
/>
);
};
任务 C:添加 TikTok 风格动画字幕
import { AbsoluteFill, useCurrentFrame, Sequence } from "remotion";
interface CaptionWord {
text: string;
startFrame: number;
durationInFrames: number;
}
const captions: CaptionWord[] = [
{ text: "这", startFrame: 0, durationInFrames: 15 },
{ text: "太", startFrame: 15, durationInFrames: 10 },
{ text: "棒了!", startFrame: 25, durationInFrames: 20 },
];
export const CaptionOverlay: React.FC = () => {
const frame = useCurrentFrame();
const activeWord = captions.find(
(c) => frame >= c.startFrame && frame < c.startFrame + c.durationInFrames
);
return (
<AbsoluteFill style={{ justifyContent: "flex-end", alignItems: "center", paddingBottom: 120 }}>
{activeWord && (
<div style={{
fontSize: 64,
fontWeight: "bold",
color: "white",
textShadow: "2px 2px 8px rgba(0,0,0,0.8)",
backgroundColor: "rgba(0,0,0,0.5)",
padding: "8px 24px",
borderRadius: 12,
}}>
{activeWord.text}
</div>
)}
</AbsoluteFill>
);
};
任务 D:渲染视频
# 本地通过 CLI 渲染
npx remotion render MyVideo out/video.mp4
# 使用自定义属性渲染
npx remotion render MyVideo out/video.mp4 --props='{"title":"自定义标题"}'
# 渲染单帧(用于缩略图)
npx remotion still MyVideo out/thumbnail.png --frame=45
# 指定编码器和质量渲染
npx remotion render MyVideo out/video.mp4 --codec=h264 --crf=18
用于大规模批量渲染:
# 在 AWS Lambda 上渲染
npx remotion lambda render MyVideo --props='{"title":"视频 1"}'
# 在 Google Cloud Run 上渲染
npx remotion cloudrun render MyVideo --props='{"title":"视频 1"}'
任务 E:动画数据可视化
import { AbsoluteFill, useCurrentFrame, interpolate } from "remotion";
interface DataPoint { label: string; value: number; color: string; }
export const BarChart: React.FC<{ data: DataPoint[] }> = ({ data }) => {
const frame = useCurrentFrame();
const maxVal = Math.max(...data.map((d) => d.value));
return (
<AbsoluteFill style={{ padding: 80, justifyContent: "flex-end", backgroundColor: "#0f0f23" }}>
<div style={{ display: "flex", alignItems: "flex-end", gap: 20, height: "70%" }}>
{data.map((point, i) => {
const height = interpolate(frame, [i * 10, i * 10 + 30], [0, (point.value / maxVal) * 100], {
extrapolateLeft: "clamp",
extrapolateRight: "clamp",
});
return (
<div key={i} style={{ flex: 1, textAlign: "center" }}>
<div style={{
height: `${height}%`,
backgroundColor: point.color,
borderRadius: "8px 8px 0 0",
transition: "height 0.3s",
}} />
<p style={{ color: "white", marginTop: 12, fontSize: 24 }}>{point.label}</p>
</div>
);
})}
</div>
</AbsoluteFill>
);
};
示例
示例 1:个性化欢迎视频
用户请求: “为每位新用户生成包含其姓名的欢迎视频”
# 从 JSON 用户列表生成视频
for user in $(jq -r '.[] | @base64' users.json); do
name=$(echo "$user" | base64 -d | jq -r '.name')
npx remotion render WelcomeVideo "out/welcome_${name}.mp4" \
--props="{\"userName\":\"${name}\"}"
done
示例 2:动画销售仪表板
用户请求: “创建一个展示季度指标逐项动画的视频”
使用包含季度数据的 BarChart 组件构建合成,并渲染:
npx remotion render SalesDashboard out/q4_report.mp4 \
--props='{"quarter":"Q4","revenue":1250000,"growth":12.5}'
示例 3:带字幕的社交媒体内容
用户请求: “为视频添加动画字幕,用于 TikTok”
将 CaptionOverlay 与 OffthreadVideo 源结合:
import { OffthreadVideo } from "remotion";
<AbsoluteFill>
<OffthreadVideo src="https://example.com/clip.mp4" />
<CaptionOverlay />
</AbsoluteFill>
指导原则
- 使用
useCurrentFrame()和interpolate()进行所有动画;避免使用 CSS 过渡。 - 保持组件纯化:将所有动态数据作为属性传递,以便轻松进行批量渲染。
- 使用
<Sequence>组织视频时间线各章节。 - 对于视频源,优先使用
<OffthreadVideo>而不是<Video>以获得更好的性能。 - 设置
crf(恒定速率因子)在 16-20 之间以获得良好的质量与大小比。 - 对于 Lambda 渲染,保持合成时间不超过 120 秒以避免超时问题。
- 在渲染前在 Remotion Studio 中测试,尽早发现布局问题。
- 使用
staticFile()引用public/文件夹中的资源。