Remotion React 標記指南
這是書寫 Remotion React 標記的指引。 如果與此無關,請改為載入 Remotion 最佳實踐。
保留用戶的修改
用戶可能在對話之外的程式碼中進行編輯。
如果你發現中途有出乎意料的變更,請不要覆寫它;假設該變更是有意的,或者請求確認。
一般規則
在構圖(composition)或場景(scene)元件內部,應從 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}) 來建立具備可編輯屬性的自訂視覺元件,並將可重複使用的場景註冊為連線構圖(connected composition)。
將時間參數直接放在支援的元件上;避免冗餘的 <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。在多場景影片中,建議對重要的場景採用此結構。
預先構圖動作
對於 Studio 請求(例如 Pre-compose Ambient glow (src/BarChart.tsx:134)),請在指定位置找到選取的序列標記。建立一個連線構圖,遵循 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 方法、預覽需求及替代方案。
視覺與像素效果
當添加視覺效果時,首先確定它應影響的元素或群組。
如果目標元件支援 effects 且以下有符合請求的內建效果,則優先使用該效果而非 CSS 近似效果。例如,當暈影(vignette)應影響某個影片時,直接將 vignette() 套用至 <Video> 的 effects 屬性。
這些元件直接接受 effects:
| Import from | Components |
|---|---|
remotion | <Img>, <CanvasImage>, <AnimatedImage>, <Solid>, <HtmlInCanvas> |
@remotion/media | <Video> |
@remotion/gif | <Gif> |
@remotion/rive | <RemotionRiveCanvas> |
@remotion/shapes | <Arrow>, <Callout>, <Circle>, <Ellipse>, <Heart>, <Pie>, <Polygon>, <Rect>, <Spark>, <Star>, <Triangle> |
一般的 HTML 元素、<Interactive.*> DOM 包裝器、<AbsoluteFill> 和 <Sequence> 不直接接受 effects。若要對其內容套用效果,請將它們包在 <HtmlInCanvas> 中,並將 effects 屬性放在該包裝器上。
對於其他元素,在適當的情況下偏好使用一般的 HTML 和 CSS。當需要對整個群組進行後製處理且環境支援時,考慮將群組包在 <HtmlInCanvas> 中。
在套用效果之前,請閱讀 effects.md 以了解匯入、參數和設定。當沒有內建效果符合需求時,它也涵蓋了使用 createEffect() 建立可重複使用的自訂效果。
可用的效果
來自 @remotion/effects 的內建效果:
- 色彩與色調:
brightness()、colorCorrection()、colorKey()、contrast()、duotone()、exposure()、grayscale()、hue()、invert()、levels()、linearGradient()、linearGradientTint()、lut()、saturation()、shadowsHighlights()、thermalVision()、tint()、vibrance()、whiteBalance()。 - 模糊與軌跡:
blur()、lightTrail()、linearProgressiveBlur()、radialProgressiveBlur()、regionBlur()、zoomBlur()。 - 光照:
dropShadow()、glow()、lightLeak()、shine()、starburst()、vignette()。 - 扭曲與變形:
barrelDistortion()、circle()、colorWipe()、linearWipe()、pixelate()、radialWipe()、waves()。 - 合成與遮罩:
chromaKey()、lumaKey()、mask()。 - 文字/圖形產生:
dateTime()、text()、timecode()。