A php tool for adjusting or correcting time of subtitles of a video movie

As English is not my mother tongue i do like (at least first time) subtitles running while watching an English movie. This time while i was watching another movie the only subtitles’ file i found over there had a straight delay of 16 seconds at the beginning. In fact , the VLC player which i normally use to play movies does have an ‘advanced option’ to handle delay of subtitles but in my somewhat special case it too failed to handle it properly. In fact i wouldn’t been bothered what i had eventually to do if i could find a most matching subtitles file but i wasn’t lucky enough. Here’s the case that made me to write this small tool:

(I Don’t want to read this post further.. Just take me to the tool.)

Once i had the first delay of 16 seconds set in VLC player I further noticed that along with the movie progressing, after some period of time – say 10-15 minutes – the display of subtitles had been lagging by 2-3 seconds. This was quite frustrating as there was no option left to set it right in VLC and then the only option left was to set titles manually in subtitles.txt file itself.

(For those who are in a hurry and just want to delay or hurry the time of their movie subtitles, just use the tool to enter the amount of time (in SECONDS only) pre-fixing it by – or + (You can skip in case of + anyway) and the piece of subtitles content or entire text content from subtitles file into the textarea below and click “get fixed”. The textarea should show the fixed content as output. Please check Step 2 if you want to fix only a selected part of you your subtitles’ file.)

FOR ALL WHO WANT TO SERIOUSLY KNOW THIS TOOL

Okay. This was where this small but useful piece of php code came handy. Every time i start to feel the lag i would just pause the movies and search for the text appearing on screen and cut the remaining subtitles text starting from that particular entry till the end of the page and pasted into the file i had been using as a source file to the script code. As i was already on WAMP, following is how i did it.

STEP 1

I created a fixsubtitles.php file in my C:/wamp/www folder with the following code:

< ?php
$fixby = " -2 seconds";

$text = file_get_contents("subtitle.txt");

function fix_time($matches) {
$timestring = $matches[1] . ":" . $matches[2] . ":" . $matches[3];
return date("H:i:s", strtotime($timestring . $fixby));
}

$newtext =  preg_replace_callback(
"|(\d{2}):(\d{2}):(\d{2})|",
"fix_time",
$text
);

$fh = fopen("subtitles_changed.txt", "w");
fwrite($fh, $newtext);
fclose($fh);
?>

STEP 2

I created another file i.e. subtitles.txt and placed that one in C:/wamp/www. This would be the source file having incorrectly timed subtitles to be fixed. Let’s say E:\MyMovieName\subtitles\MyMovieName.srt is the subtitles’ file name.

Now as soon i start to feel the delay while movie playing i would pause the movie having some subtitles on the screen.

STEP 3

Now I would open the MyMovieName.srt file in text editor and look for subtitles appearing on the screen. For example below is a subtitles file in which i would want to correct time lag by bringing it 3 seconds back starting from line 451 (assuming that you have got “You should watch the door…” on your screen),

1
00:00:54,345–> 00:00:59,575
This is start of the movie.

…………………………………
…………………………………

450
01:11:20,697 –> 01:11:23,575
You should watch the door.
We cannot wait.

451
01:11:25,118 –> 01:11:27,287
More subtitles down the page
I will bring them 3 seconds back.

…………………………………
…………………………………

2000
02:14:15,637 –> 02:33:17,889
This is End of the file.
And End of the movie as well.

From this subtitles’ file above if i would want to fix time delay by decreasing it by 3 second i would cut all the text starting from line 450 (including 450 ; you may want to fix it starting from 445 or like that usually i would include 4-5 previous titles as well) till the end of file. Then i would go to empty C:/wamp/www/subtitles.txt, the source file for the script (remember that?) and paste and save. After this i would go to fixsubtitles.php file and make a slight change to the line 1 i.e. $fixby = ” -2 seconds”; Here you can set the desired time interval, that is how much delay or hurry you want in to the display of title and save it. Now i would run my script file that is, fixsubtitles.php in the browser like so : http://localhost/fixsubtitles.php and the subtitles with fixed time should be saved as “subtitles_changed.txt” file in the same folder i.e. C:/wamp/www/. After opening this file i would ctrl+A (select all) the entire text in this file and cut-paste it right under 449th set of subtitles in E:\MyMovieName\subtitles\MyMovieName.srt file, where i had cut the subtitles text from and had pasted into the subtitles.txt, the source file to the script.

2 thoughts on “A php tool for adjusting or correcting time of subtitles of a video movie

Leave a Reply