Methods

(static) parseVTT(text) → {object}

Parses a WebVTT file into caption lines.

A WebVTT file is blocks separated by blank lines, and only a cue's text is shown: the lines after its timing line, up to the next blank line. Everything else (the WEBVTT header, NOTE / STYLE / REGION blocks, and a cue identifier, the optional line BEFORE a timing line) is skipped, because none of it follows a timing line within its block.

A cue whose timing cannot be read is dropped whole, and its text with it: attaching that text to the previous cue's timing would show it at the wrong moment.

  • start / end in milliseconds; one entry per text line
  • skippedCues counts cues dropped for an unreadable timing line, firstSkipped quotes the first of them
Parameters:
NameTypeDescription
textstring

the file contents

Returns:

{ entries: [{ start, end, text }], skippedCues: integer, firstSkipped: string }

Type: 
object

(static) vttTimestampPattern() → {object}

The timestamp grammar vttTimestampToMs() accepts: [H:]MM:SS.mmm. The hour field is optional in WebVTT. A comma before the milliseconds is not valid WebVTT, but it has reached the app in production (#584), and rejecting it would drop every cue in such a file rather than show them.

Returns:
Type: 
object

(static) vttTimestampToMs(text, patternopt) → {integer}

Milliseconds for one WebVTT timestamp, or -1 when it is not one.

converting many stamps, so the regex is compiled once per file rather than per stamp

Parameters:
NameTypeAttributesDefaultDescription
textstring

the bare stamp: no -->, no cue settings, no padding

patterndynamic<optional>
invalid

a vttTimestampPattern(); pass one in when

Returns:

milliseconds, or -1

Type: 
integer

(static) vttTimingField(text) → {string}

The timestamp at the start of one side of a timing line: the end side can carry cue settings after it (00:28.876 align:start position:10%), and a CRLF file leaves a trailing carriage return.

Parameters:
NameTypeDescription
textstring
Returns:
Type: 
string