three_play.utils.parse package

Submodules

three_play.utils.parse.models module

class three_play.utils.parse.models.ListOfSRTLine(srt_contents: str)[source]

Bases: List[SRTLine]

property contents: str

Return the SRT file contents, as a string

insert(_ListOfSRTLine__index: int, _ListOfSRTLine__object: three_play.utils.parse.models.SRTLine)None[source]

Insert a SRTLine into the list at a specified index, and increment the line numbers for all subsequent lines as needed.

class three_play.utils.parse.models.SRTLine(num: Union[int, str] = 0, time_range: str = '', dialogue: Optional[Union[List[str], str]] = None, width: int = 35)[source]

Bases: object

Represents a sequence of dialogue lines in an SRT file.

For each element in the sequence:
  • the first line will be the line number (ex. 12)

  • the second line will be the timeframe, which is formatted like so with an arrow denoting range:

    [First Timestamp] –> [Second Timestamp]

  • the remaining lines (can be empty) will be the dialogue

property dialogue: List[str]
property end_ts: str

Return the end timestamp associated with the line.

property num: int
classmethod parse(lines: Union[str, List[str]])three_play.utils.types.R[source]

Parse and return a new SRTLine object.

Parameters

lines – The lines from an SRT file, which should adhere to the sequence as specified in the class docs.

set_time_range_if_empty(time_range: str)[source]

Set the time_range attribute if it’s empty or null.

property start_ts: str

Return the start timestamp associated with the line.

property text: str

Returns the dialogue as a string.

time_range: str
width: int

three_play.utils.parse.srt module

three_play.utils.parse.srt.get_srt_duration(srt_contents: str, default_end_seconds=0.0)float[source]

Gets the total duration (based on end timestamp) of an SRT file

three_play.utils.parse.srt.remove_dialogue_between(srt_contents: str, start_ms: int, end_ms: int)[source]

Remove all dialogue between start_ms and end_ms, non-inclusive of any dialogue for end_ms - note that values are in milliseconds.

three_play.utils.parse.srt.remove_dialogue_for_first_ts(srt_contents: str, ts: str)str[source]

Removes dialogue under the first occurrence of a start timestamp in an SRT file. If the start timestamp is not found, return the srt_contents instead.

three_play.utils.parse.srt.timestamp(seconds: float = 0, *, hours: float = 0, minutes: float = 0, milliseconds: float = 0)[source]

Convert a duration (generally specified in seconds) to a formatted string in the ‘HH:mm:ss.SSS’ format, for example ‘2:01:03.150’.

three_play.utils.parse.srt.total_ms(ts: str)int[source]

Converts a timestamp containing hours, minutes, seconds, and milliseconds (for example, in the “HH:mm:ss,SSS” format) to an integer value representing the total milliseconds.

For example, a string like “1:20:32,5” will be returned as 4832005

Supports parsing the following input formats:
  • (H)H:mm:ss,SSS

  • (H)H:mm:ss.SSS

  • (H)H:mm:ss:SSS

A modified version of the following (great) solution: https://stackoverflow.com/a/57610198

three_play.utils.parse.srt.total_seconds(ts: str)str[source]

Converts a timestamp containing hours, minutes, seconds, and milliseconds (for example, in the “HH:mm:ss,SSS” format) to a string representing the total seconds, along with the millisecond part.

For example, a string like “1:20:32,5” will be returned as “4832.005”

Supports parsing the following input formats:
  • (H)H:mm:ss,SSS

  • (H)H:mm:ss.SSS

  • (H)H:mm:ss:SSS

A modified version of the following (great) solution: https://stackoverflow.com/a/57610198

three_play.utils.parse.types module

three_play.utils.parse.types.as_bool(o: Union[str, bool], default=False)[source]

Return o if already a boolean, otherwise return the boolean value for a string. If o is None or an empty string, return default instead.

three_play.utils.parse.types.as_int(o: Union[str, int], default=0, raise_=True)[source]

Return o if already a int, otherwise return the int value for a string. If o is None or an empty string, return default instead.

If o cannot be converted to an int, raise an error if raise_ is true, other return default instead.

Module contents