My Departement Of Nerdcore Enthusiasm

Tutorials and the like. Things which I have committed myself to. Often linux/UNIX-related.

tirsdag den 7. december 2010

Add subtitles to video clip with pure text (.srt) file


-----------------------------------
Note on revisit
On March the 14, 2013
When revisiting this poste I realized that people are actually reading this instruction. I also realize that the method I am advising for "burning" the subtitles to the video is no longer the method I would recommend. So in the bottom of this post I have added the method for burning subtitles that I use now.

-----------------------------------

I was pretty excited when I discovered that I could add subtitles to a video clip just by typing them into a plain text file on a very specific way. Save this file with extension .srt and then call it from my media player when playing the video clip. So I will just write about here.

You can see some specifications for the configuration of a subtitle file here:
http://www.matroska.org/technical/specs/subtitles/srt.html

Here is a extract of my .srt file:

1
00:00:04,000 --> 00:00:10,000
This little tech video was made during my
work making a slid show of my picture book:

2
00:00:11,000 --> 00:00:17,000
THE SILVER DOOR
-A Children's Book About Sex

3
00:00:18,000 --> 00:00:24,000
Inspired by some beautiful books from my
childhood. Books derived out of
Rudolf Steiner's impulse in pedagogy.

4
00:00:25,000 --> 00:00:31,000
It is a project I've been working on for many
years now. I started back in 2003 with drawings,
painting and writing

I am a Linux user and very happy with (CLI) mplayer (a very powerful media player).

So if I want to play my videos with sub-texts and I have a video clip called:

littledemo.avi

and a. srt (text file) called:

littledemo.srt

Then I can do this with the command:

mplayer -sub littledemo.srt littledemo.avi

Burn subtitles to video clip

Then I saw that you could also burn the subtitles into the video so it was a part of the video clip itself. It was a feature I wanted to use so I spent some time on how I could do this. In this solution I use mplayer to do it. Mplayer can save the stream in a special (raw) format, which in turn can be convert to a more "normal" and less bulky format with a program from a tiny package called mjpegtools. I had to install this little application package to my system in order to be able to run this process but that was peanuts and took only a moment.

And here is the commands I use to "burn" the subtitles off the .srt (pure text) file into my video clip:

mplayer -sub littledemo.srt -vo yuv4mpeg:file=yuv4mpeg.y4m littledemo.avi

Now, the finished file is, in yuv4mpeg.y4m. Now we send this file through mjpegtool program mpeg2enc. We use "cat" to send the file to "mpeg2enc" using a pipe (|).

cat yuv4mpeg.y4m | mpeg2enc-f 8-o littledemo.m2v

Then you have the video clip with the subtitles embedded in ordinary mpeg2 format.

And here is a little example video clip I med with this method:



love
Mikkel


Note on revisit
On March the 14, 2013

As I wrote at the top of this post the method to burn the subtitles into the video is no longer the one I would recommend. Today I do it with mencoder. Mencoder is a brother program for mplayer. Most often mencoder is packed with mplayer in distribution. Mencoder is for manipulating video and audio files. I will give here an example of a command I used when i "burn" srt subtitles in my videos to day with mencoder.

mencoder -utf8 -subfont-text-scale 3 -sub subtextfile.srt videofile.avi -o outputfile.avi -oac copy -ovc lavc