Computer science thoughts

Embed and adjust subtitles in a MKV file

A technique for adding a subtitle file (SRT) to an MKV file and adjusting the subtitles to spoken text.

If you have an MKV file without proper subtitles, you can download a subtitle file from the internet, drop it in the same place as your video and then play the video with your usual player. Indeed, video playback software (such as VLC or MPC-HC) are able to automatically load the subtitle files corresponding to the current video, if they are in the same directory and have the same name as the MKV file.

However, it is more interesting to embed the subtitle file directly into the MKV file in order to have a single file containing the video, audio, chapter and subtitles. Here's how to do this in three steps.

Download a SRT subtitle file

There are several possible extensions for subtitle files; one of the most common is SRT (this is a text format).Several sites offer to recover subtitle files. Here are a few that offer quality subtitles:

Search for your movie and download the corresponding file. If you downloaded an archive, extract the SRT file it contains.

Embed SRT file into MKV file

To include the SRT file in your MKV file, use MKVToolnix Gui :

  • Run MKVToolnix Gui
  • Drag and drop your MKV file
  • Drag and drop your subtitle file. To the question "Add or appending files, choose the first option (Add as new source files to the current multiplex settings)

  • Choose a destination file at the bottom of the screen and click on "Start multiplexing".
  • Once the processing is complete, launch the file thus created and test it to see if, from the beginning to the end of the file, the subtitles are well aligned with the image and the sound. If not, the next section will help you adjust this.

Adjust subtitles to match picture and audio of MKV file

Depending on your MKV file and the subtitle file, you may have a time lag between the dialogue and the displayed text. If the video playback software allows you to adjust this offset more or less, it is preferable to do it directly in the MKV settings.

There are many reasons for this delay:

  • The video contains unintended credit footage in the subtitle file
  • Video and subtitle file are timed to a different number of frames per second
  • The subtitle file doesn't exactly match the video, with extra or cut scenes...

In the latter case, it will be necessary to search for another subtitle file as this would require modifying each text of the original file. For the first 2 cases, MKVToolnixGui allows you to shift the starting point of the subtitle file and also to practice a stretch on the durations of each display. The 1st parameter is easier to find and it is therefore preferable to start there.

Find the delay of a subtitle file

Here is how to find the time difference between the subtitle file and the video:

  • Launch your video with your usual player
  • Locate the keys to vary the offset of the subtitles (F1 / F2 on MPC-HC, G / H on VLC)
  • Go to the beginning of your video file, in the first dialogues of the film and try to adjust the subtitles on the voice, by trial and error
  • Once the subtitles are adjusted at the start, go to the end of the movie and verify that this adjustment is maintained.
    • If so, you have found the number of milliseconds to shift (which can be positive or negative depending on the case) and you can move on to the next step
    • If not, your video and subtitle file do not have the same frame-per-second basis. Go to the next section.
  • Close your video player
  • On MKVToolnix Gui, click on the added subtitle and in the properties area, on the right, adjust the property "Delay (in ms)" with the value found. You can then click on "Start multiplexing" to generate your file with this setting.

Finding the Stretch Time for a Subtitle File

This last step is to adjust both the display offset and the stretch to apply to the durations of your subtitle file. By relying on 2 positions in the film, we can precisely calculate the ratio:

  • Locate 2 dialogues in your video file, one at the start of the film and the other at the end of the film (not necessarily the very first dialogue or the last sentence spoken, because given the length of a film, we should have something fairly precise).
    Example : I will take a dialogue at 2 min 50 seconds and another at 1:50 and 19 seconds.
  • Look for these 2 dialogs in your SRT subtitle file, by opening it with a text editor
    Example : the 1st dialog is indicated at 00: 03: 13,653 and the second at 01: 55: 13,825
  • Convert the times found to seconds (it is not essential at this stage to have millisecond precision).
    Example :

    Dialogue start170 seconds193 seconds
    Dialogue ebd6620 seconds6913 seconds
  • Calculate the difference between the beginning and the end of the dialogues.
    Example :

    End-start différence6620-170=64506913-193=6720
  • Calculate the ratio between the two values obtained. It is this value that we will be able to use as the stretch value.
    Example : 6450/6720 = 0.95982
  • It is also necessary to calculate the possible offset, by applying this ratio to the position of the start dialogue and by comparing it to the start position of the dialogue in the MKV file. Note that the stretch ratio is applied first , and the offset comes next.
    Example : 170-(193*0.95982)=-15
  • You can now enter the two values in MKVToolnix Gui, in the "Delay (in ms)" and "Stretch by" areas.
    Example : we have a stretch of 0.959 (3 to 4 decimal places are often sufficient), knowing that you can also write fractions in the stretch area. There is also a lag of -15000 milliseconds.

  • You can then click on "Start multiplexing" to generate your file with these parameters. Once finished, all that remains is to test.
Dernière modification le 31/10/2020 - Quillevere.net


No inscription needed if you wish to

Search in this website

fr en rss RSS info Informations