How to Merge Subtitles to a Movie File
kensai — Wed, 06/25/2008 - 09:25
My refusal to use Windows is what makes me learn to do everything using only Linux, even though it usually requires long times in front of the computer researching and tweaking the hints google.com/linux gives me. But, at least I can say that at the end the results are satisfying, I end up learning more about Linux and how powerful a command line application can be compared to a most GUI application. I must admit that I do prefer GUI in the majority of the cases, is just that from time to time I am amazed by the power of the CLI. My latest adventure was learning how to merge a .srt (movie subtitle file) with a .avi (movie file). How I did it? Well, first we need to have mplayer installed, which on Arch Linux can be done as follows:
# pacman -Sy mplayer codecs
After that you might need to create the mplayer folder (if it isn't there already) so we can add the fonts we would like to use for the displaying of the subtitles in the movie, with that purpose we issue the following command:
$ mkdir .mplayer && cp /usr/share/fonts/TTF/LiberationSans-Regular.ttf .mplayer/subfont.ttf
Change the LiberationSans-Regular.ttf with the font you want to use. If you want to use the Liberation font, install the ttf-liberation package which is available in the community repository.
# pacman -Sy ttf-liberation
OK, we have gone a bit off the curse here with the fonts thing, but by now we should have the mencoder application installed, hopefully this makes us still on track for the merging that is about to happen. The mencoder application is part of the mplayer application we just installed, mencoder is the tool we are going to use to merge the subtitles with the movie. Now, to the better part of this tutorial, how we merge the subtitles with the movie files, the following script should do the trick:
$ mencoder -o fileiwantatend.avi -sub subtitlesfile.srt -oac copy -ovc lavc -lavcopts vcodec=mpeg4:mbd=2:trell theactualmovie.avi
The first .avi we refer to in the above script is the file we want to create at the end of the merge (the resulting file), which could be named movie_subtitle.avi, now, the .srt file referred to in the script is the subtitles file which do not necessarily need to be on .srt format, it could be for example in .sub format, and the latests .avi file referred to in the command is the original movie file which you want to merge with the subtitles.
This command is very simple and has few options so it preserves the original movie file quality without much alterations, if any.
Legend:
# = as root
$ = as user
- Add new comment
- 1935 reads

This is a good way to do it,
catwell (not verified) — Thu, 06/26/2008 - 08:43This is a good way to do it, but you could also use Avidemux, which is GUI software and makes it easier to add useful filters to the processing chain.
Also, I don't like merging subtitles into the video. I prefer to put them with the original video and audio tracks in a container which can handle them separately such as mp4 or mkv. I use command-line tools to do that: MP4Box / mkvtoolnix.
Thanks for the suggestions,
kensai — Thu, 06/26/2008 - 10:02Thanks for the suggestions, but in fact avidemux gave me a headache and I could never figure out how to do it. So I had to rely on mencoder exclusively. It would be interesting for me to actualy find out how I come about doing this on avidemux.
Matroska
Jack H (not verified) — Tue, 07/08/2008 - 14:59If you're interested in working with difficult multimedia files on Linux, I whole-heartedly recommend trying out the Matroska file format. It would have been even easier than using that simple mencoder line, and the format supports as many different video, audio and subtitle tracks as you like. It supports lots of different formats together, so for example you can encode your DVDs with high-quality H.264 video and Ogg Vorbis audio and they will play together with no problems whatsoever, which is more than I could say for AVI!
You can do some cool tricks with the format too. Imagine if you had a foreign film with both dubbing and subtitling in English (or whatever is your preferred language). You can easily store both audio tracks and the subtitle along with the video in a single file, so maybe if you're feeling lazy and you can't be bothered to read subtitles, you can just select the dub track, but then if you wanted a more authentic experience you could use the subtitles and the original audio instead.
Easy to install on Arch, just pacman -S mkvtoolnix; /usr/bin/mkvmerge for the CLI version and /usr/bin/mmg for the GUI.