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: "This", startFrame: 0, durationInFrames: 15 },
{ text: "is", startFrame: 15, durationInFrames: 10 },
{ text: "amazing!", 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":"Custom 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":"Video 1"}'
# 在 Google Cloud Run 上渲染
npx remotion cloudrun render MyVideo --props='{"title":"Video 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 過渡效果。 - 保持合成純淨:將所有動態資料作為 props 傳遞,以便輕鬆批次渲染。
- 使用
<Sequence>來組織影片時間軸的各個區段。 - 優先使用
<OffthreadVideo>而非<Video>,以獲得更好的影片來源效能。 - 將
crf(恆定速率因子)設定在 16-20 之間,以獲得良好的品質與檔案大小比例。 - 對於 Lambda 渲染,請將合成保持在 120 秒以內,以避免逾時問題。
- 在渲染之前先在 Remotion Studio 中測試,以提前發現版面問題。
- 使用
staticFile()來引用public/資料夾中的資源。