Sub topic 13
The SRT format and caption timing
This chapter assumes you can run a command in a terminal and that you have a lecture video with a transcript. The transcript can be rough. Earlier chapters covered cutting mistakes and creating the transcript. Here you turn that text into a correctly timed SRT file and keep it correct when the video changes.
What an SRT file is
SRT (SubRip Text) is a plain-text caption format. It is a list of cues. A cue is one caption that appears at a start time and disappears at an end time. Almost every video player, editor, and platform accepts SRT, which is why it is the usual exchange format between transcription tools and editors.
A minimal file looks like this:
1
00:00:00,000 --> 00:00:03,500
Welcome to this lecture on video editing.
2
00:00:03,500 --> 00:00:07,000
First, we will remove a mistake
and then add a zoom.Anatomy of a cue
Each cue has the same four parts, in this order.
| Part | Example | Rule |
|---|---|---|
| Index | `1` | A whole number on its own line. Cues are numbered 1, 2, 3 and so on. |
| Timing line | `00:00:00,000 --> 00:00:03,500` | Start, the arrow `-->`, then end. |
| Text | `Welcome to this lecture...` | One or more lines of text. Two lines is the usual maximum. |
| Blank line | (empty) | Ends the cue. Without it, the next cue is read as text. |
The timing format is HH:MM:SS,mmm: hours, minutes, seconds, then milliseconds after a comma. The comma is the most common source of errors. The related WebVTT format uses a period instead. Mixing them up makes many players ignore the cue or the whole file.
Other practical rules:
- Save the file as UTF-8 so accented letters and symbols survive. A byte-order mark at the start can confuse some tools.
- Keep the end time later than the start time.
- Keep cues in chronological order. Cues should not overlap, because most players show only one caption track line at a time and overlapping cues can stack or flicker.
- Use plain text. Some players accept basic tags such as
<i>, but support varies, so do not depend on them.
How cues are timed
A caption is only useful if it appears when the viewer hears the words. Timing choices follow from that.
Start. Start a cue at, or slightly before, the first spoken word. A start up to about 100 ms early feels natural. A late start makes viewers read after they have already heard the sentence.
End. Hold a cue a little after the last word, about half a second, so slower readers can finish. Do not hold it so long that it overlaps the next sentence.
Duration. A cue shorter than about one second is hard to read. A cue longer than about six or seven seconds usually means you packed too much text into it or held it through silence. Split long sentences at natural pauses, such as clause boundaries, rather than in the middle of a phrase.
Gaps. If speech continues, let one cue end at about the time the next begins. If there is real silence, let the caption disappear. A caption left on screen during dead air misleads the viewer.
Line length and reading speed get their own chapter. For now, treat these numbers as sensible defaults.
Timing depends on the final video
The most common caption bug in lecture editing is a file whose timestamps match the uncut recording. Cutting out a section moves everything after it earlier. If you remove 8 seconds starting at 72 s, then something spoken at 80 s in the raw file now happens at 72 s in the edited video. Captions that were not shifted drift 8 seconds late.
The simplest way to avoid this is to transcribe or time the captions after all cuts are final. If you must reuse an older SRT, shift it.
A script that applies a cut to an SRT file
The script below removes a time range from an SRT file, the same range you would remove from the video. For example, it matches the first ffmpeg example in this tutorial, which deletes 72 s to 80 s. It maps every timestamp to its new position, drops cues that fall entirely inside the cut, and warns about cues that partly overlap it.
import re
import sys
TIME = re.compile(r"(\d+):(\d{2}):(\d{2}),(\d{3})")
def to_ms(text):
h, m, s, ms = map(int, TIME.fullmatch(text.strip()).groups())
return ((h * 60 + m) * 60 + s) * 1000 + ms
def to_text(ms):
h, rest = divmod(ms, 3_600_000)
m, rest = divmod(rest, 60_000)
s, ms = divmod(rest, 1000)
return f"{h:02}:{m:02}:{s:02},{ms:03}"
def parse(path):
with open(path, encoding="utf-8-sig") as f:
content = f.read().replace("\r\n", "\n").strip()
cues = []
for block in re.split(r"\n\s*\n", content):
lines = block.split("\n")
start, end = (to_ms(part) for part in lines[1].split("-->"))
cues.append((start, end, lines[2:]))
return cues
def remove_range(cues, cut_start, cut_end):
length = cut_end - cut_start
def remap(t):
if t <= cut_start:
return t
if t < cut_end:
return cut_start
return t - length
result = []
for start, end, text in cues:
if start < cut_end and end > cut_start:
print(f"Review: cue at {to_text(start)} overlaps the cut", file=sys.stderr)
new_start, new_end = remap(start), remap(end)
if new_end > new_start:
result.append((new_start, new_end, text))
return result
def write(path, cues):
with open(path, "w", encoding="utf-8") as f:
for index, (start, end, text) in enumerate(cues, start=1):
f.write(f"{index}\n{to_text(start)} --> {to_text(end)}\n")
f.write("\n".join(text) + "\n\n")
if __name__ == "__main__":
source, target, cut_a, cut_b = sys.argv[1:5]
cues = parse(source)
cues = remove_range(cues, round(float(cut_a) * 1000), round(float(cut_b) * 1000))
write(target, cues)Run it like this:
python cut_srt.py captions.srt captions_cut.srt 72 80The script also renumbers the cues, so the index stays sequential after a dropped cue. A cue that overlaps the cut is clamped to the cut point, but its text may now describe words that no longer exist. That is why it prints a warning. Open those cues and fix the text.
If you make several cuts, apply them from the last cut to the first. Then earlier cut times still refer to the original timeline and do not need recalculating.
Common problems
- Captions drift steadily later. The cut offsets were not applied, or the captions were timed against a different version of the video. Check a cue near the start and one near the end. A constant offset means a missed shift. An offset that grows means a mismatch such as a different frame rate or a variable-speed edit.
- Captions are off by a fixed amount from the start. The video may begin with a leading section, such as an intro, that the captions do not account for. Shift every cue by that amount.
- A caption flashes by unreadably. The cue is shorter than about one second. Merge it with a neighbor or extend its end time if there is room before the next cue.
- Some players show nothing. Look for a period in a timestamp, a missing blank line, or a missing
-->. Converting the file is a quick validity check, because ffmpeg stops with an error on a malformed SRT:
ffmpeg -i captions.srt captions.vttReview checklist for an SRT file
- Every timestamp uses
HH:MM:SS,mmmwith a comma. - Cue numbers run 1, 2, 3 with no gaps.
- Every cue ends with a blank line.
- No cue ends after the next one starts.
- No cue is shorter than about one second or longer than about six or seven seconds.
- The first, middle, and last cues match the audio in the final cut of the video.
Exercise
Your raw recording has a cue "00:01:20,000 --> 00:01:23,000". You cut 72 s to 80 s from the video. What should the cue's timing line become, and what would you do with a cue "00:01:10,000 --> 00:01:14,000"?
Show answer
The first cue starts exactly when the cut ends, so it shifts earlier by 8 seconds: "00:01:12,000 --> 00:01:15,000". The second cue (70 s to 74 s) overlaps the cut (72 s to 80 s). Its end is clamped to the cut point, 72 s, so it becomes "00:01:10,000 --> 00:01:12,000". Its text may now describe words that were removed, so review and edit it by hand.
Exercise
A player ignores your captions. The first cue reads "1", then "00:00:01.000 --> 00:00:03.000", then its text. What is wrong, and how could you confirm the file is valid after fixing it?
Show answer
The timestamps use periods before the milliseconds. SRT requires commas, so the line should be "00:00:01,000 --> 00:00:03,000". After fixing every timing line, run "ffmpeg -i captions.srt captions.vtt". If ffmpeg converts the file without errors, the structure is valid. Then play the video to check the timing.