DEV Community

Cover image for Check a caption cue after a timeline edit with Python
Anlin
Anlin

Posted on

Check a caption cue after a timeline edit with Python

A reviewer asks you to hold a tutorial shot long enough for the result to appear. You move the next narration line later, and its caption remains where it was. This small script flags caption starts that have drifted from their corresponding spoken line. Use times from the latest sequence or export.

from dataclasses import dataclass
from decimal import Decimal


@dataclass
class Cue:
    label: str
    voice_start: Decimal
    caption_start: Decimal


cues = [
    Cue("setup", Decimal("8.45"), Decimal("8.45")),
    Cue("result", Decimal("10.60"), Decimal("10.15")),
    Cue("next", Decimal("12.20"), Decimal("12.20")),
]
tolerance = Decimal("0.05")

for cue in cues:
    offset = cue.caption_start - cue.voice_start
    if abs(offset) <= tolerance:
        continue

    direction = "late" if offset > 0 else "early"
    print(f"{cue.label}: caption starts {abs(offset):.2f}s {direction}")
Enter fullscreen mode Exit fullscreen mode

Run it with Python 3.10 or later. It prints:

result: caption starts 0.45s early
Enter fullscreen mode Exit fullscreen mode

To adapt it to a new revision, enter the times for the changed line and one line on either side. Check the affected cue in your editor and run it again with the new time. Finally, play the exported video through the change. Make sure the action is visible and the source audio sounds continuous.

AI assistance was used to draft and edit this tutorial. The example was run locally.

Top comments (0)