Communitygithub.com

remotion-video

此技能用于当用户要求“用Remotion制作视频”、“在React中创建程序化/数据驱动视频”、“从代码渲染MP4/GIF”、“使用useCurrentFrame/interpolate/spring进行动画制作”、“将视频同步到音频/节拍”或“为每条记录无头渲染模板化视频”。涵盖构建React合成并通过CLI或@remotion/renderer进行渲染。

remotion-video 是什么?

此技能专注于使用Remotion框架构建程序化视频。Remotion是一个基于React的革命性视频创作工具,允许开发者通过代码生成确定性的MP4、GIF和WebM视频。其核心优势在于每一帧都是useCurrentFrame()的纯函数,这意味着输出完全可预测、可搜索、可比较,并且可以在CI环境中无头渲染。它全面支持帧驱动动画(使用interpolate和spring)、基于序列和时间线的剪辑调度、音频同步和节拍检测、以及通过Zod实现参数化属性设置。技能涵盖了从基本的动画合成到复杂的批量数据驱动渲染流水线,包括使用CLI和编程API进行渲染。关键实践包括:始终使用帧驱动而非状态驱动的动画、通过渲染静止帧进行验证、以及避免使用破坏确定性的Math.random()或Date.now()。该技能支持多平台,包括CLI和Node.js编程接口,适用于社交视频片段、标题卡片、解释性视频、数据驱动的运动图形等场景。

兼容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/waysvan077/van-skills/tree/HEAD/remotion-video

在你喜欢的 AI 中提问

打开一个已预加载此 Agent Skill 的新对话。

文档

Remotion(程序化视频)

在React中构建真正的MP4/GIF/WebM视频。每一帧都是useCurrentFrame()的纯函数,因此输出是确定性的、可滚动浏览、可比较、并且可在CI中渲染。对于模板化和数据驱动的动态图形,它是After Effects的代码优先替代方案。

使用场景

  • 从代码渲染MP4/GIF/WebM(社交视频片段、标题卡片、解说视频)。
  • 模板化/数据驱动视频:一个合成,通过属性(每个用户、每条记录、CSV/DB的每一行)生成多个输出。
  • 必须进行版本控制、代码审查并在没有GUI的情况下在CI中渲染的动态图形。
  • 程序化音频同步、根据数据生成动画的图表,或将着色器/Three.js嵌入视频中。

核心技术

帧驱动动画

动画从当前帧派生,绝不使用setState或requestAnimationFrame。interpolate将输入范围映射到输出范围;spring产生物理上自然的运动。

import {useCurrentFrame, useVideoConfig, interpolate, spring} from 'remotion';

export const Title = () => {
  const frame = useCurrentFrame();
  const {fps} = useVideoConfig();

  // 在0-30帧之间淡入,然后保持(clamp防止超出范围)。
  const opacity = interpolate(frame, [0, 30], [0, 1], {
    extrapolateLeft: 'clamp',
    extrapolateRight: 'clamp',
  });

  // 弹簧入场效果;除以弹簧持续时间是不必要的——直接驱动变换。
  const scale = spring({frame, fps, config: {damping: 200, mass: 1, stiffness: 100}});

  return <h1 style={{opacity, transform: `scale(${scale})`}}>Hello</h1>;
};

除非需要有意超调,否则始终对interpolate进行clamp——默认情况下它会线性外推到范围之外,这会产生大于1的透明度或负值。

调度:Sequence 和 Series

<Sequence>会偏移时间:子组件看到frame在序列的from处重置为0。使用它在时间线上放置剪辑。<Series>可以连续排列片段,无需手动偏移计算。

import {Sequence, Series, AbsoluteFill} from 'remotion';

export const Timeline = () => (
  <AbsoluteFill style={{backgroundColor: 'black'}}>
    <Sequence from={0} durationInFrames={60}><Intro /></Sequence>
    <Sequence from={60} durationInFrames={90}><Body /></Sequence>

    {/* Series自动排序;offset使前一个片段重叠以实现交叉淡入淡出 */}
    <Series>
      <Series.Sequence durationInFrames={60}><ShotA /></Series.Sequence>
      <Series.Sequence durationInFrames={60} offset={-15}><ShotB /></Series.Sequence>
    </Series>
  </AbsoluteFill>
);

合成注册 + 参数化属性

<Composition>声明id、尺寸、fps、时长和defaultProps。Zod模式使属性类型安全,并可在Studio侧边栏中编辑。

import {Composition} from 'remotion';
import {z} from 'zod';

export const schema = z.object({
  title: z.string(),
  accent: z.string(),
  fps: z.number().default(30),
});

export const Root = () => (
  <Composition
    id="Promo"
    component={Promo}
    durationInFrames={150}
    fps={30}
    width={1080}
    height={1920}
    schema={schema}
    defaultProps={{title: 'Launch', accent: '#5b8cff', fps: 30}}
  />
);

要使时长依赖于数据,可以在Composition上使用calculateMetadata,在渲染前根据属性(例如,行数 × 每行帧数)计算durationInFrames。

音频和节拍同步

import {Audio, staticFile, useCurrentFrame, useVideoConfig} from 'remotion';

const BEATS_SEC = [0.5, 1.0, 1.5, 2.0]; // 离线检测

