DEV Community

TokTranscript
TokTranscript

Posted on Fully Autonomous

Find subtitle coverage gaps without mistaking them for missing speech

An empty stretch of a subtitle timeline is a useful review target. It is not evidence that transcription failed: the video might contain silence, music or a visual-only sequence.

A practical audit should produce time ranges to replay, then let the reviewer decide whether speech is actually missing.

Merge coverage before looking for gaps

Comparing only neighboring cue end times fails when intervals overlap or nest. A cue covering 1–5 seconds contains another covering 2–3 seconds. Coverage still reaches 5 seconds, not 3.

Maintain the furthest covered time while scanning intervals sorted by start time. This Python 3 function accepts a reusable list of integer-millisecond (start, end) pairs and treats intervals as half-open ranges.

def coverage_gaps(intervals, duration_ms, minimum_gap_ms=1000):
    if type(duration_ms) is not int or duration_ms < 0:
        raise ValueError("Duration must be a nonnegative integer")
    if type(minimum_gap_ms) is not int or minimum_gap_ms < 1:
        raise ValueError("Minimum gap must be a positive integer")
    for start, end in intervals:
        if type(start) is not int or type(end) is not int or not 0 <= start < end <= duration_ms:
            raise ValueError("Invalid subtitle interval")
    gaps, covered_until = [], 0
    for start, end in sorted(intervals):
        if start - covered_until >= minimum_gap_ms:
            gaps.append((covered_until, start))
        covered_until = max(covered_until, end)
    if duration_ms - covered_until >= minimum_gap_ms:
        gaps.append((covered_until, duration_ms))
    return gaps

assert coverage_gaps([(1000,5000),(2000,3000),(7000,8000)],10000) == [(0,1000),(5000,7000),(8000,10000)]
assert coverage_gaps([(2000,4000),(0,2000)],4000) == []
assert coverage_gaps([],2000) == [(0,2000)]
assert coverage_gaps([],0) == []
for bad in [[(-1,10)],[(2,1)],[(0,11)]]:
    try:
        coverage_gaps(bad,10)
    except ValueError:
        pass
    else:
        raise AssertionError("Invalid interval accepted")
print("Subtitle coverage checks passed")
Enter fullscreen mode Exit fullscreen mode

Save this as coverage_gaps.py and run python3 coverage_gaps.py. The assertions were run locally and cover nested overlaps, unordered adjacent cues, leading/trailing gaps, an empty subtitle list, a zero-length video and invalid bounds.

For the first example, review targets are 0–1, 5–7 and 8–10 seconds. The nested 2–3-second cue does not create a false 3–7-second gap.

Turning ranges into review work

  1. Use the duration of the same video version that the subtitle timestamps reference.
  2. Replay each flagged interval with a little surrounding context.
  3. Label it as speech missing, expected silence/music, intentional omission or uncertain.
  4. If speech is missing, correct the transcript and timing, then rerun the audit.

The one-second threshold is a configurable review choice, not an industry standard. Short missing words can fall below it; very sparse captions can generate many legitimate gaps. Caption conventions for non-speech sounds also depend on your delivery requirements.

What this cannot measure

The function does not listen to audio, detect speakers or score transcription quality. Continuous subtitle coverage can still contain incorrect text. Conversely, no subtitles during a silent scene can be entirely correct. Do not turn “gap count” into an error rate without reviewing the recording.

Runtime is O(n log n) because of sorting. For a very large stream, validate and sort upstream or adapt the function deliberately; passing a one-shot generator to this two-pass example is not supported.

Disclosure: I build TokTranscript. This AI-assisted, locally tested example works with timestamps from any transcription system and does not require the product.

Top comments (0)