元素参考

Subtitle 元素

1. 结构

type SubtitleSource =
  | SubtitleWord[]
  | { $ref: string }
  | { src: string };

interface SubtitleClip extends BaseClip {
  type: "subtitle";
  words: SubtitleSource;
  config?: SubtitleConfig;
}

SubtitleClip 继承 BaseClip,因此也支持:

  • opacity
  • animations
  • keyframes
  • transition
  • transform
  • zIndex

2. SubtitleWord

interface SubtitleWord {
  word: string;
  punctuated_word: string;
  start: number;
  end: number;
  confidence?: number;
}

3. 支持的字幕来源

内联数组

{
  "words": [
    {
      "word": "hello",
      "punctuated_word": "Hello",
      "start": 0,
      "end": 0.4
    }
  ]
}

字幕素材 $ref

{
  "words": { "$ref": "subtitle-main" }
}

外部 URL

{
  "words": { "src": "https://example.com/subtitle.json" }
}

4. 外部字幕 URL 规则

远程地址必须返回 JSON。

支持两种结构:

  • SubtitleWord[]
  • { "words": SubtitleWord[] }

真实流程:

  • POST /api/json 时会先拉取并归一化
  • 浏览器播放器初始化前也会再归一化一次

注意:

  • 如果你绕过这些入口,直接把 { src } 扔给底层渲染器,字幕不会自动拉取

5. SubtitleConfig

interface SubtitleConfig {
  mode?: "batch" | "stream";
  wordsPerBatch?: number;
  fontSize?: number;
  fontFamily?: string;
  fontWeight?: number | string;
  textAlign?: "left" | "center" | "right";
  textBoxWidth?: number | string;
  textColor?: string;
  highlightColor?: string;
  backgroundColor?: string;
  shadowColor?: string;
  shadowBlur?: number;
  borderColor?: string;
  borderWidth?: number;
  fadeInAnimation?: boolean;
  position?: "bottom" | "top" | "center";
  paddingBottom?: number;
}

6. 当前已实现配置

  • mode
  • wordsPerBatch
  • fontSize
  • fontFamily
  • fontWeight
  • textAlign
  • textBoxWidth
  • textColor
  • highlightColor
  • backgroundColor
  • shadowColor
  • shadowBlur
  • borderColor
  • borderWidth
  • fadeInAnimation
  • position
  • paddingBottom

7. 模式说明

batch

  • 按批次显示
  • 会高亮当前朗读词

stream

  • 单词逐个出现

8. 整体动画说明

字幕整体动画作用在整个 subtitle 容器上,不影响逐词高亮逻辑。

注意:

  • config.fadeInAnimation 是批次内文字淡入
  • animations / keyframes / transition 是整个字幕块的整体动画

例如可以给整块字幕加淡入、滑入、位移动画,而不是只让单词本身变化。

9. 示例

{
  "type": "subtitle",
  "start": 0,
  "duration": 8,
  "words": { "$ref": "sub-main" },
  "animations": [
    { "type": "slideIn", "duration": 0.4, "direction": "up" }
  ],
  "config": {
    "position": "bottom",
    "fontSize": 60,
    "highlightColor": "#22d3ee",
    "backgroundColor": "rgba(0,0,0,0.4)"
  }
}