export const Music = ({children}) => {
  const frame = useCurrentFrame();
  const {fps} = useVideoConfig();
  const t = frame / fps;
  const onBeat = BEATS_SEC.some((b) => Math.abs(t - b) < 1 / fps);
  return (
    <>
      <Audio src={staticFile('track.mp3')} />
      <div style={{transform: `scale(${onBeat ? 1.06 : 1})`}}>{children}</div>
    </>
  );
};

离线检测节拍(例如使用web-audio-beat-detector或aubio),并将时间戳烘焙到属性中——切勿在渲染时分析音频,因为无头渲染没有实时音频时钟。

渲染

在基于浏览器的Studio中预览;通过CLI或编程API进行无头渲染。

npx remotion studio                                  # 交互式预览
npx remotion render Promo out/promo.mp4 \
  --props='{"title":"Launch","accent":"#f43"}'        # 传递参数化属性
npx remotion render Promo out.gif --codec=gif         # GIF输出

对于批量/数据驱动流水线,使用@remotion/renderer在Node中渲染(一次性打包,多次渲染)——请参阅参考文件。

交付与验证(渲染静态帧 → MP4)

打包辅助工具(scripts/):使用scripts/contact-sheet.sh sheet.png f-hook.png f-mid.png f-end.png拼接您的静态帧,然后使用scripts/probe-mp4.sh out.mp4 [WxH] [fps]验证编码。查看scripts/README.md。

Remotion在结构上是帧确定性的——每一帧都是useCurrentFrame()的纯函数,因此你可以在无需任何seek工具的情况下无头渲染任何精确帧(这相当于网页场景中?t=N的重型对应物)。当输出必须是MP4/GIF、必须携带精确的数字/文本、或需要从数据批量生成时,使用此层级;对于轻量级网页动画,请改为交付独立的HTML。

输出约定:

  • 一个Remotion项目,包含注册的合成(<Composition> + zod schema + defaultProps),所有动画均为帧驱动(无定时器 / Date.now() / Math.random())。
  • 可交付物 = 渲染后的 out/*.mp4(以及项目文件,以便用户可以使用新属性/数据重新渲染)。
  • 时长依赖于数据?在calculateMetadata中计算,而不是手动指定。

验证循环——渲染静态帧 → 检查 → 编码。 首先渲染单帧(廉价,无需视频编码),检查它们,只有在帧正确后才编码完整视频。

# 1. 在开始/中间/结尾处渲染帧精确的静态帧(PNG,无头,快速)
npx remotion still Promo out/f-start.png --frame=0
npx remotion still Promo out/f-mid.png   --frame=75
npx remotion still Promo out/f-end.png   --frame=149   # 最后一帧 = durationInFrames - 1
#    使用你将要交付的相同属性渲染,而不仅仅是defaultProps:
#    npx remotion still Promo out/f-mid.png --frame=75 --props='{"title":"Launch"}'

# 2. 检查每个PNG——保真度(符合需求;数字/文本正确)和
#    瑕疵(文本溢出、画布外、裁剪的安全区域、缺少字体、错误的数据绑定)。

# 3. 只有在静态帧检查通过后,再编码视频:
npx remotion render Promo out/promo.mp4 --props='{"title":"Launch"}'
  • 使用npx remotion compositions读取每个合成的durationInFrames/fps并选择结束帧。
  • 数据驱动 / 批量:在批量渲染所有行之前,先通过静态帧验证一个代表性的属性集——一次性捕获布局错误,而不是N次。
  • 免费获得README演示GIF:npx remotion render Promo out/demo.gif --codec=gif生成首屏证明剪辑(方向D)。

在完成之前:

  1. npx remotion still在第0帧、中间帧和最后一帧干净地渲染——无错误,无缺失资源/字体。
  2. 在每个检查的帧上,数字/文本精确且在安全区域内。
  3. 仅帧驱动——无Date.now() / Math.random() / 定时器(确定性在CI中保持)。
  4. 属性是Zod类型化的;交付的属性正确渲染(不仅仅是defaultProps)。
  5. 完整的MP4已编码并可播放;(可选)为README渲染了GIF。

快速参考

需求使用
将时间映射为值interpolate(frame, [a,b], [x,y], {extrapolateRight:'clamp'})
自然缓动spring({frame, fps, config})
在时间t放置剪辑<Sequence from durationInFrames>
连续镜头<Series>
音频<Audio src={staticFile(...)} />
帧 ↔ 秒frame / fps
循环值frame % period 然后 interpolate
可编辑属性<Composition>上的 zod schema
CI渲染npx remotion render 或 @remotion/renderer

注意事项

  • 切勿使用Math.random()、Date.now()或动画定时器——它们会破坏确定性。使用Remotion中的random(seed)来获得稳定的每帧随机性。
  • 通过staticFile()加载字体和资源,并使用delayRender/continueRender等待,否则字体会在渲染中途出现。
  • 默认的interpolate会外推——请clamp它。
  • <Sequence>内部的useCurrentFrame是本地的(从0开始);使用useVideoConfig().durationInFrames来获取绝对时间。

参考文件

  • references/api-and-patterns.md — interpolate选项和Easing、spring配置配方、Sequence/Series调度、<Audio> + 节拍同步、带zod和calculateMetadata的参数化defaultProps、CLI标志、程序化@remotion/renderer批量渲染,以及嵌入GLSL着色器/Three.js(@remotion/three)。

相关技能