srt (SubRip) Format
pytitle.srt.subtitle
- class pytitle.srt.subtitle.SrtSubtitle(path: Optional[Union[str, bytes, PathLike]] = None, lines: Optional[List[Line]] = None, encoding: Optional[str] = 'utf-8')[source]
Bases:
objectSubtitle object for .srt formatted subtitles
- classmethod open(path: ~typing.Union[str, bytes, ~os.PathLike], encoding: ~typing.Optional[str] = 'utf-8', use_chardet: bool = False, fallback_encodings: ~pytitle.srt.types.Encodings = <pytitle.srt.types.Encodings object>, **kwargs) SrtSubtitle[source]
Open subtitle file from a path
- Parameters
path (str) – the path to the subtitle file
encoding (Optional[str]) – the encoding of the subtitle file
use_chardet (bool) – if True, use chardet to detect the encoding if ‘utf-8’ failed
- Returns
the subtitle object
- Return type
- classmethod parse(filestring: str) List[Line][source]
Parse the string formatted as a .srt file
- Parameters
filestring (str) – the string of the subtitle file
- Returns
a list of Line objects
- Return type
List[Line]
- save(path: Optional[Union[str, bytes, PathLike]] = None, encoding: Optional[str] = None) None[source]
Save subtitle to a path
- Parameters
path (str) – the path to save the subtitle to
encoding (str) – the encoding of the subtitle file
- Returns
None
- Return type
None
- shift(shift_by: Timestamp, indexes: Optional[List[int]] = None, lines: Optional[List[Line]] = None, backward: bool = False, start: bool = True, end: bool = True) None[source]
Shift the timing of one or multiple lines of subtitle, backward or forward
- shift_forward(hours: int = 0, minutes: int = 0, seconds: int = 0, milliseconds: int = 0, index: Optional[Union[int, List[int]]] = None, lines: Optional[List[Line]] = None) None[source]
- Shift the timing of a line by index or all
lines of subtitle forward by hour, minutes, seconds, milliseconds
shortcut for SrtSubtitle.shift(…)
- Parameters
hours (int) – the number of hours to shift
minutes (int) – the number of minutes to shift
seconds (int) – the number of seconds to shift
milliseconds (int) – the number of milliseconds to shift
index (Union[int, List[int]]) – the index of the line to shift, all lines if None
- Lines
the lines to shift, all lines if None
- Returns
None
- Return type
None
- shift_backward(hours: int = 0, minutes: int = 0, seconds: int = 0, milliseconds: int = 0, index: Optional[Union[int, List[int]]] = None, lines: Optional[List[Line]] = None) None[source]
- Shift the timing of a line by index or all
lines of subtitle backward by hour, minutes, seconds, milliseconds
shortcut for SrtSubtitle.shift(…)
- Parameters
hours (int) – the number of hours to shift
minutes (int) – the number of minutes to shift
seconds (int) – the number of seconds to shift
milliseconds (int) – the number of milliseconds to shift
index (Union[int, List[int]]) – the index of the line to shift, all lines if None
- Lines
the lines to shift, all lines if None
- Returns
None
- Return type
None
- search(keyword: str, filters: Optional[str] = None) Line[source]
Serach a keyword in text lines, duration in timings and line index
- property output: str
pytitle.srt.types
- class pytitle.srt.types.Timestamp(*args: Any, **kwargs: Any)[source]
Bases:
BaseModelThe Timestamp object is used to store the start or end of a subtitle the Timestamp is rendered as 00:00:00,000 from the
outputproperty- hours: int = 0
- minutes: int = 0
- seconds: int = 0
- milliseconds: int = 0
- property output: str
output the timestamp in the format 00:00:00,000
- classmethod from_string(timestr: str) Timestamp[source]
Create a Timestamp object from a string
- Parameters
timestr (srt) – the string to create the Timestamp from in the format of 00:00:00,000
- Returns
the Timestamp object
- Return type
- get_value(property: Literal['hours', 'minutes', 'seconds', 'milliseconds']) str[source]
get the hour, minute, second or millisecond values properly formatted
- Parameters
property (str) – the property to get the value of
- Returns
the value of the property
- Return type
str
- beautify(property: str, optimal_length: int = 2, right_append: bool = False)[source]
Beautify the property value
adds 0s to the left or right side of the each property to change them into the right format that is: 00:00:00,000
- Parameters
optimal_length (int) – the length that the property should be, 2 or 3 based on the type
right_append (bool) – add the 0s to the right side of the property instead of the left
- Returns
the property value with the right format
- Return type
str
- class pytitle.srt.types.Timing(*args: Any, **kwargs: Any)[source]
Bases:
BaseModelTiming object for a subtitle Timing consists of a
startandendTimestamp and is rendered as 00:00:00,000 –> 00:00:00,000 from theoutputmethod- property output: str
output the timing in the format 00:00:00,000 –> 00:00:00,000
- classmethod from_string(start: str, end: str) Timing[source]
Create a Timing object from a string the
startandendshould be in the format of 00:00:00,000- Parameters
start (str) – the start Timestamp of the subtitle
end (str) – the end Timestamp of the subtitle
- Returns
the Timing object
- Return type
- shift(shift_by: Timestamp, backward: bool = False, start: bool = True, end: bool = True) None[source]
Shift the timing by a Timestamp
- Parameters
shift_by (Timestamp) – the Timestamp to shift the timing by
backward (bool) – shift the timing backward instead of forward
start (bool) – shift the start Timestamp
end (bool) – shift the end Timestamp
- Returns
None
- Return type
None
- class pytitle.srt.types.Line(*args: Any, **kwargs: Any)[source]
Bases:
BaseModelLine object for a subtitle
Line consists of
index,timingandtextan example formatted line would be:1 00:00:00,000 --> 00:00:00,000 This is a subtitle
- index: int
- text: Optional[str] = ''
- property output: str
output the line in the format of:
1 00:00:00,000 --> 00:00:00,000 This is a subtitle
- classmethod from_string(linestring: str) Line[source]
Create a Line object from a string
- Parameters
linestring (str) – the string to create the Line from
- Returns
the Line object
- Return type
- classmethod get_lines(lines: List[Line], index: List[int], check_contains: bool = False) List[Line][source]
get line objects from index of them
- Parameters
lines (List[int]) – the index of the lines to get
index (List[int]) – the index of the lines to get
check_contains (bool) – check if all the indexes are in the lines
- Returns
the lines
- Return type
List[Line]