perf(sample): download only the subtitle segments around the window

A segmented subtitle track took every segment in a --sample run. It now takes the window plus one segment on each side.
This commit is contained in:
imSp4rky
2026-10-11 19:14:14 -06:00
parent 4c2e74de80
commit b4136e1cf5
3 changed files with 34 additions and 11 deletions
+14 -2
View File
@@ -11,6 +11,7 @@ from io import BytesIO
from pathlib import Path
from typing import Any, Callable, Iterable, Optional, Sequence, Union
import click
import pycaption
import requests
from construct import Container
@@ -1004,8 +1005,19 @@ class Subtitle(Track):
def sample_slice(
self, starts: Sequence[float], timescale: float = 1, window: Optional[tuple[int, int]] = None
) -> slice:
"""Keep every segment: ``trim`` cuts a subtitle by its cue times after the download."""
return slice(None)
"""Return the segments of ``window`` plus one segment on each side.
Cue times in a segment are absolute, so ``sample_start`` stays zero and ``trim`` makes the cut.
A track that ends before the window keeps only its last segment.
"""
try:
keep = super().sample_slice(starts, timescale, window)
except click.ClickException:
keep = slice(len(starts) - 1, None)
self.sample_start = 0.0
if keep.start is None:
return keep
return slice(max(keep.start - 1, 0), None if keep.stop is None else keep.stop + 1)
def trim(self, start: float, end: float) -> bool:
"""Keep the cues between ``start`` and ``end`` seconds and move them to start at zero.