这是编写 Remotion React 标记的指南。 如果与此无关,请改为加载 Remotion 最佳实践。
保留用户的更改
用户可能会在对话之外对代码进行编辑。
如果您检测到在此期间发生了意外的更改,请不要覆盖它,假设它是故意的或请求确认。
通用规则
使用 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}),
}),
}}
// 👎 非内联值和变换字符串在 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 字幕栏的背景视频。
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: "文字" },
accentColor: {
type: "color",
default: "#0b84f3",
description: "强调色",
},
} 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。没有注册组合的组件提取不能完成预组合请求。
语音旁白
有关使用 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 或其他 Web 技术
- 直接对元素(
<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 字体的信息,请参阅 google-fonts.md。
本地字体
有关如何加载本地字体的信息,请参阅 local-fonts.md。
GIF
有关如何显示与 Remotion 时间线同步的 GIF 的信息,请参阅 gifs.md。
高级图像
有关调整图像大小和位置、动态图像路径以及获取图像尺寸的信息,请参阅 images.md。
Lottie 动画
有关在 Remotion 中嵌入 Lottie 动画的信息,请参阅 lottie.md。
计时
更多关于 interpolate() 的计时技巧,请参阅 timing.md。
参数化视频
有关通过添加 Zod 模式使组合可参数化的信息,请参阅 parameters.md。
测量 DOM 节点
有关在 Remotion 中测量 DOM 元素尺寸的信息,请参阅 measuring-dom-nodes.md。
测量文本
有关测量文本尺寸、使文本适合容器以及检查溢出,请参阅 measuring-text.md。
使用 FFmpeg
对于某些视频操作,例如修剪视频或检测静音,应使用 FFmpeg。加载 ./ffmpeg.md 文件以获取更多信息。
静音检测
当需要检测并修剪视频或音频文件中的静音片段时,加载 ./silence-detection.md 文件。
动态时长、尺寸和数据
有关动态设置组合时长、尺寸和属性的信息,请参阅 calculate-metadata.md。
组合与静态帧
在注册 <Composition> 或 <Still> 元素之前,请阅读 compositions.md 以了解源可编辑注册、文件夹、默认属性和嵌套。对于 Studio 导航进入场景自己的时间线,请使用 连接组合。
高级排序
更多排序模式(延迟、修剪、限制项目持续时间)请参阅 sequencing.md。
安装模块
使用 npx remotion add 添加具有正确版本的新包:
npx remotion add
…