subtitle

package
v0.0.0-...-ae8f3be Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 1, 2026 License: MIT Imports: 16 Imported by: 0

README

字幕处理

提供多种字幕格式的解析和处理功能。

支持格式

格式 说明 状态
SRT SubRip
ASS/SSA Advanced SubStation Alpha
WebVTT Web Video Text Tracks
CEA-608 隐藏字幕 (Line 21)
CEA-708 隐藏字幕 (DTV)

功能

  • 字幕解析: 从文件或流中解析字幕
  • 字幕烧录: 将字幕渲染到视频(硬字幕)
  • 软字幕封装: 将字幕封装到容器格式
  • 格式转换: 在不同字幕格式间转换

快速开始

// 解析 SRT 文件
cues, err := subtitle.ParseSRTFile("subtitle.srt")
if err != nil {
    log.Fatal(err)
}

for _, cue := range cues {
    fmt.Printf("%s -> %s: %s\n",
        cue.StartTime, cue.EndTime, cue.Text)
}
CEA-608/708 提取
// 从 TS 流中提取隐藏字幕
extractor := subtitle.NewCEAExtractor()
captions, err := extractor.Extract(data)

文件说明

文件 说明
subtitle.go 字幕核心类型和解析
srt.go SRT 格式解析
ass.go ASS/SSA 格式解析
webvtt.go WebVTT 格式解析
cea608.go CEA-608 提取
cea708.go CEA-708 提取

Documentation

Overview

Package subtitle 提供字幕处理功能

本包实现了多种字幕格式的解析和处理:

  • SRT (SubRip) 格式
  • ASS/SSA (Advanced SubStation Alpha) 格式
  • WebVTT (Web Video Text Tracks) 格式
  • CEA-608/708 隐藏字幕提取

支持字幕烧录(硬字幕)和软字幕封装。

Index

Constants

View Source
const (
	CEA708ServicePrimary   = 1 // 主字幕服务
	CEA708ServiceSecondary = 2 // 次字幕服务
)

CEA708 服务类型

Variables

View Source
var (
	ErrInvalidFormat    = errors.New("subtitle: invalid format")
	ErrInvalidTimestamp = errors.New("subtitle: invalid timestamp")
	ErrEmptySubtitle    = errors.New("subtitle: empty subtitle")
	ErrUnsupportedCodec = errors.New("subtitle: unsupported codec")
)

错误定义

Functions

This section is empty.

Types

type ASSParser

type ASSParser struct{}

ASSParser ASS/SSA 格式解析器

func (*ASSParser) Parse

func (p *ASSParser) Parse(r io.Reader) (*Subtitle, error)

Parse 解析 ASS/SSA 格式

type Burner

type Burner struct {
	// contains filtered or unexported fields
}

Burner 字幕烧录器

func NewBurner

func NewBurner(config BurnerConfig) *Burner

NewBurner 创建字幕烧录器

func (*Burner) BurnFrame

func (b *Burner) BurnFrame(img image.Image, pts time.Duration) image.Image

BurnFrame 在帧上烧录字幕

func (*Burner) SetSubtitle

func (b *Burner) SetSubtitle(sub *Subtitle)

SetSubtitle 设置字幕

type BurnerConfig

type BurnerConfig struct {
	FontSize     float64     // 字体大小
	FontColor    color.Color // 字体颜色
	OutlineColor color.Color // 轮廓颜色
	OutlineWidth int         // 轮廓宽度
	ShadowColor  color.Color // 阴影颜色
	ShadowOffset int         // 阴影偏移
	MarginBottom int         // 底部边距
	MarginSide   int         // 侧边距
	Alignment    int         // 对齐方式 (1=左, 2=中, 3=右)
	LineSpacing  int         // 行间距
}

BurnerConfig 字幕烧录配置

func DefaultBurnerConfig

func DefaultBurnerConfig() BurnerConfig

DefaultBurnerConfig 返回默认烧录配置

type CEA608Channel

type CEA608Channel int

CEA608Channel CEA-608 频道

const (
	CEA608CC1 CEA608Channel = iota // 字幕频道 1
	CEA608CC2                      // 字幕频道 2
	CEA608CC3                      // 字幕频道 3 (文本)
	CEA608CC4                      // 字幕频道 4 (文本)
)

type CEA608Extractor

type CEA608Extractor struct {
	// contains filtered or unexported fields
}

CEA608Extractor CEA-608 字幕提取器

func NewCEA608Extractor

func NewCEA608Extractor() *CEA608Extractor

NewCEA608Extractor 创建 CEA-608 提取器

func (*CEA608Extractor) GetSubtitle

func (e *CEA608Extractor) GetSubtitle() *Subtitle

GetSubtitle 获取提取的字幕

func (*CEA608Extractor) Process

func (e *CEA608Extractor) Process(data []byte, pts time.Duration) error

Process 处理 CEA-608 数据

func (*CEA608Extractor) Reset

func (e *CEA608Extractor) Reset()

Reset 重置提取器

type CEA708Extractor

type CEA708Extractor struct {
	// contains filtered or unexported fields
}

CEA708Extractor CEA-708 字幕提取器

func NewCEA708Extractor

func NewCEA708Extractor() *CEA708Extractor

NewCEA708Extractor 创建 CEA-708 提取器

