SRT is a tiny, yet featureful Python library designed for robust parsing, modification, and composition of SRT subtitle files. It can handle many broken SRT files and has no dependencies beyond the Python Standard Library. The current version is 3.5.3, with an active, though not rapid, release cadence.
pip install srtVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to parse an SRT string into a list of `Subtitle` objects, access and modify their properties, and then compose them back into an SRT formatted string. It also shows how to create a new `Subtitle` object and use `srt.sort_and_reindex`.
Specify the `encoding` when opening the file, e.g., `with open('file.srt', 'r', encoding='utf-8') as f: srt_data = f.read()`.Handle `SRTParseError` with a `try...except` block, or set `ignore_errors=True` in `srt.parse()` to skip invalid blocks and continue parsing, though this might result in incomplete subtitle data. For example: `subs = list(srt.parse(srt_content, ignore_errors=True))`.
Avoid blank lines in subtitle content when composing SRTs. If you absolutely need to compose an SRT with blank lines (not recommended), you can set `strict=False`: `srt.compose(subtitles, strict=False)`.
If you want a new, sorted, and reindexed list without modifying the original, ensure you pass a copy or explicitly handle the return value. For example, `new_sorted_subs = srt.sort_and_reindex(list(original_subs))` if `in_place` is True, or simply `new_sorted_subs = srt.sort_and_reindex(original_subs, in_place=False)` if the function supports `in_place=False` (which it does not directly, but the default behavior is to create a new list if `in_place` is not specified as True. Always check the function signature). Note: for `srt.sort_and_reindex`, the default behavior implicitly returns a new list if `in_place` is not explicitly set to `True` for older versions, for newer versions the parameter is `skip` and `in_place` is not present, so clarification on its usage is key in current documentation. Current `srt.sort_and_reindex` function takes `subtitles`, `start_index`, and `skip`. The function returns a new list without modifying the original, so `in_place` is not a concern for the current API. Rephrasing this warning for current version: The *returned* list from `srt.sort_and_reindex` should be used as it will contain the sorted and re-indexed subtitles. If you're appending this back to an existing list, be careful to replace or extend, not just append.
pip install srt
import srt
# To parse from a file
with open('subtitles.srt', 'r', encoding='utf-8') as f:
srt_string = f.read()
subtitles = srt.parse(srt_string)
# To compose to a file
composed_srt = srt.compose(subtitles)
with open('output.srt', 'w', encoding='utf-8') as f:
f.write(composed_srt)import srt
# If you have lines from a file readlines()
lines = [
"1\n",
"00:00:01,000 --> 00:00:02,000\n",
"Hello\n",
"\n",
"2\n",
"00:00:02,500 --> 00:00:03,500\n",
"World\n",
"\n"
]
srt_string = "".join(lines) # Assumes lines already contain newlines
subtitles = srt.parse(srt_string)import srt
srt_data = """1
00:00:01,000 --> 00:00:02,000
Hello
"""
subtitles = srt.parse(srt_data)
for sub in subtitles:
print(f"Index: {sub.index}")
print(f"Content: {sub.content}")
print(f"Start: {sub.start}")import srt
srt_data = """1
00:00:01,000 --> 00:00:02,000
Hello
"""
subtitles = list(srt.parse(srt_data))
if subtitles:
print(subtitles[0].content) # Correct way to accessNo dependency data recorded yet.