webvtt-py package reference

webvtt.webvtt

WebVTT module.

class webvtt.webvtt.WebVTT(file: str | None = None, captions: List[Caption] | None = None, styles: List[Style] | None = None, header_comments: List[str] | None = None, footer_comments: List[str] | None = None)

Parse captions in WebVTT format and also from other formats like SRT.

To read WebVTT:

WebVTT.read(‘captions.vtt’)

For other formats:

WebVTT.from_srt(‘captions.srt’) WebVTT.from_sbv(‘captions.sbv’)

property content: str

Return the webvtt capions as string.

This property is useful in cases where the webvtt content is needed but no file-like destination is required. Storage in DB for instance.

classmethod from_buffer(buffer: Iterator[str]) WebVTT

Read WebVTT captions from a file-like object.

Such file-like object may be the return of an io.open call, io.StringIO object, tempfile.TemporaryFile object, etc.

Parameters:

buffer – the file-like object to read captions from

Returns:

a WebVTT instance

classmethod from_sbv(file: str, encoding: str | None = None) WebVTT

Read captions from a file in YouTube SBV format.

Parameters:
  • file – the file path

  • encoding – encoding of the file

Returns:

a WebVTT instance

classmethod from_srt(file: str, encoding: str | None = None) WebVTT

Read captions from a file in SubRip format.

Parameters:
  • file – the file path

  • encoding – encoding of the file

Returns:

a WebVTT instance

classmethod from_string(string: str) WebVTT

Read captions from a string.

Parameters:

string – the captions in a string

Returns:

a WebVTT instance

iter_slice(start: str | None = None, end: str | None = None) Generator[Caption, None, None]

Iterate a slice of the captions based on a time range.

Parameters:
  • start – start timestamp of the range

  • end – end timestamp of the range

Returns:

generator of Captions

classmethod read(file: str, encoding: str | None = None) WebVTT

Read a WebVTT captions file.

Parameters:
  • file – the file path

  • encoding – encoding of the file

Returns:

a WebVTT instance

classmethod read_buffer(buffer: Iterator[str]) WebVTT

Read WebVTT captions from a file-like object.

This method is DEPRECATED. Use from_buffer instead.

Such file-like object may be the return of an io.open call, io.StringIO object, tempfile.TemporaryFile object, etc.

Parameters:

buffer – the file-like object to read captions from

Returns:

a WebVTT instance

save(output: str | None = None, encoding: str | None = None, add_bom: bool | None = None)

Save the WebVTT captions to a file.

Parameters:
  • output – destination path of the file

  • encoding – encoding of the file

  • add_bom – save the file with Byte Order Mark

Raises:

MissingFilenameError – if output cannot be determined

save_as_srt(output: str | None = None, encoding: str | None = 'utf-8')

Save the WebVTT captions to a file in SubRip format.

Parameters:
  • output – destination path of the file

  • encoding – encoding of the file

Raises:

MissingFilenameError – if output cannot be determined

property total_length

Returns the total length of the captions.

write(f: IO[str], format: str = 'vtt')

Save the WebVTT captions to a file-like object.

Parameters:
  • f – destination file-like object

  • format – the format to use (vtt or srt)

Raises:

MissingFilenameError – if output cannot be determined

webvtt.segmenter

Segmenter module.

webvtt.segmenter.segment(webvtt_path: str, output: str, seconds: int = 10, mpegts: int = 900000)

Segment a WebVTT captions file.

Parameters:
  • webvtt_path – the path to the file

  • output – the path to the destination folder

  • seconds – the number of seconds for each segment

  • mpegts – value for the MPEG-TS

webvtt.segmenter.slice_segments(captions: Sequence[Caption], seconds: int) List[List[Caption]]

Slice segments of captions based on seconds per segment.

Parameters:
  • captions – the captions

  • seconds – seconds per segment

Returns:

list of lists of Caption objects

webvtt.segmenter.write_manifest(output_folder: Path, segments: Iterable[Iterable[Caption]], seconds: int)

Write the manifest in the output folder.

Parameters:
  • output_folder – folder where the manifest will be stored

  • segments – the segments of Caption objects

  • seconds – the seconds per segment

webvtt.segmenter.write_segments(output_folder: Path, segments: Iterable[Iterable[Caption]], mpegts: int)

Write the segments to the output folder.

Parameters:
  • output_folder – folder where the segment files will be stored

  • segments – the segments of Caption objects

  • mpegts – value for the MPEG-TS

webvtt.cli

CLI module.

webvtt.cli.main(argv: Sequence | None = None)

Segment WebVTT file from command line.

Parameters:

argv – command line arguments

webvtt.generic

webvtt.parsers

webvtt.writers

webvtt.exceptions