One of my current projects involves editing several hours’ worth of subtitle files in the
.srt format to accompany an online course in numerical optimization. Because we haven’t
decided exactly how we want to break up the videos, I needed an efficient way to delay
or advance all the subtitles in time, so I wrote a small Python program called
srt_delay.py today to help me with this task.
Usage examples below the cut, or you can just go straight to the readme in the GitHub repository.
Here is what the input file looks like:
$ head sample_input.srt 1 00:00:15,000 --> 00:00:17,951 At the left we can see... 2 00:00:18,166 --> 00:00:20,083 At the right we can see the... 3 00:00:20,119 --> 00:00:21,962
Each line contains either an index number for the subtitle (
1), the subtitle’s
00:00:18,166 --> 00:00:20,083), or the subtitle text itself.
Suppose we want to advance the subtitles by 1.5 seconds. We can pass
this interval in
.srt timestamp format (
$ python3 srt_delay.py sample_input.srt -a 00:00:01,500 | head 1 00:00:13,500 --> 00:00:16,451 At the left we can see... 2 00:00:16,666 --> 00:00:18,583 At the right we can see the... 3 00:00:18,619 --> 00:00:20,462
srt_delay.py prints to standard output. In most cases,
you will want to save the output to another file:
$ python3 srt_delay.py sample_input.srt -a 00:00:01,500 > sample_input_advanced_by_1.5s.srt