# Subtitle 元素

## 1. 结构

```ts
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`

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

## 3. 支持的字幕来源

### 内联数组

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

### 字幕素材 `$ref`

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

### 外部 URL

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

## 4. 外部字幕 URL 规则

远程地址必须返回以下任一 JSON 结构：
- `SubtitleWord[]`
- `{ "words": SubtitleWord[] }`

公开行为：
- 预览流程支持 `words: { "src": "..." }`
- 预览播放前会先归一化字幕数据
- 如果你绕过这些入口，直接把 `{ src }` 传给底层渲染器，字幕不会自动拉取

## 5. `SubtitleConfig`

```ts
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. 示例

```json
{
  "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)"
  }
}
```