How to merge subtitle to video?

How can I merge subtitle to a movie to play on my android TV?

I searched for a long time but almost program no longer work on ubuntu or I can't find the repository . Here is some answer but I think it's no longer supported on 16.04


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

Edit: avconv is deprecated in Ubuntu 18.04 and later versions. Hence, if you're using Ubuntu 18.04 or any later version, just install ffmpeg with:

sudo apt install ffmpeg -y

...or:

sudo snap install ffmpeg

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

Edit: if you're using Ubuntu 18.04 or later, the command above must begin with ffmpeg instead of with avconv.

The above command uses the libx264 video codec to create an H.264 video file (.mp4 is just the container's file extension). If you however prefer to create an MPEG-4 video file, use the mpeg4 video codec, instead:

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

Edit: if you're using Ubuntu 18.04 or later, the command above must begin with ffmpeg instead of with avconv.

The output of the commands above will be /tmp/output.mp4 and you'll notice that such MP4 (i.e. x264 or MPEG-4) video file will have hardcoded bitmap subtitles.

If you wish to attempt increasing the quality of the merged/output video (even though at the cost of getting a bigger file size), replace the 512k parameter with a larger one (e.g. 1024k). If you however decide to get a smaller file size (even though at the cost of reducing the quality of the merged/output video), replace the 512k parameter with a smaller one (e.g. 256k). 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.


It now works without converting to .ass on Ubuntu 20.10

They's enabled libass support recently it seems on the package, so now you can just do:

ffmpeg -i hello.mp4 -vf "subtitles=hello2.srt" hello-sub.mp4

Before it was enabled, you needed:

ffmpeg -i hello2.srt hello.ass
ffmpeg -i hello.mp4 -vf ass=hello.ass hello-sub.mp4

You can check for libsass support with:

ffmpeg --version

which now contains:

--enable-libass

The generated output can be seen at: https://www.youtube.com/watch?v=Q0nVA3mYXLU

The test files can be obtained with:

wget -O hello.mp4 https://github.com/cirosantilli/media/blob/master/Top_Down_2D_Continuous_Game_with_Urho3D_C++_SDL_and_Box2D_for_Reinforcement_Learning_first_8_seconds.mp4?raw=true
wget -O hello2.srt https://github.com/cirosantilli/media/blob/master/Top_Down_2D_Continuous_Game_with_Urho3D_C++_SDL_and_Box2D_for_Reinforcement_Learning_first_8_seconds.srt?raw=true

where:

  • the video is a cut of the first 8 seconds of: https://www.youtube.com/watch?v=j_fl4xoGTKU
  • the subtitles were produced manually with Gnome Subtitles, see also: Subtitle editor for 16.04

Related:

  • https://stackoverflow.com/questions/8672809/use-ffmpeg-to-add-text-subtitles/13125122#13125122
  • https://superuser.com/questions/869248/hardcoding-subs-with-ffmpeg
  • https://superuser.com/questions/882425/hardcoding-ffmpeg-subtitles-on-mp4-mkv

Tested on Ubuntu 20.04, ffmpeg 4.2.4.