Timestamps in srt files with Mathematica

⇠ Back to Blog:Hacks

This describes a hack to manually find the overlapping subtitles in a srt file (and do other more interesting things).

Subtitle files or subRip .srt contain the text along with timestamps, in a characteristic format. This is, for instance, the opening of A Clockwork Orange:

1
00:01:39,516 --> 00:01:41,268
There was me.

2
00:01:41,685 --> 00:01:44,479
That is, Alex,
and my three droogs.

3
00:01:44,688 --> 00:01:48,358
That is, Pete, Georgie and Dim.

kdenlive has a great subtitle facility. Unfortunately, when cutting a subtitle in two (to better distribute the text from speech recognition, for instance), it will often have the ending timestamp of the first chunk overlap (be later) with the starting timestamp of the next one. That can be very short with the effect of not actually overlapping the text on screen but moving it up, to leave room for the fleeting milli-seconds from the previous text. The final effect is disagreeable and weird, so one would like to avoid such overlapping.

There is a go script [1] to sort this out but on a broken ubuntu repo and in urgent need to fix that problem (for Tom Taylor's IOP Evening Lecture), I hacked the following, using Mathematica.

The notebook is available here: timing-srt.nb.

First I extract the timestamps only (this is best done with sed):

cat tom-iop-lecture-edited.srt | sed -n '/-->/p' > timestamps

Then I will leave those with need of the details to turn directly to the notebook, but the highlights of the procedure are:

  • Split the timestamps:
StringSplit[#, "-->"] & /@ subt
  • Convert the weird srt notation (in particular for ms) into numerical times:
ltimes = Map[
   AbsoluteTime[{#, {"Hour", ":", "Minute", ":", "Second", ",", 
       "Millisecond"}}] &, ltimes, {2}];
  • Find the positions where there is an overlap:
Drop[Flatten[
  Position[ltimesStart - RotateRight[ltimesStop, 1], 
   val_ /; val < 0]], 1]

So for instance, in Tom's subtitling, the 84th occurrence was found, which means both 83 & 84 overlap:

83
00:05:16,970 --> 00:05:19,900
So this is clearly having a big impact.

84
00:05:19,898 --> 00:05:24,041
There's an Office for national statistics data here

and this can be fixed either by bringing down the stopping time 00:05:19,900 of the first subtitle to 00:05:19,897 (one could put zero delay with 898) or bringing up the start time of the second subtitle 00:05:19,898 to 00:05:19,901.

There are more interesting things to do (especially if there are many overlaps, in which case one wouldn't want to do this by hand). For instance, one can look at the duration of speech (as measured by the subtitling) along the video. In the case of Tom's talk, that would not be particularly interesting except that there happened to have a funny interruption of his lecture:

Screenshot 20230709 230852.png

But, besides this awkward moment for him (and funny for the rest of us), such features might be more interesting for movies. I couldn't resist having a quick look at four famous pieces which, off the top of my head, appeared to me as quite distinct the ones from the others:

SpeechDurationInMovies.png

It would be interesting to do that properly (averaging over various srt, checking oddities, etc.) and look for patterns of commonalities and departures, style or just pure genius (as is the case for Kubrick.