Communitygithub.com

sayyed73/ai-moderator-demo-video

结构化 Remotion 标记以实现可交互性

ai-moderator-demo-video 是什么?

该技能详细说明了如何通过编写特定结构的 Remotion 标记,使 Remotion Studio 能够识别代码结构并使其具备交互性。它指导开发者如何创建可交互组件,例如允许用户通过点击选择项目、拖放、调整大小和旋转,以及编辑 CSS 样式和关键帧值。技能强调了使用 `Interactive.withSchema({wrapInSequence: true})` 创建具有独立时间线的自定义场景或组件的重要性,并提供了完整的 TypeScript 示例(包括 `LowerThird` 组件的实现)。此外,技能还涵盖了如何将可复用组件注册为“连接合成”、如何将时序直接放在组件上、如何为每个可独立编辑的项创建单独的 JSX 节点,以及如何通过 `interpolate()` 进行内联动画。最后,它给出了保持 CSS 内联、使用描述性名称和优先使用内联文本等最佳实践。该技能适用于所有基于 Remotion 的开发平台,并通过清晰的规范和丰富的代码示例帮助开发者充分利用 Remotion Studio 的交互式编辑功能。

兼容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/sayyed73/ai-moderator-demo-video/tree/HEAD/.claude/skills/remotion-interactivity

在你喜欢的 AI 中提问

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

预览

来自技能 README

ai-moderator-demo-video preview

文档

通过以特定方式编写 Remotion 标记,Remotion Studio 能够识别代码结构并使其具备交互性:

  • 允许通过点击选择项目
  • 允许拖放、调整大小和旋转
  • 编辑 CSS 样式
  • 使关键帧和缓动值可编辑

如果标记对于 Studio 来说过于复杂而无法使其具有交互性,则这些值将变为灰色。

优先使用具有自身时间线的交互式组件

对于自定义场景、卡片、标题以及其他应该按实例可编辑属性的视觉组件,请使用 Interactive.withSchema({wrapInSequence: true})。在 InteractivitySchema 中公开有意义的內容和外观控制。将装饰性的实现细节保留在组件内部。

该组件必须接受 style 并将其转发到一个视觉根元素。当一个共享的根元素的变换、裁剪或不透明度应影响其所有层时,请保持该根元素共享。当冗余的内部包装器的样式可以移动到现有元素上而不改变布局或动画时,请展平这些包装器。

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,
});

在组件内部转发注入的 style 是必需的;在组件调用点保持内联样式可编辑。

选择 schema 字段

Schema 中的每个键都是一个属性,Studio 可以在调用点读取并编辑它。不在 schema 中的属性仍然有效,但在 Studio 中不可编辑。键可以使用点号表示法,例如 style.color。

类型用途可添加关键帧
text-content文本,例如标题否
number数字,具有可选的 min、max、step、integer是
boolean开/关开关是(保持)
colorCSS 颜色字符串是
enum在 variants 之间选择,每个选项可以有自己的嵌套 schema可选加入(保持)
array数字、颜色、枚举等的列表否
asset媒体源,具有可选的 assetType否
font-familyCSS 字体系列否
font-weight字体粗细是
translate、scale、rotation-css、rotation-degrees、transform-origin变换是
uv-coordinate元素上的归一化 [x, y] 点是
svg-pathSVG 路径数据是
remotion-captionsCaption[] 数据否
hidden保持在控件之外的值—

每个字段都需要一个 default 值,并且应该有一个 description 描述,Studio 将其显示为标签。 设置 keyframable: false 以显示静态控件。

公开内容以及每个实例的外观,例如文本和强调色。 不要为 style.translate、style.scale、style.rotate、style.transformOrigin 和 style.opacity 添加字段:wrapInSequence: true 已经添加了它们。

要使根元素的 CSS 属性按实例可编辑,请展开内置的 schema 片段:

const cardSchema = {
  ...Interactive.textSchema, // style.color, style.fontSize, style.fontWeight, ...
  ...Interactive.backgroundSchema, // style.backgroundColor
  ...Interactive.borderRadiusSchema, // style.borderRadius, ...
} as const satisfies InteractivitySchema;

也可用:Interactive.borderSchema,以及用于接受字幕的组件的 Interactive.captionsSchema。\n有关所有选项,请参阅 InteractivitySchema。

将可复用组件注册为连接合成

将具有自身图层或动画的重要场景和可复用组件注册为独立的合成。Studio 将这些称为连接合成:它们可以在自身的时间线中打开,同时与父视频共享相同的组件实现。

在组件内部定义所需的样式并加载字体,以便它在没有父组件的情况下也能正常工作。请参阅父组件独立性。

注册与父组件渲染的导出组件引用相同的组件。对于上面的示例,使用 component={LowerThird},而不是 LowerThirdInner 或内联包装器。 使用 wrapInSequence: true 时,连接不需要额外的 <Sequence>。

import {Composition} from 'remotion';
import {LowerThird} from './LowerThird';

export const RemotionRoot = () => (
  <Composition
    id="LowerThird"
    component={LowerThird}
    width={1280}
    height={720}
    fps={30}
    durationInFrames={30}
    defaultProps={{
      children: '简·多伊,产品设计师',
      accentColor: '#0b84f3',
    }}
  />
);

选择唯一的 ID、有代表性的内联默认值,以及适合预览组件的尺寸、fps 和时长。注册默认值不会自动从父实例复制属性。请参阅连接合成以了解注册和提取细节。

直接将时序放在组件上

避免在已经接受时序属性的组件周围使用单独的 <Sequence>。内置交互式组件和通过 wrapInSequence: true 创建的自定义组件可以直接接收 name、from、durationInFrames、trimBefore、playbackRate 和 premountFor。

<LowerThird
  name="下方三分之一"
  from={30}
  durationInFrames={90}
  accentColor="#0b84f3"
>
  简·多伊,产品设计师
</LowerThird>

在移除冗余序列时,将其时序和名称移动到子组件。保留任何现有的子组件时序;不要覆盖或重复应用偏移量。组件内部的 useCurrentFrame() 调用读取其本地帧,而调用点的样式表达式仍使用调用者的帧。在移动动画样式时保持这种区别。

当 <Sequence> 为多个同级组件提供共享时序、覆盖 useVideoConfig() 的尺寸或包装不处理时序的组件时,请保留它。为连续布局保留 <Series.Sequence>,为过渡保留 <TransitionSeries.Sequence>;直接的 from 属性不能替代这些行为。

为每个可独立编辑的项创建单独的 JSX 节点

Studio 编辑创建了一个项目的 JSX 源代码节点。如果多个运行时项目来自同一个 JSX 节点,它们共享一个源代码编辑目标。

对于每个合成注册、剪辑、场景、图层或序列(如果它们应该单独可编辑),请编写一个单独的 JSX 节点,并将其可编辑属性保留在该节点上。这适用于 <Composition>、<Still>、内置媒体组件、<Sequence>、<Series.Sequence>、<TransitionSeries.Sequence> 和自定义组件。

例如,像这样编写一个可编辑的时间线:

<Series>
  <Series.Sequence name="介绍" durationInFrames={90}>
    <Introduction />
  </Series.Sequence>
  <Series.Sequence name="演示" durationInFrames={150}>
    <Demo />
  </Series.Sequence>
</Series>

.map() 或其他程序化循环会从一个 JSX 源节点创建多个运行时项目。这对于有意作为单个模板控制重复输出(例如可视化条形图或粒子)是合适的。但当实例在 Studio 中需要独立的 ID 或名称、属性、元数据、时序、排序、删除或复制时,则不合适。

使用 Interactive 使 HTML 元素具有交互性

