webvtt

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 3, 2026 License: MIT Imports: 9 Imported by: 0

README

go-webvtt

Test Status Coverage Status MIT License PkgGoDev

A Go library for parsing WebVTT (Web Video Text Tracks) files.

Synopsis

package main

import (
	"fmt"
	"os"

	"github.com/Songmu/go-webvtt"
)

func main() {
	f, _ := os.Open("subtitles.vtt")
	defer f.Close()

	// Batch parsing - get all cues at once
	vtt, _ := webvtt.ParseAll(f)
	for _, cue := range vtt.Cues {
		fmt.Printf("[%v - %v] %s\n", cue.StartTime, cue.EndTime, cue.Voices[0].Text)
	}
}
// Stream parsing - process blocks one by one
f, _ := os.Open("subtitles.vtt")
defer f.Close()

for block, err := range webvtt.Parse(f) {
	if err != nil {
		log.Fatal(err)
	}
	switch b := block.(type) {
	case webvtt.Cue:
		fmt.Printf("Cue: %s\n", b.Voices[0].Text)
	case webvtt.Note:
		fmt.Printf("Note: %s\n", b.Text)
	}
}

Features

  • Stream-first design with iter.Seq2 iterator
  • Parses cues, notes, styles, and regions
  • Supports both timestamp formats: HH:MM:SS.mmm and MM:SS.mmm
  • Handles voice/speaker tags <v Speaker>text</v>
  • Parses cue settings (position, align, line, size, vertical, region)

Installation

% go get github.com/Songmu/go-webvtt

API

// Stream API - returns an iterator of blocks (Cue, Note, Style, Region)
func Parse(r io.Reader) iter.Seq2[Block, error]

// Batch API - parses all cues into a WebVTT struct
func ParseAll(r io.Reader) (*WebVTT, error)

Author

Songmu

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Parse

func Parse(r io.Reader) iter.Seq2[Block, error]

Parse parses WebVTT content and returns an iterator of blocks

Types

type Block

type Block interface {
	// contains filtered or unexported methods
}

Block is an interface for WebVTT blocks

type BlockType

type BlockType int

BlockType represents the type of a WebVTT block

const (
	BlockTypeCue BlockType = iota
	BlockTypeNote
	BlockTypeStyle
	BlockTypeRegion
)

type Cue

type Cue struct {
	ID        string
	StartTime time.Duration
	EndTime   time.Duration
	Settings  CueSettings
	Voices    []Voice
}

Cue represents a WebVTT cue block

type CueSettings

type CueSettings struct {
	Vertical string
	Line     string
	Position string
	Size     string
	Align    string
	Region   string
}

CueSettings represents cue settings

type Note

type Note struct {
	Text string
}

Note represents a NOTE block

type Region

type Region struct {
	ID       string
	Settings map[string]string
}

Region represents a REGION block

type Style

type Style struct {
	Text string
}

Style represents a STYLE block

type Voice

type Voice struct {
	Speaker string
	Text    string
}

Voice represents a voice span in cue text

type WebVTT

type WebVTT struct {
	Cues []Cue
}

WebVTT represents a parsed WebVTT file

func ParseAll

func ParseAll(r io.Reader) (*WebVTT, error)

ParseAll parses WebVTT content and returns a WebVTT struct containing all cues

Jump to

Keyboard shortcuts

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