subaligner.utils module

class subaligner.utils.Utils[source]

Bases: object

Utility functions

FFMPEG_BIN = 'ffmpeg'
static ass2srt(ass_file_path: str, srt_file_path: Optional[str, None] = None) → None[source]

Convert Advanced SubStation Alpha v4.0+ subtitles to SubRip subtitles.

Parameters
  • {string} -- The path to the ASS file. (ass_file_path) –

  • {string} -- The path to the SubRip file. (srt_file_path) –

static contains_embedded_subtitles(video_file_path: str, timeout_secs: int = 30) → bool[source]

Detect if the input video contains embedded subtitles.

Parameters
  • {string} -- The path to the video file. (video_file_path) –

  • {int} -- The timeout in seconds on extraction {default (timeout_secs) – 30}.

Returns

bool – True if the video contains embedded subtitles or False otherwise.

static detect_encoding(subtitle_file_path: str) → str[source]

Detect the encoding of the subtitle file.

Parameters

{string} -- The path to the subtitle file. (subtitle_file_path) –

Returns

string – The string represent the encoding

static double_quoted(s: str) → str[source]
static download_file(remote_file_url: str, local_file_path: str) → None[source]
static extract_matroska_subtitle(mkv_file_path: str, stream_index: int, output_file_path: str, timeout_secs: int = 30) → None[source]

Extract subtitles from Matroska files and convert them into the output format.

Parameters
  • {string} -- The path to the Matroska file. (mkv_file_path) –

  • {int} -- The index of the subtitle stream (stream_index) –

  • {string} -- The path to the output file. (output_file_path) –

  • {int} -- The timeout in seconds on extraction {default (timeout_secs) – 30}.

static extract_teletext_as_subtitle(ts_file_path: str, page_num: int, output_file_path: str, timeout_secs: int = 30) → None[source]

Extract DVB Teletext from MPEG transport stream files and convert them into the output format.

Parameters
  • {string} -- The path to the Transport Stream file. (ts_file_path) –

  • {int} -- The page number for the Teletext (page_num) –

  • {string} -- The path to the output file. (output_file_path) –

  • {int} -- The timeout in seconds on extraction {default (timeout_secs) – 30}.

static format_timestamp(seconds: float) → str[source]
static get_file_root_and_extension(file_path: str) → Tuple[str, str][source]

Get the root path and the extension of the input file path.

Returns

tuple – the root path and the extension of the input file path.

static get_iso_639_alpha_2(language_code: str) → str[source]

Find the alpha 2 language code based on an alpha 3 one.

Parameters

{string} -- An alpha 3 language code derived from ISO 639-3. (language_code) –

Returns

string – The alpha 2 language code if exists otherwise the alpha 3 one.

Raises

ValueError -- Thrown when the input language code cannot be recognised.

static get_language_table() → List[str][source]

Get all known language codes and their human-readable versions.

Returns

list – A list of all known language codes and their human-readable versions.

static get_misc_language_codes() → List[str][source]

Get all known language codes.

Returns

list – A list of all known language codes.

static get_stretch_language_codes() → List[str][source]

Get language codes used by stretch.

Returns

list – A list of language codes derived from ISO 639-3.

static microdvd2srt(microdvd_file_path: str, srt_file_path: Optional[str, None] = None) → None[source]

Convert MicroDVD subtitles to SubRip subtitles.

Parameters
  • {string} -- The path to the MPL2 file. (microdvd_file_path) –

  • {string} -- The path to the SubRip file. (srt_file_path) –

static mpl22srt(mpl2_file_path: str, srt_file_path: Optional[str, None] = None) → None[source]

Convert MPL2 subtitles to SubRip subtitles.

Parameters
  • {string} -- The path to the MPL2 file. (mpl2_file_path) –

  • {string} -- The path to the SubRip file. (srt_file_path) –

static remove_trailing_newlines(source_file_path: str, encoding: Optional[str, None], target_file_path: Optional[str, None] = None) → None[source]
static sami2srt(sami_file_path: str, srt_file_path: Optional[str, None] = None) → None[source]

Convert SAMI subtitles to SubRip subtitles.

Parameters
  • {string} -- The path to the SAMI file. (sami_file_path) –

  • {string} -- The path to the SubRip file. (srt_file_path) –

static sbv2srt(sbv_file_path: str, srt_file_path: Optional[str, None] = None) → None[source]

Convert SubViewer subtitles to SubRip subtitles.

Parameters
  • {string} -- The path to the SubViewer file. (sbv_file_path) –

  • {string} -- The path to the SubRip file. (srt_file_path) –

static scc2srt(scc_file_path: str, srt_file_path: Optional[str, None] = None) → None[source]

Convert SCC subtitles to SubRip subtitles.

Parameters
  • {string} -- The path to the Scenarist Closed Captions file. (scc_file_path) –

  • {string} -- The path to the SubRip file. (srt_file_path) –

static srt2ass(srt_file_path: str, ass_file_path: Optional[str, None] = None) → None[source]

Convert SubRip subtitles to Advanced SubStation Alpha v4.0+ subtitles.

Parameters
  • {string} -- The path to the SubRip file. (srt_file_path) –

  • {string} -- The path to the ASS file. (ass_file_path) –

static srt2microdvd(srt_file_path: str, microdvd_file_path: Optional[str, None] = None, frame_rate: Optional[float, None] = 25.0)[source]

