Saturday, August 25, 2012

How to add and synchronize subtitles

This is tutorial on

  • Adding subtitle to a video clip and synchronizing it correctly. 
  • Shifting the subtitles correctly for out of sync subtitles.

Adding subtitle to a video clip

Lets say that the video clip is from a movie and we want to cut the subtitles from that portion of the movie and add it to this clip. The only problem is that the subtitles files are written in a continuous manner which makes it difficult to plain cut and paste operation of the relevant section. 
The steps involved are --

1.Get the subtitle file. 

2. Get the relevant portion of the subtitle file from the whole srt file and save it in another srt file.
If for eg. we want know some words at the beginning and the end we can extract the text from the srt file using grep.

Example file : The relevant section is in yellow.

01:53:46,980 --> 01:53:48,140

01:54:17,648 --> 01:54:23,780
I'm a foreigner,
I came from foreign...

01:54:34,699 --> 01:54:40,569
For proud young men,
I left Washington...

01:54:51,148 --> 01:54:53,673


01:58:02,773 --> 01:58:08,541

New Zealand, Netherlands,

Thailand, Finland landed here....


01:58:08,712 --> 01:58:11,078

Landed here?

What should we do now?


01:58:11,415 --> 01:58:17,513

Put hand over hand
and dial the land line...

 We search for the term "foreign" because that is what we can recognize from the video. And extract till the word "Finland"

cat | grep foreign -A 160 -B 2 | grep Finland -B 160 >

What this command does : selects 160 lines AFTER the word "Foreign" from the srt file and 2 lines BEFORE it. Since the 160 lines were added ad hoc it has to then cut the lines before the word "Finland" which is done by the last grep command. Then the command dumps the text into a file which is named the same the video clip file with the extension changed to srt. 

3. Rename the clipped srt file so that the filenames are name.

4. Now open Gnome Subtitles. If it is not installed then install it

sudo apt-get install gnome-subtitles

Open the file clipped srt file. In this example it is 

As you see the timings for the srt are all messed up. It starts at 1hr 54 min 7sec. That is way too far. We need to readjust the starting time.

5. Adjust start time. We will use Timings -> Synchronize.

But first we need to select the frame where we want the subtitles to begin and then the frame where we want want it to end. Gnome-subtitles would appropriated stretch the subtitles by their interval ratios so that all text appear at the right moment.

Select the frame for the first sentence. 

As you can see it is not at the starting point.

Now goto Timings -> Synchronize. And press Add

6. Select the last line of the subtitle. Select the last frame where you want the subtitle to appear.

Repeat  procedure #5 with the end frame also. 

NOTE :- Remember to select the last line of the subtitle first otherwise the synchronize dialog would just replace the previous frame.

7. Save the subtitle file.

Adding subtitles into a video file

The subtitles can either be hard coded into the video or they can be soft coded into a container like mkv. The advantage of hard coding the subtitle file is that the subtitles are then viewable on all devices, even when the file is read by the TV from an USB device. The disadvantage is that it can never be turned off. The pros and cons of the soft coding is exactly the opposite. Since I play my files on my computer or phone and then stream them to the TV using Chromecast, I will adopt the latter procedure of soft encoding the subtitle file into the video file. This allows far greater flexibility. In fact, with modern smart/streaming devices and TVs hard-encoding is not required at all.

To soft-encode the subtitle file we need to create a container (like mkv) and include the video and the subtitle file in it. A simple tool called mkvmerge can do the job in Linux. If mkvmerge is not already installed then the mkv-toolbox needs to be installed first.

sudo apt-get install mkvtoolnix

Then we merge the video and the subtitle files. Let's say the video file is video.mp4 and the subtitle file is

mkvmerge -o output.mkv video.mp4

The file output.mkv should now have embedded subtitles.

Shifting subtitles

Shifting the subtitles when there is a lag with the video can achieved in the player itself. For example in mplayer the z and x keys would shift the subtitles in the backward and forward directions respectively (v disables subtitles in mplayer). In vlc the appropriate keys can be found and also modified in the preferences. 
But the players have the handicap that they cannot adjust for delays more than 10 mins or ever increasing delays.

For ever increasing delays just use the procedure of last section. For larger than 10 min delay use the Timing -> Shift option of the Gnome-subtitles.

No comments:

OK GOOGLE on Samsung Galaxy S7 doesn’t work

To make Ok Google detection work on Galaxy S7 (Galaxy series phones) we need to perform a couple of steps. 1. As long as Samsung S vo...