func (*CEA708Extractor) GetSubtitle

func (e *CEA708Extractor) GetSubtitle() *Subtitle

GetSubtitle 获取提取的字幕

func (*CEA708Extractor) Process

func (e *CEA708Extractor) Process(data []byte, pts time.Duration) error

Process 处理 CEA-708 数据

func (*CEA708Extractor) Reset

func (e *CEA708Extractor) Reset()

Reset 重置提取器

type CEA708Window

type CEA708Window struct {
	ID          int
	Visible     bool
	RowLock     bool
	ColLock     bool
	Priority    int
	RelativePos bool
	AnchorV     int
	AnchorH     int
	AnchorPoint int
	RowCount    int
	ColCount    int
	PenStyle    int
	WindowStyle int

	// 内容
	Text bytes.Buffer
	Row  int
	Col  int
}

CEA708Window 窗口定义

type Cue

type Cue struct {
	Index     int           // 序号
	StartTime time.Duration // 开始时间
	EndTime   time.Duration // 结束时间
	Text      string        // 文本内容
	Style     *Style        // 样式(可选)
	Position  *Position     // 位置(可选)
}

Cue 字幕条目

func (*Cue) Duration

func (c *Cue) Duration() time.Duration

Duration 返回字幕持续时间

type Format

type Format int

Format 字幕格式

const (
	FormatUnknown Format = iota
	FormatSRT            // SubRip
	FormatASS            // Advanced SubStation Alpha
	FormatSSA            // SubStation Alpha
	FormatWebVTT         // Web Video Text Tracks
	FormatCEA608         // CEA-608 隐藏字幕
	FormatCEA708         // CEA-708 隐藏字幕
)

func DetectFormat

func DetectFormat(r io.Reader) (Format, error)

DetectFormat 检测字幕格式

func (Format) String

func (f Format) String() string

String 返回格式名称

type FrameProcessor

type FrameProcessor func(img image.Image, pts time.Duration) image.Image

BurnSubtitle 便捷函数:烧录整个字幕到视频帧序列

func CreateBurnProcessor

func CreateBurnProcessor(sub *Subtitle, config BurnerConfig) FrameProcessor

CreateBurnProcessor 创建烧录处理器

type Position

type Position struct {
	X        int     // X 坐标
	Y        int     // Y 坐标
	Align    string  // 对齐方式
	Line     int     // 行号
	Size     float64 // 大小百分比
	Vertical bool    // 垂直排列
}

Position 字幕位置

type SRTParser

type SRTParser struct{}

SRTParser SRT 格式解析器

func (*SRTParser) Parse

func (p *SRTParser) Parse(r io.Reader) (*Subtitle, error)

Parse 解析 SRT 格式

func (*SRTParser) Write

func (p *SRTParser) Write(w io.Writer, sub *Subtitle) error

Write 写入 SRT 格式

type Style

type Style struct {
	Name           string  // 样式名称
	FontName       string  // 字体名称
	FontSize       float64 // 字体大小
	Bold           bool    // 粗体
	Italic         bool    // 斜体
	Underline      bool    // 下划线
	StrikeOut      bool    // 删除线
	PrimaryColor   string  // 主颜色 (ARGB)
	SecondaryColor string  // 次颜色
	OutlineColor   string  // 轮廓颜色
	BackColor      string  // 背景颜色
	BorderStyle    int     // 边框样式
	Outline        float64 // 轮廓宽度
	Shadow         float64 // 阴影深度
	Alignment      int     // 对齐方式
	MarginL        int     // 左边距
	MarginR        int     // 右边距
	MarginV        int     // 垂直边距
}

Style 字幕样式

type Subtitle

type Subtitle struct {
	Format   Format            // 格式
	Title    string            // 标题
	Language string            // 语言
	Styles   map[string]*Style // 样式表
	Cues     []*Cue            // 字幕条目
	Metadata map[string]string // 元数据
}

Subtitle 字幕文件

func Convert

func Convert(sub *Subtitle, targetFormat Format) (*Subtitle, error)

Convert 转换字幕格式

func NewSubtitle

func NewSubtitle(format Format) *Subtitle

NewSubtitle 创建空字幕

func Parse

func Parse(r io.Reader, format Format) (*Subtitle, error)

Parse 自动检测格式并解析

func ParseString

func ParseString(s string, format Format) (*Subtitle, error)

ParseString 从字符串解析

func (*Subtitle) AddCue

func (s *Subtitle) AddCue(cue *Cue)

AddCue 添加字幕条目

func (*Subtitle) Duration

func (s *Subtitle) Duration() time.Duration

Duration 返回字幕总时长

func (*Subtitle) GetCueAt

func (s *Subtitle) GetCueAt(t time.Duration) []*Cue

GetCueAt 获取指定时间的字幕

type WebVTTParser

type WebVTTParser struct{}

WebVTTParser WebVTT 格式解析器

func (*WebVTTParser) Parse

func (p *WebVTTParser) Parse(r io.Reader) (*Subtitle, error)

Parse 解析 WebVTT 格式

func (*WebVTTParser) Write

func (p *WebVTTParser) Write(w io.Writer, sub *Subtitle) error

Write 写入 WebVTT 格式

Source Files

  • burner.go
  • cea608.go
  • cea708.go
  • subtitle.go

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL