Skip to content

Class: WebVttParser ​

Defined in: libs/webvtt/src/WebVttParser.ts:32

A WebVTT parser.

Example ​

ts
const vtt = `WEBVTT\n\nREGION\nid:test\n\nSTYLE\n::cue {}\n\nCUE_1\n00:00:00.000 --> 00:00:35.000\nWevVTT Sample\n`
const cues = [] as WebVttCue[]
const errors = [] as WebVttParsingError[]
const regions = [] as WebVttRegion[]
const styles = [] as string[]

const parser = new WebVttParser()
parser.oncue = cue => cues.push(cue)
parser.onparsingerror = error => errors.push(error)
parser.onregion = region => regions.push(region)
parser.onstyle = style => styles.push(style)
parser.parse(vtt)
parser.flush()

assert(cues[0].id === 'CUE_1')
assert(cues[0].text === 'WevVTT Sample')
assert(errors.length === 0)
assert(regions[0].id === 'test')
assert(styles.length === 1)

See ​

WebVTT Specification

Constructors ​

Constructor ​

ts
new WebVttParser(options?): WebVttParser;

Defined in: libs/webvtt/src/WebVttParser.ts:77

Create a new WebVTT parser.

Parameters ​

options? ​

WebVttParserOptions = {}

The options to use for the parser.

Returns ​

WebVttParser

Properties ​

oncue? ​

ts
oncue?: (cue) => void;

Defined in: libs/webvtt/src/WebVttParser.ts:60

A callback function that is called when a cue is parsed.

Parameters ​

cue ​

WebVttCue

Returns ​

void


onflush? ​

ts
onflush?: () => void;

Defined in: libs/webvtt/src/WebVttParser.ts:70

A callback function that is called when the parser is flushed.

Returns ​

void


onparsingerror? ​

ts
onparsingerror?: (error) => void;

Defined in: libs/webvtt/src/WebVttParser.ts:45

A callback function that is called when a parsing error occurs.

Parameters ​

error ​

WebVttParsingError

Returns ​

void


onregion? ​

ts
onregion?: (region) => void;

Defined in: libs/webvtt/src/WebVttParser.ts:50

A callback function that is called when a region is parsed.

Parameters ​

region ​

WebVttRegion

Returns ​

void


onstyle? ​

ts
onstyle?: (style) => void;

Defined in: libs/webvtt/src/WebVttParser.ts:65

A callback function that is called when a style is parsed.

Parameters ​

style ​

string

Returns ​

void


ontimestampmap? ​

ts
ontimestampmap?: (timestampMap) => void;

Defined in: libs/webvtt/src/WebVttParser.ts:55

A callback function that is called when a timestamp map is parsed.

Parameters ​

timestampMap ​

TimestampMap

Returns ​

void

Methods ​

flush() ​

ts
flush(): WebVttParser;

Defined in: libs/webvtt/src/WebVttParser.ts:435

Flush the parser.

Returns ​

WebVttParser

The parser.


parse() ​

ts
parse(data?, reuseCue?): WebVttParser;

Defined in: libs/webvtt/src/WebVttParser.ts:95

Parse the given data.

Parameters ​

data? ​

string

The data to parse.

reuseCue? ​

boolean = false

Whether to reuse the cue.

Returns ​

WebVttParser

The parser.

A Streaming Video Technology Alliance project.