If you need to manipulate the subtitle list (e.g., after changing timestamps) without immediately generating a string, use sort_and_reindex(). This function sorts subtitles by start time and updates their index attribute.
Parameters:
subtitles: An iterable of Subtitle objects.start_index (int): The index to start from (default 1).in_place (bool): Whether to modify the objects in the original list.skip (bool): If True (default), it filters out "useless" subtitles. A subtitle is skipped if:- Content is empty or only whitespace.
- Start time is negative.
- Start time is greater than or equal to the end time.
from datetime import timedelta
from edge_tts.srt_composer import Subtitle, sort_and_reindex
subs = [
Subtitle(index=1, start=timedelta(seconds=10), end=timedelta(seconds=12), content="Late"),
Subtitle(index=2, start=timedelta(seconds=1), end=timedelta(seconds=2), content="Early"),
]
# Returns a generator of sorted, reindexed subtitles
sorted_subs = list(sort_and_reindex(subs))
for s in sorted_subs:
print(f"Index: {s.index}, Start: {s.start}")