使用時機
每當處理 Remotion 程式碼時,使用此技能以取得領域特定知識。
新專案設定
當處在一個空資料夾或工作區,且沒有任何現有 Remotion 專案時,使用以下指令建立一個新專案:
npx create-video@latest --yes --blank --no-tailwind my-video
將 my-video 替換為合適的專案名稱。
設計影片
使用 useCurrentFrame() 和 interpolate() 來為屬性製作動畫。使用 Easing 來自訂動畫的時序。
import { useCurrentFrame, Easing } 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>;
};
禁止使用 CSS 過渡或動畫 —— 它們無法正確渲染。
禁止使用 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 表示 1 秒鐘的位置(--frame 從 0 開始)。
字幕
處理字幕時,請載入 ./rules/subtitles.md 檔案以獲取更多資訊。
使用 FFmpeg
對於某些影片操作,例如修剪影片或檢測靜音,應使用 FFmpeg。載入 ./rules/ffmpeg.md 檔案以獲取更多資訊。
靜音檢測
當需要檢測並修剪影片或音訊檔案中的靜音片段時,請載入 ./rules/silence-detection.md 檔案。
音訊可視化
當需要可視化音訊(頻譜條、波形、低音反應效果)時,請載入 ./rules/audio-visualization.md 檔案以獲取更多資訊。
音效
當需要使用音效時,請載入 ./rules/sfx.md 檔案以獲取更多資訊。
3D 內容
請參閱 rules/3d.md 了解如何在 Remotion 中使用 Three.js 和 React Three Fiber 建立 3D 內容。
進階音訊
請參閱 rules/audio.md 了解進階音訊功能,例如修剪、音量、速度和音高。
動態持續時間、尺寸與數據
請參閱 rules/calculate-metadata.md 了解如何動態設定組合的持續時間、尺寸和 props。
進階組合
請參閱 rules/compositions.md 了解如何定義靜態影像、資料夾、預設 props 以及如何嵌套組合。
Google Fonts
這是在 Remotion 中載入字型的推薦方式。請參閱 rules/google-fonts.md 了解如何載入 Google Fonts。
本地字型
請參閱 rules/local-fonts.md 了解如何載入本地字型。
取得音訊長度
請參閱 rules/get-audio-duration.md 了解如何使用 Mediabunny 以秒為單位取得音訊檔案的長度。
取得影片尺寸
請參閱 rules/get-video-dimensions.md 了解如何使用 Mediabunny 取得影片檔案的寬度和高度。
取得影片長度
請參閱 rules/get-video-duration.md 了解如何使用 Mediabunny 以秒為單位取得影片檔案的長度。
GIF
請參閱 rules/gifs.md 了解如何顯示與 Remotion 時間軸同步的 GIF。
進階圖片
請參閱 rules/images.md 了解圖片的尺寸調整與定位、動態圖片路徑以及取得圖片尺寸。
光暈效果
請參閱 rules/light-leaks.md 了解如何使用 @remotion/light-leaks 製作光暈疊加效果。
Lottie 動畫
請參閱 rules/lottie.md 了解如何在 Remotion 中嵌入 Lottie 動畫。
Canvas 中的 HTML
如果你需要將 HTML 渲染到 <canvas> 中,以透過 <HtmlInCanvas> 套用 2D 或 WebGL 效果,請參閱 rules/html-in-canvas.md。
測量 DOM 節點
請參閱 rules/measuring-dom-nodes.md 了解如何在 Remotion 中測量 DOM 元素的尺寸。
測量文字
請參閱 rules/measuring-text.md 了解如何測量文字尺寸、將文字適配到容器以及檢查溢位。
進階序列化
請參閱 rules/sequencing.md 了解更多序列化模式——延遲、修剪、限制項目的持續時間。
TailwindCSS
請參閱 rules/tailwind.md 了解如何在 Remotion 中使用 TailwindCSS。
文字動畫
請參閱 rules/text-animations.md 了解排版與文字動畫模式。
進階時序
請參閱 rules/timing.md 了解進階時序,包含 interpolate、貝茲緩動以及彈簧效果。
轉場效果
請參閱 rules/transitions.md 了解場景轉場模式。
透明影片
請參閱 rules/transparent-videos.md 了解如何渲染出具有透明背景的影片。
修剪
請參閱 rules/trimming.md 了解修剪模式——剪裁動畫的開頭或結尾。
進階影片
請參閱 rules/videos.md 了解關於嵌入影片的進階知識——修剪、音量、速度、循環、音高。
參數化影片
請參閱 rules/parameters.md 了解如何透過新增 Zod schema 來使組合可參數化。
地圖
對於簡單且帶少量飛越效果的地圖,請考慮使用靜態地圖圖片。 對於具有動畫路線或飛越效果的複雜地圖,請載入地圖規則:rules/maplibre.md
旁白
請參閱 rules/voiceover.md 了解如何使用 ElevenLabs TTS 在 Remotion 組合中加入 AI 生成的旁白。