这是编写 Remotion React 标记的指导。 如果与此无关,请改为加载 Remotion 最佳实践。
保留用户的更改
用户可能会在对话之外的代码中进行编辑。
如果您在此时检测到一个意外的更改,请不要覆盖它,假定该更改是有意的,或者请求确认。
通用规则
在组成或场景组件内部,从 useVideoConfig() 获取 fps,用于所有秒到帧的计算,包括 from、trimBefore、durationInFrames 和 premountFor 等 timing props。将表达式保留在 JSX 节点上,例如 trimBefore={4 * fps},这样可以让 Studio 编辑 timing,并在组成帧率发生变化时保持其正确性。
使用 useCurrentFrame() 和 interpolate() 驱动动画。
CSS transition 或 animation 无法正确渲染,需要进行重构。
Tailwind 动画类无法正确渲染,需要进行重构。
使用 Easing.bezier() 和 Easing.spring() 自定义动画曲线。
根据 Remotion 交互性最佳实践构建您的标记。
对于具有可编辑 props 的自定义视觉组件,优先使用 Interactive.withSchema({wrapInSequence: true}),并将可复用的场景注册为关联组成(connected compositions)。
将 timing 直接放在支持它的组件上;避免冗余的 <Sequence> 包装器。
为每个支持 premountFor 的定时组件提供一秒钟的预挂载:premountFor={fps},其中 fps 来自 useVideoConfig()。将此应用于媒体、交互式组件、<Sequence>、<Series.Sequence>、<TransitionSeries.Sequence> 和 <TransitionSeries.Overlay>,包括嵌套在场景中的定时组件。当嵌套项目需要在父级开始之前挂载时,也要预挂载父时间线项。没有 premountFor 的组件,如 <TransitionSeries.Transition>,无需替代。
Studio 编辑创建项的 JSX 源节点。将每个应独立编辑的组成注册、剪辑、场景、层和序列都编写为独立的 JSX 节点,其可编辑的 props 内联放置。 当生成的实例是作为一个源模板有意控制时,程序化循环是合适的,而不是当用户需要分别编辑这些实例时。
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 prop 中。
在 transform 上使用 scale、translate、rotate CSS 属性。
// 👍 内联可编辑的关键帧和变换简写
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 是一个可编辑的 prop。
淡入效果在调用点内联设置关键帧。
// 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、示例和操作顺序,请参见 Timing props。
地图
如果希望在视频中包含地图,请参见 Remotion 地图。
文本高亮和标注
有关文本高亮(高亮标记)、圆圈、下划线、删除线、划掉文本、方框等,请参见 text-highlights.md。
多场景视频
如果计划制作包含多个连续场景的视频,请参见 multi-scene-video.md。
关联组成 (Connected Compositions)
当一个场景或图层组需要自己可编辑的时间线时,请遵循 connected-compositions.md。在多场景视频中,对于重要的场景,优先选择此结构。
预组合 (Pre-compose) 操作
对于诸如 Pre-compose Ambient glow (src/BarChart.tsx:134) 之类的 Studio 请求,请在给定位置找到选定的序列标记。创建一个关联组成(connected composition),遵循 connected-compositions.md:将标记提取到一个命名的组件中,最好使用 Interactive.withSchema({wrapInSequence: true}) 使其可交互,并在根目录中使用唯一的 <Composition> 注册相同的导出组件引用。直接使用其 timing props 渲染交互式组件,或者当该包装器有用途时,将其作为序列的唯一子级渲染。如果选定的节点已经是一个序列,则保留其 props 并提取其子级。注册需要相当于其父级使用的尺寸、fps、时长和 defaultProps。没有注册组成的组件提取不能完成预组合请求。
将继承的样式(如 fontFamily)和字体加载带入提取的组件中;请参见 父级独立性。
画外音
有关使用 ElevenLabs TTS 向 Remotion 组成添加 AI 生成的画外音,请参见 voiceover.md。
嵌入视频
有关嵌入视频的高级知识——修剪、音量、速度、循环、音调,请参见 embedding-videos.md。
嵌入音频
有关修剪、音量、速度、音调等高级音频功能,请参见 audio.md。
裁剪
如果需要对组件的可见矩形进行裁剪,请参见 cropping.md。
过渡
有关场景过渡模式,请参见 transitions.md。
运动模糊
在添加运动模糊或运动轨迹时,请阅读 motion-blur.md 以了解首选的在 canvas 中使用 HTML 的方法、预览要求及替代方案。
视觉与像素效果
在添加视觉效果时,首先确定它应影响的元素或组。
如果目标组件支持 effects 并且下面的内置效果与请求匹配,则优先使用该效果而不是 CSS 近似。例如,如果需要晕影效果影响某个视频,直接将 vignette() 应用到 <Video> 的 effects prop 上。
以下组件直接接受 effects:
| 导入来源 | 组件 |
|---|---|
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 prop 放在包装器上。
对于其他元素,在适用的情况下首选普通的 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(),colorWipe(),cropAndScale(),halftone(),mirror(),noise(),pixelate(),posterize().