GoSaveTool.Subtitles (GoSaveTool v1.0.0)

Copy Markdown

SRT and WebVTT: read cues, write them in either format, shift and repair timings. The Elixir version of @gosavetool/subtitles.

Online editor: https://www.gosavetool.com/vtt-subtitle-generator

iex> GoSaveTool.Subtitles.convert("1\n00:00:01,000 --> 00:00:02,500\nHello\n", :vtt)
"WEBVTT\n\n00:00:01.000 --> 00:00:02.500\nHello\n"

Summary

Functions

Converts a subtitle text to :srt or :vtt.

Milliseconds to "00:01:02,500" (separator ",", SRT) or "00:01:02.500" (separator ".", WebVTT).

Cues of an SRT or WebVTT text. Headers, NOTE and STYLE blocks are skipped.

Repairs common faults: cues out of order, an end before its start, overlapping cues and empty cues. Returns {cues, number_of_changes}. Cues that end before they start get min_duration milliseconds.

Moves every cue by ms (negative = earlier); cues pushed before zero are clipped or dropped.

"00:01:02,500" or "01:02.5" to milliseconds; nil when it is not a timestamp.

SRT text, numbered from 1.

WebVTT text; cue settings are kept.

True when the text starts with the WEBVTT signature.

Functions

convert(src, other)

@spec convert(String.t(), :srt | :vtt) :: String.t()

Converts a subtitle text to :srt or :vtt.

from_ms(ms, sep \\ ",")

@spec from_ms(number(), String.t()) :: String.t()

Milliseconds to "00:01:02,500" (separator ",", SRT) or "00:01:02.500" (separator ".", WebVTT).

iex> GoSaveTool.Subtitles.from_ms(3723004)
"01:02:03,004"

parse(src)

@spec parse(String.t()) :: [GoSaveTool.Subtitles.Cue.t()]

Cues of an SRT or WebVTT text. Headers, NOTE and STYLE blocks are skipped.

repair(cues, min_duration \\ 2000)

Repairs common faults: cues out of order, an end before its start, overlapping cues and empty cues. Returns {cues, number_of_changes}. Cues that end before they start get min_duration milliseconds.

shift(cues, ms)

Moves every cue by ms (negative = earlier); cues pushed before zero are clipped or dropped.

to_ms(stamp)

@spec to_ms(String.t()) :: non_neg_integer() | nil

"00:01:02,500" or "01:02.5" to milliseconds; nil when it is not a timestamp.

iex> GoSaveTool.Subtitles.to_ms("1:02:03.4")
3723400

to_srt(cues)

@spec to_srt([GoSaveTool.Subtitles.Cue.t()]) :: String.t()

SRT text, numbered from 1.

to_vtt(cues)

@spec to_vtt([GoSaveTool.Subtitles.Cue.t()]) :: String.t()

WebVTT text; cue settings are kept.

vtt?(src)

@spec vtt?(String.t()) :: boolean()

True when the text starts with the WEBVTT signature.