Adjust subtitles timing with Perl 1-liner
I usually record movies off TV with only one soundtrack – English. And it happened that I needed to give one such movie to someone Russian-speaking.
The solution was to find the appropriate subtitles in Russian and hand the subtitles file along with the video file.
The only problem was the subtitles were for the DVD version of the movie and I only had a TV recording.
A little Perl programming effort did the trick.
Technically I needed to adjust all the timings in the subtitles .srt file to shift 6.5 seconds forward.
Here’s a fragment of the subtitles .srt file:
4
00:00:49,654 –> 00:00:51,609
Прости, забыл, как тебя зовут?5
00:00:51,773 –> 00:00:54,286
-Пэки.
-Пэки?
-Ага.6
00:01:01,154 –> 00:00:56,331
Мы что, заблудились, пэки?
The whole Perl program to catch those timestamps and subtract 6.5 sec from each of them is just one line long – a cool 1-liner example:
perl -pe '$/=" "; s^(\d\d):(\d\d):(\d\d),(\d\d\d)^$s=$1*3600+$2*60+$3+$4/1000 -6.5; sprintf("%02d:%02d:%02d,%03d", int($s/3600), int($s%3600/60), $s%60, ($s-int $s)*1000)^e' < /mnt/pine/media/movies/TV-movies-Better/10-items-or-less-offsync.ru.srt > /mnt/pine/media/movies/TV-movies-Better/10-items-or-less.ru.srt
Similarly other adjustments may be performed, like stretching the file from 24fps (film) to 25fps (PAL) or vice-versa – the “-6.5” program token would change to “*25/24”.
Amazing (and effective) piece of code.
I am still trying to figure out how is it possible to put all that code in the second half of the substitution: $d, the math and the sprintf! I guess that I need to try some coding using the e modifier.
This is a huge example of the power behind perl.
THX
alberto
The “e” modifier allows you to use any expression which produces a string.
In this example I have used sprintf with a specific output format, though any expression could be used instead, e.g. concatenation of substrings.