Convert SubRip subtitles to MicroDVD subtitles.

Parameters
  • {string} -- The path to the SubRip file. (srt_file_path) –

  • {string} -- The path to the MicroDVD file. (microdvd_file_path) –

  • {float} -- The frame rate for frame-based MicroDVD. (frame_rate) –

static srt2mpl2(srt_file_path: str, mpl2_file_path: Optional[str, None] = None) → None[source]

Convert SubRip subtitles to MPL2 subtitles.

Parameters
  • {string} -- The path to the SubRip file. (srt_file_path) –

  • {string} -- The path to the MPL2 file. (mpl2_file_path) –

static srt2sami(srt_file_path: str, sami_file_path: Optional[str, None] = None) → None[source]

Convert SubRip subtitles to SAMI subtitles.

Parameters
  • {string} -- The path to the SubRip file. (srt_file_path) –

  • {string} -- The path to the SAMI file. (sami_file_path) –

static srt2sbv(srt_file_path: str, sbv_file_path: Optional[str, None] = None) → None[source]

Convert SubRip subtitles to SubViewer subtitles.

Parameters
  • {string} -- The path to the SubRip file. (srt_file_path) –

  • {string} -- The path to the SubViewer file. (sbv_file_path) –

static srt2scc(srt_file_path: str, scc_file_path: Optional[str, None] = None) → None[source]

Convert SubRip subtitles to SCC subtitles.

Parameters
  • {string} -- The path to the SubRip file. (srt_file_path) –

  • {string} -- The path to the Scenarist Closed Captions file. (scc_file_path) –

static srt2ssa(srt_file_path: str, ssa_file_path: Optional[str, None] = None) → None[source]

Convert SubRip subtitles to SubStation Alpha v4.0 subtitles.

Parameters
  • {string} -- The path to the SubRip file. (srt_file_path) –

  • {string} -- The path to the SSA file. (ssa_file_path) –

static srt2tmp(srt_file_path: str, tmp_file_path: Optional[str, None] = None) → None[source]

Convert SubRip subtitles to TMP subtitles.

Parameters
  • {string} -- The path to the SubRip file. (srt_file_path) –

  • {string} -- The path to the TMP file. (tmp_file_path) –

static srt2ttml(srt_file_path: str, ttml_file_path: Optional[str, None] = None) → None[source]

Convert SubRip subtitles to TTML subtitles.

Parameters
  • {string} -- The path to the SubRip file. (srt_file_path) –

  • {string} -- The path to the TTML file. (ttml_file_path) –

static srt2vtt(srt_file_path: str, vtt_file_path: Optional[str, None] = None, timeout_secs: int = 30) → None[source]

Convert SubRip subtitles to WebVTT subtitles.

Parameters
  • {string} -- The path to the SubRip file. (srt_file_path) –

  • {string} -- The path to the WebVTT file. (vtt_file_path) –

  • {int} -- The timeout in seconds on conversion {default (timeout_secs) – 30}.

static srt2ytt(srt_file_path: str, transcript_file_path: Optional[str, None] = None) → None[source]

Convert SubRip subtitles to YouTube transcript subtitles.

Parameters
  • {string} -- The path to the SubRip file. (srt_file_path) –

  • {string} -- The path to the YouTube transcript file. (transcript_file_path) –

static ssa2srt(ssa_file_path: str, srt_file_path: Optional[str, None] = None) → None[source]

Convert SubStation Alpha v4.0 subtitles to SubRip subtitles.

Parameters
  • {string} -- The path to the SSA file. (ssa_file_path) –

  • {string} -- The path to the SubRip file. (srt_file_path) –

static stl2srt(stl_file_path: str, srt_file_path: Optional[str, None] = None) → None[source]

Convert EBU-STL subtitles to SubRip subtitles.

Parameters
  • {string} -- The path to the EBU-STL file. (stl_file_path) –

  • {string} -- The path to the SubRip file. (srt_file_path) –

static suppress_lib_logs() → None[source]
static tmp2srt(tmp_file_path: str, srt_file_path: Optional[str, None] = None) → None[source]

Convert TMP subtitles to SubRip subtitles.

Parameters
  • {string} -- The path to the TMP file. (mpl2_file_path) –

  • {string} -- The path to the SubRip file. (tmp_file_path) –

static ttml2srt(ttml_file_path: str, srt_file_path: Optional[str, None] = None) → None[source]

Convert TTML subtitles to SubRip subtitles.

Parameters
  • {string} -- The path to the TTML file. (ttml_file_path) –

  • {string} -- The path to the SubRip file. (srt_file_path) –

static vtt2srt(vtt_file_path: str, srt_file_path: Optional[str, None] = None, timeout_secs: int = 30) → None[source]

Convert WebVTT subtitles to SubRip subtitles.

Parameters
  • {string} -- The path to the WebVTT file. (vtt_file_path) –

  • {string} -- The path to the SubRip file. (srt_file_path) –

  • {int} -- The timeout in seconds on conversion {default (timeout_secs) – 30}.

static ytt2srt(transcript_file_path: str, srt_file_path: Optional[str, None] = None) → None[source]

Convert YouTube transcript subtitles to SubRip subtitles.

Parameters
  • {string} -- The path to the YouTube transcript file. (transcript_file_path) –

  • {string} -- The path to the SubRip file. (srt_file_path) –