除了 <Img>(它已经是交互式的)之外,每个 HTML 和 SVG 元素(例如 <div>)都可以使用 Interactive 变为交互式:

<Interactive.Div
  name="问候卡"
  style={{fontSize: 80, padding: 24}}
>
  你好
</Interactive.Div>

这允许在 Studio 中设置样式和关键帧。要明智地使用,如果组件有许多元素,时间线可能会变得混乱。

优先使用内联文本

如果文本是固定的且只使用一次,请直接将其写在交互式元素内部,而不是将其提取到常量中。

// 👍 固定副本保持可编辑
<Interactive.Div name="标题">
  Remotion 最佳实践
</Interactive.Div>

仅当文本是动态的或重复使用时,才使用属性或变量。

为交互式元素提供描述性名称

为元素添加 name 属性,使其易于识别。 避免计算名称,直接硬编码它们。

<>
  <Interactive.Div name="主标题" style={{fontSize: 80}}>
    发布日
  </Interactive.Div>
  <Img name="头像" src="https://remotion.media/image.jpeg" />
  <Video name="背景" src="https://remotion.media/video.mp4" />
  <Interactive.Div name="标题">
    发布日
  </Interactive.Div>
</>

将所有 CSS 样式保持内联

最好的方法是将纯对象传递给 style - 不要引用常量,不要展开对象,不要做数学运算。

<Interactive.Div
  style={{
    fontSize: 80,
    color: 'red',
  }}
>
  你好,世界!
</Interactive.Div>
const baseStyle = useMemo(() => {
  return {
    fontSize: 12 // ❌ 不支持非内联样式
  }
}, []);

<Interactive.Div
  style={{
    ...baseStyle, // ❌ 不支持展开
    color: RED, // ❌ 不支持引用常量
    scale: frame * 10 // ❌ 不支持数学运算
  }}
>
  你好,世界!
</Interactive.Div>

使用 interpolate() 制作动画

将动画编写为在发生变化的属性上的内联 interpolate() 调用。 输出范围、缓动、外推和 output 属性应使用硬编码值。

输入范围还可以使用从 useVideoConfig() 中解构出来的 durationInFrames、fps、width 和 height。支持像 durationInFrames 这样的裸标识符、与数字相乘(例如 2 * fps 或 fps * 2),以及减去一个数字(例如 durationInFrames - 1)。

const {fps, durationInFrames} = useVideoConfig();

// 👍 内联值可以进行标准化和关键帧化
<Interactive.Div
  name="产品卡片"
  style={{
    color: 'white',
    fontSize: 80,
    scale: interpolate(frame, [0, fps], [0, 1], {
      easing: Easing.spring({damping: 200}),
      output: 'perceptual-scale',
      extrapolateLeft: 'clamp',
      extrapolateRight: 'clamp'
    }),
    rotate: interpolate(frame, [0, 1 * fps], ['0deg', '20deg'], {
      easing: Easing.spring({damping: 200}),
      extrapolateLeft: 'clamp',
      extrapolateRight: 'clamp'
    }),
    translate: interpolate(
      frame,
      [durationInFrames - 30, durationInFrames],
      ['0px 0px', '0px 120px'],
      {
        easing: Easing.spring({damping: 200}),
        output: 'perceptual-scale',
        extrapolateLeft: 'clamp',
        extrapolateRight: 'clamp'
      }
    ),
  }}
/>
const translateY = interpolate(frame, [0, 30], [0, 120]); // ❌ 数学运算应直接在标记中

<Interactive.Div
  name="产品卡片"
  style={{
    translate: translateY, // ❌ 仅支持内联 interpolate() 调用
    rotate: interpolate(frame, [start, start + 10], [0, Math.PI]), // ❌ 不能对任意变量使用数学运算,不能使用常量
    scale
  }}
/>

Individual skills in this repo

This repo contains 5 individual skills — each has its own dedicated page.

相关技能