使用時機
當你處理 Remotion 程式碼時,請使用此技能以獲取領域特定知識。
新專案設置
當處於沒有現有 Remotion 專案的空白資料夾或工作區時,使用以下指令建立新專案:
npx create-video@latest --yes --blank --no-tailwind my-video
將 my-video 替換為合適的專案名稱。
設計影片
在設計視覺場景、佈局、宣傳片、動態圖形或大量文字影片之前,請載入 rules/video-layout.md 以取得影片優先佈局與文字大小的指導。
使用 useCurrentFrame() 和 interpolate() 動畫化屬性。除非明確需要基於物理的動畫,否則請優先使用 interpolate() 而非 spring()。使用 Easing.bezier() 自訂時間曲線,包括跳動或超調運動。
對於應在 Remotion Studio 中可編輯的動畫,請將 interpolate() 呼叫內嵌在 style prop 中,並使用個別的 CSS transform 屬性(scale、translate、rotate),而不是組合 transform 字串。
import { useCurrentFrame, Easing, interpolate, useVideoConfig } from "remotion";
export const FadeIn = () => {
const frame = useCurrentFrame();
const { fps } = useVideoConfig();
const opacity = interpolate(frame, [0, 2 * fps], [0, 1], {
extrapolateRight: "clamp",
extrapolateLeft: "clamp",
easing: Easing.bezier(0.16, 1, 0.3, 1),
});
return <div style={{ opacity }}>Hello World!</div>;
};
優先使用:
style={{
scale: interpolate(frame, [0, 100], [0, 1]),
translate: interpolate(frame, [0, 100], ["0px 0px", "100px 100px"]),
rotate: interpolate(frame, [0, 100], ["20deg", "90deg"]),
}}
而非:
const scale = interpolate(frame, [0, 100], [0, 1]);
style={{
transform: `scale(${scale})`,
}}
禁止使用 CSS transitions 或 animations —— 它們無法正確渲染。 禁止使用 Tailwind 動畫類別名稱 —— 它們無法正確渲染。
將素材放在專案根目錄的 public/ 資料夾中。
使用 staticFile() 引用 public/ 資料夾中的檔案。
使用 <Img> 元件加入圖片:
import { Img, staticFile } from "remotion";
export const MyComposition = () => {
return <Img src={staticFile("logo.png")} style={{ width: 100, height: 100 }} />;
};
使用 @remotion/media 的 <Video> 元件加入影片:
import { Video } from "@remotion/media";
import { staticFile } from "remotion";
export const MyComposition = () => {
return <Video src={staticFile("video.mp4")} style={{ opacity: 0.5 }} />;
};
使用 @remotion/media 的 <Audio> 元件加入音訊:
import { Audio } from "@remotion/media";
import { staticFile } from "remotion";
export const MyComposition = () => {
return <Audio src={staticFile("audio.mp3")} />;
};
素材也可以透過遠端 URL 引用:
import { Video } from "@remotion/media";
export const MyComposition = () => {
return <Video src="https://remotion.media/video.mp4" />
};
若要延遲內容,請用 <Sequence> 包裝並使用 from。
若要限制元件的持續時間,請使用 <Sequence> 的 durationInFrames。
<Sequence> 預設為絕對填滿。若為內嵌內容,請使用 layout="none"。
import { Sequence } from "remotion";
export const Title = () => {
const frame = useCurrentFrame();
const { fps } = useVideoConfig();
const opacity = interpolate(frame, [0, 2 * fps], [0, 1], {
extrapolateRight: "clamp",
extrapolateLeft: "clamp",
easing: Easing.bezier(0.16, 1, 0.3, 1),
});
return <div style={{ opacity }}>Title</div>;
};
export const Subtitle = () => {
return <div>Subtitle</div>;
};
const Main = () => {
const {fps} = useVideoConfig();
return (
<AbsoluteFill>
<Sequence>
<Background />
</Sequence>
<Sequence from={1 * fps} durationInFrames={2 * fps} layout="none">
<Title />
</Sequence>
<Sequence from={2 * fps} durationInFrames={2 * fps} layout="none">
<Subtitle />
</Sequence>
</AbsoluteFill>
);
}
寬度、高度、fps 和持續時間在 src/Root.tsx 中定義:
import { Composition } from "remotion";
import { MyComposition } from "./MyComposition";
export const RemotionRoot = () => {
return (
<Composition
id="MyComposition"
component={MyComposition}
durationInFrames={100}
fps={30}
width={1080}
height={1080}
/>
);
};
後設資料也可以動態計算:
import { Composition, CalculateMetadataFunction } from "remotion";
import { MyComposition, MyCompositionProps } from "./MyComposition";
const calculateMetadata: CalculateMetadataFunction<
MyCompositionProps
> = async ({ props, abortSignal }) => {
const data = await fetch(`https://api.example.com/video/${props.videoId}`, {
signal: abortSignal,
}).then((res) => res.json());
return {
durationInFrames: Math.ceil(data.duration * 30),
props: {
...props,
videoUrl: data.url,
},
width: 1080,
height: 1080,
};
};
export const RemotionRoot = () => {
return (
<Composition
id="MyComposition"
component={MyComposition}
fps={30}
width={1080}
height={1080}
defaultProps={{ videoId: "abc123" }}
calculateMetadata={calculateMetadata}
/>
);
};
啟動預覽
啟動 Remotion Studio 以預覽影片:
npx remotion studio
可選:單影格渲染檢查
你可以使用 CLI 渲染單一影格來驗證佈局、顏色或時間。 對於簡易編輯、純重構或你已從 Studio 或先前渲染獲得足夠信心時,可跳過此步驟。
npx remotion still [composition-id] --scale=0.25 --frame=30
在 30 fps 下,--frame=30 代表一秒鐘的位置(--frame 從零開始)。
字幕
處理字幕或隱藏式字幕時,請載入 ./rules/subtitles.md 檔案以取得更多資訊。
使用 FFmpeg
對於某些影片操作,例如修剪影片或偵測靜音,應使用 FFmpeg。請載入 ./rules/ffmpeg.md 檔案以取得更多資訊。
靜音偵測
當需要偵測並修剪影片或音訊檔案中的靜音段落時,請載入 ./rules/silence-detection.md 檔案。
音訊視覺化
當需要視覺化音訊(頻譜條、波形、低音反應效果)時,請載入 ./rules/audio-visualization.md 檔案以取得更多資訊。
音效
當需要使用音效時,請載入 ./rules/sfx.md 檔案以取得更多資訊。
視覺與像素效果
建立視覺效果時,優先順序為:1. 一般 Remotion/HTML/CSS/SVG/濾鏡/混合/遮罩動畫;2. 透過 rules/effects.md 使用列出的效果(包括在 <HtmlInCanvas> 中渲染的 HTML);3. 當使用者要求可重用或專案專屬效果時,透過 rules/effects.md 使用自訂 createEffect();4. 僅在無合適效果時,使用自訂 <HtmlInCanvas onPaint>(透過 rules/html-in-canvas.md)。
關於光漏疊加,請參閱 rules/light-leaks.md。文件:https://www.remotion.dev/docs/effects
可用效果:brightness(), contrast(), colorKey(), duotone(), grayscale(), hue(), invert(), saturation(), tint(), thermalVision(), blur(), linearProgressiveBlur(), zoomBlur(), dropShadow(), glow(), lightTrail(), evolve(), mirror(), scale(), uvTranslate(), xyTranslate(), barrelDistortion(), chromaticAberration(), fisheye(), cornerPin(), wave(), burlap(), emboss(), dotGrid(), halftone(), noise(), noiseDisplacement(), pattern(), pixelate(), pixelDissolve(), scanlines(), speckle(), shine(), shrinkwrap(), vignette(), contourLines(), checkerboard(), halftoneLinearGradient(), gridlines(), whiteNoise(), tvSignalOff(), lines(), rings(), waves(), zigzag(), lightLeak(), starburst().
3D 內容
關於在 Remotion 中使用 Three.js 和 React Three Fiber 的 3D 內容,請參閱 rules/3d.md。
進階音訊
關於修剪、音量、速度、音高等進階音訊功能,請參閱 rules/audio.md。
動態持續時間、尺寸與資料
關於動態設定構圖持續時間、尺寸和 props,請參閱 rules/calculate-metadata.md。
進階構圖
關於定義靜止畫面、資料夾、預設 props 以及巢狀構圖的方法,請參閱 rules/compositions.md。
Google Fonts
這是 Remotion 中載入字型的最佳方式。關於如何載入 Google Fonts,請參閱 rules/google-fonts.md。
本地字型
關於如何載入本地字型,請參閱 rules/local-fonts.md。
取得音訊持續時間
關於使用 Mediabunny 取得音訊檔案的持續時間(秒),請參閱 rules/get-audio-duration.md。
取得影片尺寸
關於使用 Mediabunny 取得影片檔案的寬度和高度,請參閱 rules/get-video-dimensions.md。
取得影片持續時間
關於使用 Mediabunny 取得影片檔案的持續時間(秒),請參閱 rules/get-video-duration.md。
GIF
關於如何顯示與 Remotion 時間軸同步的 GIF,請參閱 rules/gifs.md。
進階圖片
關於調整圖片大小與位置、動態圖片路徑以及取得圖片尺寸,請參閱 rules/images.md。
Lottie 動畫
關於在 Remotion 中嵌入 Lottie 動畫,請參閱 rules/lottie.md。
測量 DOM 節點
關於在 Remotion 中測量 DOM 元素尺寸,請參閱 rules/measuring-dom-nodes.md。
測量文字
關於測量文字尺寸、將文字放入容器以及檢查溢出,請參閱 rules/measuring-text.md。
進階序列化
關於更多的序列化模式——延遲、修剪、限制項目持續時間,請參閱 rules/sequencing.md。
TailwindCSS
關於在 Remotion 中使用 TailwindCSS,請參閱 rules/tailwind.md。
文字動畫
關於排版和文字動畫模式,請參閱 rules/text-animations.md。
進階時間控制
關於使用 interpolate、Bézier 緩動以及彈簧的進階時間控制,請參閱 rules/timing.md。
轉場
關於場景轉場模式,請參閱 rules/transitions.md。
透明影片
關於渲染帶透明度輸出的影片,請參閱 rules/transparent-videos.md。
修剪
關於修剪模式——剪掉動畫的開頭或結尾,請參閱 rules/trimming.md。
進階影片
關於嵌入影片的進階知識——修剪、音量、速度、循環、音高,請參閱 rules/videos.md。
參數化影片
關於透過添加 Zod schema 使構圖可參數化,請參閱 rules/parameters.md。
地圖
對於簡單的地圖(少量飛越鏡頭),請考慮使用靜態地圖圖片。 對於具有動畫路線或飛越鏡頭的複雜地圖,請載入地圖規則:rules/maplibre.md
旁白
關於使用 ElevenLabs TTS 為 Remotion 構圖添加 AI 生成的旁白,請參閱 rules/voiceover.md。