How to merge subtitle to video?

If your TV can play movies that have subtitles muxed into the same file as the video, there are many advantages to adding the subtitles as a subtitle track, instead of burning them into the video.

mkvmerge -o movie_with_subs.mkv   movie.mp4  subs.srt

That will include all tracks from the mp4 (video, audio, chapters), and subs from the srt as a text subtitle track. It takes about as long as copying the file, since it doesn't have to decode/re-encode the video.

Ubuntu packages mkvmerge in mkvtoolnix. There's an mkvtoolnix-gui package, with a gui frontend. It has a lot of options to let you control things like the subtitle offset.

The major advantage to this is that you avoid degrading the quality with another decode/encode cycle of generation loss. It's impossible to avoid losing quality when transcoding, and it takes a lot of CPU time to even come close to the quality-per-filesize of a well-encoded source. (e.g. x264 with -preset slower, or if your player supports it, x265 if you're willing to spend a huge amount of CPU time to make smaller files that still look good). If you don't care about file size because you're just streaming it to your TV, transcoding with x264 with -preset veryfast -crf15 can run quickly and lose minimal quality.

Another advantage to muxing subs is that you can then toggle the subs on/off, or have your player show them in a different position on screen.

You can also extract them later and search them if you're trying to remember a line from the movie.

You can even extract them, fix typos, and mux them back in.


I use Hand Brake on my ubuntu 16.04 great application and very simple to use . https://launchpad.net/~stebbins/+archive/ubuntu/handbrake-releases

Installation:

sudo add-apt-repository ppa:stebbins/handbrake-releases
sudo apt-get update 
sudo apt-get install handbrake

How it work:

  • Click on source and chose movie.
  • From subtitle List tab click on Add.
  • don't forget to tick on Burn into video. List item
  • finally click on Start.

I think that MKVToolNix is the simplest and easiest-to-use free tool available for you to merge a video file with a subtitles file. Just install it by e.g. running a terminal/shell command such as:

sudo apt-get install mkvtoolnix mkvtoolnix-gui -y

...and then run it, right-click the Source files area in order to add your video file and your subtitles file (step 1, at the picture below), specify the location of the destination MKV video file (step 2) and then click on Start multiplexing (step 3). Done.

MVKToolNix's main window

MKVToolNix muxes/multiplexes the subtitles track/stream along with the video track/stream. This means that the output MKV file (e.g. /tmp/output.mkv) will be a video file that contains subtitles embedded in it and you will be able to turn the subtitles on and off, while such MKV video is playing.

PS: if your Android TV doesn't play MKV video files, use MX Player, Kodi Player or VLC Media Player to add MKV playback support.


What if you want subtitles "fused" (hardcoded) in the video, instead of just muxed/multiplexed?

In such case, a laborious-yet-rewarding approach consists of (1) converting your subtitles file to the SSA format, (2) editing the SSA's Style line and then (3) using avconv/ffmpeg to merge/hardcode such stylized subtitles into the video track/stream (the subtitles' characters will then be converted to pictograms or graphical symbols, i.e. pixels not only "laid over" the video's pictures/frames but replacing some of such pixels).

Here's how to do it:

  1. Use a subtitles editor such as GNOME Subtitles (to install it from the shell, run sudo apt-get install gnome-subtitles -y) to convert your subtitles file (e.g. input.srt) to the SSA format (e.g. input.ssa) and then save the SSA file in /tmp (you'll thus have /tmp/input.ssa).
  2. Use a simple text editor such as Gedit (install it with the shell command sudo apt-get install gedit -y) to open your SSA file and then replace the entire Style line by this one:

Style: Default,Arial,16,&H00FFFF,&H00FFFF,&H00FFFF,&H77000000,2,0,3,2,1,2,10,10,10,0,0

After replacing the Style line, save the SSA file and then close the text editor. The configuration line above will globally preset the subtitles with a 16pt yellow Arial font and will add a semi-transparent black background behind the subtitles (to make reading them easier).

  1. Now it's time to use avconv:

    3.1. Install the avconv and ffmpeg packages by running this shell command:

    sudo apt-get install ffmpeg libav-tools -y
    

    3.2. Move your video file (e.g. input.avi) to the /tmp folder, in order to end up having e.g. /tmp/input.avi and /tmp/input.ssa

    3.3. Run the shell command cd /tmp in order to cause the Linux shell to access the /tmp directory

    3.4. Hardcode the SSA subtitles into the video file by running this shell command:

    avconv -i input.avi -map 0:0 -map 0:1 -c:v libx264 -aspect 16:9 -q:v 1 -b:v 512k -strict -2 -c:a aac -ac 2 -filter:v subtitles=input.ssa output.mp4
    

The output of the command above will be /tmp/output.mp4 and you'll notice that such MP4 video file will have hardcoded bitmap subtitles.

If you want to decrease the quality of the merged/output video in order to make its file smaller and possibly wait less time for the merging process to end, decrease 512k to a smaller value (e.g. 256k). If you decide to increase the quality of the merged/output video at the cost of getting a bigger file size and possibly waiting more for the merging process to end, increase 512k to a bigger value (e.g. 768k). If the original (input) video file has an aspect ratio of e.g. 4:2 and you want to preserve it, change -aspect 16:9 to -aspect 4:2. If you don't know the aspect ratio of such video file, open it with a media player and then check it out. For instance, if you open it with VLC, then hit Ctrl + i while it's playing and then select the Codec tab, you'll see such aspect ratio informed at the field named Decoded format.

Tags:

Video