這份文件是編寫 Remotion React 標記語言的指引。 如果不適用,請載入 Remotion 最佳實務。
保留使用者的更動
使用者可能會在對話之外的程式碼中進行編輯。
如果您在此期間偵測到出乎意料的更動,請不要覆寫它,應視為有意的變更或要求確認。
通用規則
在合成或場景元件內部,請使用 useVideoConfig() 取得 fps,
用於所有「秒數轉幀數」的計算,包括 from、trimBefore、durationInFrames 和 premountFor 等時間屬性。
將表達式保留在 JSX 節點上,例如 trimBefore={4 * fps},這讓 Studio 可以編輯時間設定,
並在合成幀率變更時維持正確性。
使用 useCurrentFrame() 和 interpolate() 驅動動畫。
CSS transition 或 animation 將無法正確渲染,需要進行重構。
Tailwind 動畫類別將無法正確渲染,需要進行重構。
使用 Easing.bezier() 和 Easing.spring() 來自訂時間曲線。
根據 Remotion 互動性最佳實務來建構您的標記。
建議對具有可編輯屬性的自訂視覺元件使用 Interactive.withSchema({wrapInSequence: true}),
並將可重複使用的場景註冊為「連動合成」。
將時間屬性直接放在支援它的元件上;避免多餘的 <Sequence> 包裝。
為每個支援 premountFor 的計時元件賦予一秒的預先掛載時間:
premountFor={fps},其中 fps 來自 useVideoConfig()。將此應用於媒體、互動元件、
<Sequence>、<Series.Sequence>、<TransitionSeries.Sequence> 和 <TransitionSeries.Overlay>,
包括巢狀在場景內的計時元件。當巢狀項目需要在父項目開始前掛載時,也請預先掛載父時間軸項目。
不需要為 premountFor 的元件(例如 <TransitionSeries.Transition>)提供替代方案。
Studio 會編輯建立某個項目的 JSX 來源節點。請將每個合成註冊、片段、場景、圖層和序列都視為 獨立的 JSX 節點來撰寫,並將其可編輯屬性內聯化。 當生成的實例被有意控制為單一來源模板時,才適合使用程式化迴圈, 而不適用於使用者需要分別編輯每個實例的情況。
import { useCurrentFrame, useVideoConfig, Easing, interpolate, Interactive } from "remotion";
export const FadeIn = () => {
const frame = useCurrentFrame();
const {fps} = useVideoConfig();
return (
<Interactive.Div
name="Title"
premountFor={fps}
style={{
opacity: interpolate(frame, [0, 2 * fps], [0, 1], {
extrapolateRight: "clamp",
extrapolateLeft: "clamp",
easing: Easing.bezier(0.16, 1, 0.3, 1),
}),
}}
>
Hello World!
</Interactive.Div>
);
};
將 interpolate() 呼叫保留在 style 屬性中內聯。
優先使用 scale、translate、rotate 等 CSS 屬性,而不是 transform。
// 👍 內聯可編輯的關鍵影格和變形簡寫
style={{
scale: interpolate(frame, [0, 100], [0, 1], {
extrapolateLeft: 'clamp',
extrapolateRight: 'clamp',
easing: Easing.spring({damping: 200}),
output: 'perceptual-scale' // 對於 `scale` 動畫,使用 "output: 'perceptual-scale'"
}),
translate: interpolate(frame, [0, 100], ["0px 0px", "100px 100px"], {
extrapolateLeft: 'clamp',
extrapolateRight: 'clamp',
easing: Easing.spring({damping: 200}),
}),
rotate: interpolate(frame, [0, 100], ["20deg", "90deg"], {
extrapolateLeft: 'clamp',
extrapolateRight: 'clamp',
easing: Easing.spring({damping: 200}),
}),
}}
// 👎 非內聯的值和 transform 字串在 Studio 中會更難編輯
const scale = interpolate(frame, [0, 100], [0, 1]);
style={{
transform: `scale(${scale})`,
}}
素材
將素材放置在專案根目錄的 public/ 資料夾中。
使用 staticFile() 來引用 public/ 資料夾中的檔案。
媒體元件
使用 @remotion/media 套件中的 <Video> 和 <Audio> 來加入影片和音訊。
使用 <CanvasImage> 元件來加入圖片。
使用 <AnimatedImage> 來加入動畫 GIF、APNG、WebP 或 AVIF 圖片;如果不是使用 Chrome,請改用 @remotion/gif。
對於 public/ 中的檔案,使用 staticFile(),或直接傳遞遠端 URL:
import { Audio, Video } from "@remotion/media";
import { staticFile, CanvasImage, AnimatedImage, useVideoConfig } from "remotion";
export const MyComposition = () => {
const {fps} = useVideoConfig();
return (
<>
<Video src={staticFile("video.mp4")} premountFor={fps} style={{ opacity: 0.5 }} />
<Audio src={staticFile("audio.mp3")} premountFor={fps} />
<CanvasImage
src={staticFile("logo.png")}
premountFor={fps}
style={{ width: 100, height: 100 }}
/>
<Video src="https://remotion.media/video.mp4" premountFor={fps} />
<AnimatedImage src={staticFile('nyancat.gif')} premountFor={fps} />
</>
);
};
如果該合成主要是影片或音訊片段的時間軸,請在選擇其來源結構之前先閱讀 video-editing.md。
範例場景
一個背景影片帶有下方三分之一區塊(lower third)。
該下方三分之一區塊是一個擁有自己時間軸的互動元件,已註冊為 連動合成。
其文字作為 children 傳遞,而 accentColor 是一個可編輯屬性。
淡入效果在呼叫處內聯關鍵影格化。
// MyScene.tsx
import { Video } from "@remotion/media";
import { Easing, interpolate, useCurrentFrame, useVideoConfig } from "remotion";
import { LowerThird } from "./LowerThird";
export const MyScene = () => {
const { fps } = useVideoConfig();
const frame = useCurrentFrame();
return (
<>
<Video
name="Background"
src="https://remotion.media/video.mp4"
premountFor={fps}
objectFit="cover"
style={{
position: "absolute",
width: "100%",
height: "100%",
}}
/>
<LowerThird
name="Lower third"
from={1 * fps}
premountFor={fps}
accentColor="#0b84f3"
style={{
opacity: interpolate(frame, [1 * fps, 2 * fps], [0, 1], {
extrapolateRight: "clamp",
extrapolateLeft: "clamp",
easing: Easing.bezier(0.16, 1, 0.3, 1),
}),
}}
>
Jane Doe, Product Designer
</LowerThird>
</>
);
};
// LowerThird.tsx
import type React from "react";
import { Interactive, type InteractivitySchema } from "remotion";
type LowerThirdProps = {
readonly children: string;
readonly accentColor: string;
readonly style?: React.CSSProperties;
};
const LowerThirdInner: React.FC<LowerThirdProps> = ({
children,
accentColor,
style,
}) => {
return (
<Interactive.Div
style={{
position: "absolute",
left: 80,
bottom: 80,
display: "flex",
alignItems: "center",
gap: 20,
backgroundColor: "white",
borderRadius: 16,
padding: "20px 32px",
color: "black",
fontFamily: "Helvetica, Arial, sans-serif",
fontSize: 48,
fontWeight: 600,
...style,
}}
>
<div
style={{
width: 8,
alignSelf: "stretch",
borderRadius: 4,
backgroundColor: accentColor,
}}
/>
{children}
</Interactive.Div>
);
};
const lowerThirdSchema = {
children: { type: "text-content", default: "", description: "Text" },
accentColor: {
type: "color",
default: "#0b84f3",
description: "Accent color",
},
} as const satisfies InteractivitySchema;
export const LowerThird = Interactive.withSchema({
Component: LowerThirdInner,
componentName: "<LowerThird>",
schema: lowerThirdSchema,
wrapInSequence: true,
});
// Root.tsx
import { Composition } from "remotion";
import { LowerThird } from "./LowerThird";
import { MyScene } from "./MyScene";
export const RemotionRoot: React.FC = () => {
return (
<>
<Composition
id="MyScene"
component={MyScene}
durationInFrames={60}
fps={30}
width={1280}
height={720}
/>
<Composition
id="LowerThird"
component={LowerThird}
durationInFrames={30}
fps={30}
width={1280}
height={720}
defaultProps={{
children: "Jane Doe, Product Designer",
accentColor: "#0b84f3",
}}
/>
</>
);
};
延遲與修剪
將時間屬性直接放在支援它的元件上。有關支援的屬性、範例和操作順序,請參閱 時間屬性。
地圖
如果要在影片中包含地圖,請參閱 Remotion 地圖。
文字高亮與註解
有關文字高亮(高亮標記)、圓圈、底線、刪除線、劃掉文字、方框,請參閱 text-highlights.md。
多場景影片
如果計劃製作包含多個連續場景的影片,請參閱 multi-scene-video.md。
連動合成
當某個場景或圖層群組需要有自己的可編輯時間軸時,請遵循 connected-compositions.md。在多場景影片中,對於重要的場景,建議優先使用此結構。
預先組合操作
對於像是 Pre-compose Ambient glow (src/BarChart.tsx:134) 這樣的 Studio 請求,
請在指定位置找到選定的序列標記。製作一個連動合成,遵循 connected-compositions.md:
將標記提取到一個命名元件中,最好使用 Interactive.withSchema({wrapInSequence: true}) 使其成為互動元件,
並在根層級用唯一的 <Composition> 註冊相同的匯出元件引用。
直接使用其時間屬性渲染互動元件,或者當包裝器有特定用途時,將其作為序列的唯一子元素渲染。
如果選定的節點已經是一個序列,請保留其屬性並提取其子元素。
註冊需要維度、fps、持續時間以及與其父層使用方式等效的 defaultProps。
未註冊合成的元件提取不算完成預先組合請求。
將繼承的樣式(如 fontFamily)和字型載入程式碼帶入提取的元件中;
請參閱 父層獨立性。
旁白
有關使用 ElevenLabs TTS 為 Remotion 合成添加 AI 生成的旁白,請參閱 voiceover.md。
嵌入影片
有關嵌入影片的進階知識 - 修剪、音量、速度、循環、音高,請參閱 embedding-videos.md。
嵌入音訊
有關音訊的進階功能,如修剪、音量、速度、音高,請參閱 audio.md。
裁切
如果需要裁切元件的可視矩形,請參閱 cropping.md。
轉場效果
有關場景轉換模式,請參閱 transitions.md。
動態模糊
當需要添加動態模糊或運動軌跡時,請閱讀 motion-blur.md 了解首選的 HTML-in-canvas 方法、預覽需求及替代方案。
視覺與像素效果
在建立視覺效果時,考慮它是否可以使用 CSS 和 HTML 實現,還是需要使用著色器。 偏好順序:
- 常規 HTML + CSS 或其他網頁技術
- 直接應用於元素的效果(
<Video>、<Img>),或通過將內容包裹在<HtmlInCanvas>中,該元件也接受effects屬性:
- 透過 effects.md 提供的預設效果
- 當沒有預設效果可用時,透過 effects.md 使用自訂的
createEffect()。
3D 內容
有關使用 Three.js 和 React Three Fiber 在 Remotion 中創建 3D 內容,請參閱 ./3d.md。
音效
當需要使用音效時,請載入 ./sfx.md 檔案以獲取更多資訊。
音訊視覺化
當需要視覺化音訊(頻譜條、波形、低音反應效果)時,請載入 ./audio-visualization.md 檔案以獲取更多資訊。
地圖
對於靜態地圖、動畫路徑和標記、地理解說、Mapbox、MapLibre、MapTiler、GeoJSON 或 3D 地理飛越,請載入 Remotion 地圖。
字幕
當涉及到字幕時,請載入 Remotion 字幕技能以獲取更多資訊。
Google 字型
這是 Remotion 中載入字型的建議方式。請參閱 google-fonts.md 了解如何載入 Google 字型。
本地字型
請參閱 local-fonts.md 了解如何載入本地字型。
GIF
請參閱 gifs.md 了解如何顯示與 Remotion 時間軸同步的 GIF。
進階圖片
請參閱 images.md 了解圖片大小調整和定位、動態圖片路徑以及取得圖片尺寸。
Lottie 動畫
有關在 Remotion 中嵌入 Lottie 動畫,請參閱 lottie.md。
時間控制
請參閱 timing.md 了解 interpolate() 的更多時間控制技巧。
參數化影片
請參閱 parameters.md 了解如何透過添加 Zod 模式來使合成參數化。
測量 DOM 節點
請參閱 measuring-dom-nodes.md 了解如何在 Remotion 中測量 DOM 元素的尺寸。
測量文字
請參閱 measuring-text.md 了解如何測量文字尺寸、將文字適配到容器以及檢查溢出。
使用 FFmpeg
對於某些影片操作,例如修剪影片或檢測靜音,應使用 FFmpeg。請載入 ./ffmpeg.md 檔案以獲取更多資訊。
靜音檢測
當需要檢測並修剪影片或音訊檔案中的靜音片段時,請載入 ./silence-detection.md 檔案。
動態持續時間、尺寸和資料
請參閱 calculate-metadata.md 了解如何動態設定合成的持續時間、尺寸及其它元資料。