You load a subtitle file and the lines show up two seconds late. Or they start fine and slip further behind as the film plays. Both are common, and both are easy to fix once you know which one you have.
Which problem is it?
Play the video and check two lines, one near the start and one near the end.
- Same gap in both places. Every line is, say, 2.5 seconds late. That's a constant offset. You add or subtract one number from every timestamp.
- The gap grows. The first line is 1 second late and the last is 9 seconds late. That's drift, and it usually means the subtitles were made for a version of the video with a different frame rate. You fix it by stretching or squeezing all the timestamps by the same factor.
What's inside an SRT file
An SRT file is plain text. Each subtitle is a number, a time range, and the text:
2
00:01:00,250 --> 00:01:02,000
Second line.
The time format is hours:minutes:seconds,milliseconds. Note the comma before the milliseconds, not a dot. That's SRT. VTT uses a dot. So fixing the sync means rewriting every timestamp and leaving everything else alone.
The script
This reads the timestamps with a regular expression, converts them to milliseconds, applies a scale and an offset, and writes them back:
import re
import sys
TIME = re.compile(r"(\d+):(\d{2}):(\d{2}),(\d{3})")
def to_ms(match):
h, m, s, ms = map(int, match.groups())
return ((h * 60 + m) * 60 + s) * 1000 + ms
def fmt(ms):
ms = max(0, round(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 retime(text, offset_ms=0, scale=1.0):
return TIME.sub(lambda m: fmt(to_ms(m) * scale + offset_ms), text)
if __name__ == "__main__":
src, dst, offset = sys.argv[1], sys.argv[2], float(sys.argv[3])
with open(src, encoding="utf-8") as f:
text = f.read()
with open(dst, "w", encoding="utf-8") as f:
f.write(retime(text, offset_ms=offset * 1000))
Fix a constant offset
Save it as shift_srt.py. To make every line appear 1.5 seconds earlier:
python shift_srt.py movie.srt movie-fixed.srt -1.5
Use a positive number to push lines later. Any timestamp that would go below zero is clamped to 00:00:00,000, so a line near the very start can lose a fraction of a second.
Fix drift
For drift you need two reference points. Pick one line near the start and one near the end, and note the time in the file and the time you actually hear it.
Say the first line is at 1.000 s in the file but you hear it at 2.000 s, and a line near the end is at 60.250 s in the file but you hear it at 62.250 s. Then:
file_1, file_2 = 1000, 60250 # where the lines are in the file (ms)
real_1, real_2 = 2000, 62250 # where you actually hear them (ms)
scale = (real_2 - real_1) / (file_2 - file_1)
offset = real_1 - file_1 * scale
fixed = retime(open("movie.srt", encoding="utf-8").read(), offset, scale)
open("movie-fixed.srt", "w", encoding="utf-8").write(fixed)
Here scale comes out at about 1.0169 and offset at about 983 ms. A line at 00:01:00,250 moves to 00:01:02,250, which is exactly where you hear it. A scale close to 1.0427 or 0.9590 is a strong hint of a 25 vs 23.976 fps mismatch, which is the usual cause.
Two things that trip people up
Encoding. If the file isn't UTF-8, Python will either fail or save garbage. Old subtitles are often Windows-1252 or similar. Open it with encoding="cp1252" and save as UTF-8.
Other formats. The script only matches SRT's comma-separated times. For VTT, change the comma in the regex and in fmt to a dot.
If you'd rather not run code
You can do the same thing in the browser. The free shift subtitles tool handles a constant offset, and sync subtitles lines the file up against the audio for you. No account needed for either.
Top comments (1)
Nice split between a constant offset and drift. One extra check before you stretch: confirm the video's actual frame rate with ffprobe (r_frame_rate and avg_frame_rate), because a 23.976 file that someone labelled 24 fps will produce a scale very close to 1, and a tiny error at ten seconds becomes a large one at two hours. Your 25 versus 23.976 hint is spot on, since 25 divided by 23.976 is about 1.0427. After the rewrite, watch for two leftovers. A cue whose start was clamped to zero may now be shorter than a readable line, and a linear stretch can make two cues overlap if the original file already had tight timing. A quick pass that shortens or drops any cue that starts before the previous one ends saves a surprise in the player. To preview without burning the captions in, play the video with ffmpeg's subtitles filter against the new file and jump to your two reference lines.