这是关于编写 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}),
}),
}}
// 👎 非内联值和变换字符串在 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。
示例场景
一个带有下方三分之一区域的背景视频。
下方三分之一是一个交互式组件,有自己的时间线,注册为已连接组合。
其文本作为 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",
}}
/>
</>
);
};
延迟与裁剪
将时序直接放在支持它的组件上。有关支持的属性、示例和操作顺序,请参阅 Timing props。
地图
如果希望在视频中包含地图,请参阅 Remotion Maps。
文本高亮和注释
有关文本高亮(高亮标记)、圆圈、下划线、删除线、划掉文本、文本框,请参阅 text-highlights.md。
多场景视频
如果计划制作包含多个后续场景的视频,请参阅 multi-scene-video.md。
已连接组合
当一个场景或一组图层需要拥有自己的可编辑时间线时,请遵循 connected-compositions.md。在多场景视频中,对于重要的场景,优先使用此结构。
预合成操作
对于 Studio 的请求,例如 预合成 Ambient glow (src/BarChart.tsx:134),找到给定位置处的选定序列标记。创建一个已连接组合,遵循 connected-compositions.md:将标记提取到一个命名组件中,最好使用 Interactive.withSchema({wrapInSequence: true}) 使其成为交互式组件,并在根目录中使用唯一的 <Composition> 注册相同的导出组件引用。直接使用其时序属性渲染交互式组件,或者当包装器有特定用途时,将其作为序列的唯一子元素渲染。如果选定的节点已经是一个序列,则保留其属性并提取其子元素。注册需要尺寸、fps、持续时间和与其父级使用情况等效的 defaultProps。未注册组合的组件提取不能完成预合成请求。
将继承的样式(例如 fontFamily 和字体加载)带入提取的组件中;有关更多信息,请参阅 parent independence。
旁白
有关使用 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 Maps。
字幕
在处理字幕或副标题时,请加载 Remotion Captions 技能以获取更多信息。
Google Fonts
这是在 Remotion 中加载字体的推荐方式。有关如何加载 Google Fonts,请参阅 google-fonts.md。
本地字体
有关如何加载本地字体,请参阅 local-fonts.md。
GIF
有关如何显示与 Remotion 时间线同步的 GIF,请参阅 gifs.md。
高级图像
有关调整图像大小和位置、动态图像路径以及获取图像尺寸,请参阅 images.md。
Lottie 动画
有关在 Remotion 中嵌入 Lottie 动画,请参阅 lottie.md。
时序
有关 interpolate() 的更多时序技巧,请参阅 timing.md。
参数化视频
有关通过添加 Zod schema 使组合可参数化,请参阅 parameters.md。
测量 DOM 节点
有关在 Remotion 中测量 DOM 元素尺寸,请参阅 measuring-dom-nodes.md。
测量文本
有关测量文本尺寸、将文本适配到容器以及检查溢出,请参阅 measuring-text.md。
使用 FFmpeg
对于某些视频操作,例如裁剪视频或检测静音,应使用 FFmpeg。请加载 ./ffmpeg.md 文件以获取更多信息。
静音检测
当需要检测并裁剪视频或音频文件中的静音片段时,请加载 ./silence-detection.md 文件。
动态时长、尺寸和数据
有关动态设置组合时长、尺寸等,请参阅 calculate-metadata.md。