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',
});
// Spring 入場;除以 spring 持續時間是不必要的——直接驅動變換
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 schema 使屬性型別安全且在 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() 的純函數,因此您可以無需任何搜尋工具(這是 Web 場景 ?t=N 的繁重對應物)即可無頭渲染任何精確影格。當輸出必須是 MP4/GIF、必須攜帶精確的數字/文字、或批次來自資料時,請使用此層級;對於輕量級 Web 動畫,請改為交付獨立的 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)