Remotion Compose 是做什麼的?
您能將自然語言簡報轉換為完整、可預覽的 Remotion 組合 —— 真正的 React/TypeScript,搭配影格驅動的動畫。輸出檔案為 src/compositions/ 目錄下的 .tsx 檔案,並會註冊在 src/Root.tsx 中,使用者能立即在 Studio 中拖曳時間軸檢視,或交給 remotion-render 處理。
當收到任何建立、產生、動畫化或「為……撰寫影片程式碼」的請求時觸發,包括產品展示、導覽、功能介紹或行銷動畫 —— 例如「製作一個 20 秒的影片來展示 X」。
步驟 1 - 提取簡報
在開始編寫程式碼前,必須先取得這些資訊。以一次緊湊的批次提問;對於明顯的缺漏項目,使用合理的預設值填補,而非停滯不前:
- 持續時間(秒)、fps(預設 30 —— 產品與行銷影片的標準,渲染成本為 60fps 的一半。只有當動畫涉及精細的滑鼠游標移動或滾動 UI,且 30fps 會產生明顯卡頓時,才考慮使用 60fps)
- 場景 —— 依序發生的事件。盡量引導出一個節奏清單:「標誌、三個功能、CTA」優於「我的應用程式展示」
- 品牌 —— 主要 / 強調 / 背景色十六進位色碼,以及字型(一個
@remotion/google-fonts系列名稱)。 - 長寬比 / 平台 —— 16:9 橫向(1920×1080)、9:16 直向(1080×1920)用於社交媒體,或 1:1 正方形(1080×1080)。
- 文案 —— 實際的標題 / 功能名稱 / CTA 文字。絕不捏造產品宣稱;要求真實用語。
到處都將秒數轉換為影格:durationInFrames = 秒數 * fps。典型總長度:社交 / 產品展示 15-60 秒,廣告剪輯 6-15 秒。
步驟 2 - 建立場景計畫
每個影片一個 <Composition>,每個場景一個元件,在時間軸上使用 <Sequence> 串接。在撰寫程式碼前先配置好影格預算:
總計:20 秒 @ 30fps = 600 個影格
片頭(標誌) 影格 0-90 (Sequence from=0 durationInFrames=90)
功能 1 影格 90-240 (Sequence from=90 durationInFrames=150)
功能 2 影格 240-390 (Sequence from=240 durationInFrames=150)
功能 3 影格 390-540 (Sequence from=390 durationInFrames=150)
CTA 結尾 影格 540-600 (Sequence from=540 durationInFrames=60)
在預算規劃時需注意的節奏閾值:
- 任何承載文字的節拍至少需要 2 秒(30fps 下為 60 影格)—— 加上進場動畫與停留時間。一個標題,每個單字大約需要 150-200 毫秒的停留時間才能閱讀;一個 6 個單字的標題在其動畫播放完畢後需要大約 1 秒的停留時間。
- 進場動畫:10-20 影格。交叉淡出和離場動畫:12-15 影格。時間過長會顯得遲緩,過短則像一個閃爍。
- 除非是刻意的閃現剪接,否則沒有場景短於約 45 影格。
<Sequence from={F}> 會移動子元件的本地時鐘,使該元件內部的 useCurrentFrame() 從 0 開始。每一個場景都像從影格 0 開始一樣進行動畫。
步驟 3 - 核心 API 參考
useCurrentFrame()—— 當前影格(時鐘)。所有動畫都是這個值的函數。useVideoConfig()——{ fps, width, height, durationInFrames }。interpolate(frame, [inFrames], [outValues], opts)—— 線性映射。在進場 / 離場時始終傳入{ extrapolateLeft: "clamp", extrapolateRight: "clamp" },避免數值超出範圍。spring({ frame, fps, config })—— 基於物理的 0→1(會稍微超過)。用於任何需要感覺有生命力的元素(彈出、滑動、縮放顯示)。AbsoluteFill—— 一個全螢幕、絕對定位的 div;每個場景的基礎圖層。Sequence—— 時間位移並限制子元件的時間範圍(場景調度器)。Series/Series.Sequence—— 緊接連續的場景,無需手動計算from偏移量。Img、staticFile—— 請參閱步驟 6。
步驟 4 - spring() 與 interpolate() 的比較
使用 spring() 在……情況下 | 使用 interpolate() 在……情況下 |
|---|---|
| 運動應有物理感 —— 彈出、反彈、縮放顯示、滑入 | 需要在精確影格之間進行精確的線性控制 |
| 需要自然穩定 / 過衝效果 | 淡入淡出(透明度 0→1)、色彩變化、進度條、任何需要精準對齊影格的效果 |
| 卡片、標誌、徽章的進入效果 | 交叉淡出和離場,過衝會看起來不對時使用 |
模式 —— 使用 spring 驅動變換,使用 interpolate 驅動透明度:
const { fps } = useVideoConfig();
const frame = useCurrentFrame();
const pop = spring({ frame, fps, config: { damping: 200 } }); // 0 → 1,有活力
const opacity = interpolate(frame, [0, 15], [0, 1], { extrapolateRight: "clamp" });
const scale = interpolate(pop, [0, 1], [0.8, 1]);
damping: 200 是主力配置 —— 快速穩定且幾乎沒有過衝。較低的阻尼(約 10-20)會產生明顯的反彈;僅在反彈是想要的效果時才使用低阻尼值。
步驟 5 - 標準產品展示結構
除非簡報另有說明,否則採用此結構:標誌 / 品牌片頭 → N 個功能介紹 → CTA 結尾。使其資料驅動(步驟 6),以便可以自由變體。
// src/compositions/ProductDemo.tsx
import {
AbsoluteFill, Sequence, useCurrentFrame, useVideoConfig,
interpolate, spring,
} from "remotion";
import { loadFont } from "@remotion/google-fonts/Inter";
const { fontFamily } = loadFont();
export type Feature = { title: string; body: string };
export type ProductDemoProps = {
brand: string;
colors: { bg: string; fg: string; accent: string };
logoText: string;
features: Feature[];
cta: string;
};
const Intro: React.FC<{ text: string; colors: ProductDemoProps["colors"] }> = ({ text, colors }) => {
const frame = useCurrentFrame();
const { fps } = useVideoConfig();
const s = spring({ frame, fps, config: { damping: 200 } });
const scale = interpolate(s, [0, 1], [0.7, 1]);
const opacity = interpolate(frame, [0, 12], [0, 1], { extrapolateRight: "clamp" });
return (
<AbsoluteFill style={{ backgroundColor: colors.bg, justifyContent: "center", alignItems: "center" }}>
<h1 style={{ fontFamily, fontSize: 120, color: colors.fg, transform: `scale(${scale})`, opacity }}>
{text}
</h1>
</AbsoluteFill>
);
};
const FeatureCallout: React.FC<{ feature: Feature; colors: ProductDemoProps["colors"] }> = ({ feature, colors }) => {
const frame = useCurrentFrame();
const { fps } = useVideoConfig();
const slide = spring({ frame, fps, config: { damping: 200 } });
const x = interpolate(slide, [0, 1], [-60, 0]);
const opacity = interpolate(frame, [0, 15], [0, 1], { extrapolateRight: "clamp" });
return (
<AbsoluteFill style={{ backgroundColor: colors.bg, justifyContent: "center", paddingLeft: 160 }}>
<div style={{ transform: `translateX(${x}px)`, opacity }}>
<div style={{ fontFamily, fontSize: 32, color: colors.accent, fontWeight: 700, letterSpacing: 2 }}>
{feature.title.toUpperCase()}
</div>
<div style={{ fontFamily, fontSize: 64, color: colors.fg, maxWidth: 1100, marginTop: 16 }}>
{feature.body}
</div>
</div>
</AbsoluteFill>
);
};
const Outro: React.FC<{ cta: string; colors: ProductDemoProps["colors"] }> = ({ cta, colors }) => {
const frame = useCurrentFrame();
const opacity = interpolate(frame, [0, 18], [0, 1], { extrapolateRight: "clamp" });
return (
<AbsoluteFill style={{ backgroundColor: colors.accent, justifyContent: "center", alignItems: "center" }}>
<h2 style={{ fontFamily, fontSize: 84, color: colors.bg, opacity }}>{cta}</h2>
</AbsoluteFill>
);
};
export const ProductDemo: React.FC<ProductDemoProps> = ({ colors, logoText, features, cta }) => {
const intro = 90;
const per = 150;
const outro = 60;
return (
<AbsoluteFill>
<Sequence durationInFrames={intro}>
<Intro text={logoText} colors={colors} />
</Sequence>
{features.map((f, i) => (
<Sequence key={i} from={intro + i * per} durationInFrames={per}>
<FeatureCallout feature={f} colors={colors} />
</Sequence>
))}
<Sequence from={intro + features.length * per} durationInFrames={outro}>
<Outro cta={cta} colors={colors} />
</Sequence>
</AbsoluteFill>
);
};
在 src/Root.tsx 中註冊,使用相同數字計算 durationInFrames 並提供 defaultProps:
<Composition
id="ProductDemo"
component={ProductDemo}
durationInFrames={90 + 3 * 150 + 60}
fps={30}
width={1920}
height={1080}
defaultProps={{
brand: "Acme",
colors: { bg: "#0B0B0F", fg: "#FFFFFF", accent: "#6C5CE7" },
logoText: "Acme",
features: [
{ title: "快速", body: "數秒內交付,而非數個衝刺。" },
{ title: "簡單", body: "一個提示,一個影片。" },
{ title: "屬於你", body: "你的品牌,你的文案。" },
],
cta: "立即免費開始 →",
}}
/>
對於直向 / 社交媒體,設定 width={1080} height={1920};對於正方形,設定 1080×1080。保持元件相同 —— 只有畫布改變。
步驟 6 - 資料驅動的屬性與靜態資源
上述組合是完全資料驅動的:每個文字和顏色都是一個屬性。這就是重點 —— 變體(新的文案、新的調色盤、在地化的文字)無需重新提示;只需傳遞不同的屬性(remotion-render 可以從 JSON 檔案批次處理此操作)。
靜態資源(標誌、截圖、音訊)位於 public/assets/ 目錄中,並使用 staticFile() 引用 —— 絕不使用 import 或原始路徑:
import { Img, staticFile } from "remotion";
<Img src={staticFile("assets/logo.png")} />
步驟 7 - 輸出
將完成的檔案儲存到 src/compositions/<VideoName>.tsx,在 src/Root.tsx 中註冊,並告知使用者使用 npx remotion studio 預覽(http://localhost:3000)。然後交接:「在 Studio 中拖曳時間軸。告訴我需要更改什麼 —— 速度太快、顏色不對、文案不同 —— 我會編輯並重新渲染(remotion-render)。」
禁止事項
- 禁止使用 CSS 轉場 / 動畫或
setTimeout/setInterval進行動畫。Remotion 會獨立且並行地渲染影格;任何不源自useCurrentFrame()的內容在最終渲染中都是不可見或不一致的,即使它在瀏覽器預覽中看起來正常。 - 禁止使用
Math.random()進行散落 / 粒子效果 —— 每個渲染工作執行緒會得到不同的數值,導致影格無法匹配。請使用 Remotion 的random(seed)來獲得確定性的偽亂數。 - 禁止在沒有
delayRender()/continueRender()的情況下擷取資料或等待非同步工作 —— 渲染器會在資料到達之前捕捉影格。 - 禁止在場景元件內部硬編碼 fps、width 或 height —— 從
useVideoConfig()讀取它們,這樣切換 30↔60fps 或橫向↔直向時不會破壞每個時間和佈局。 - 禁止省略進場 / 離場 interpolations 中的
extrapolateLeft/Right: "clamp"—— 透明度會超過 1,元素在其範圍結束後會飄離畫面。 - 禁止捏造標題、功能宣稱或 CTA —— 使用使用者的真實文案,如果缺少則詢問。
品質標準
- 每個動畫值都是
useCurrentFrame()(加上屬性)的純函數 —— 在 Studio 中向後拖曳時間軸,看起來與向前播放完全相同。 - 計畫中的影格預算總和必須與
Root.tsx中的durationInFrames完全相等,且沒有任何文字節拍的停留時間低於其可讀最小時間。 - 所有文案和顏色均透過具有
defaultProps的型別屬性傳入 —— 場景元件中沒有埋入的字面常數。 - 資源透過
staticFile()載入;檔案能乾淨註冊並在 Studio 中預覽,無控制檯錯誤。