June 27, 2008

Creating DVD subtitles using Linux tools

Author: David Pendell

My mother is completely deaf without her cochlear implant, so closed captions and subtitles on DVDs are standard fare in our house. However, when I make a home movie, I haven't always had a way to subtitle it for her. You can make a video with captions that are part of the video itself, but I could find no way to turn them off -- until I found SRT files and learned how to author a DVD with them using open source tools.

SRT files, created by the SubRip project, are formatted text files that contain information on subtitles and captions for DVDs. With the right tools, you can use them to make a captions that play on a standard DVD player.

The overall process is simple. First, make sure you have a fast computer with lots of memory, hard disk space, and a good DVD burner, because working with video and audio is a resource-intensive process. Load your video file into a milliseconds-accurate video editor such as Cinelerra and get the time indexes for each phrase of dialogue. Enter the dialogue and the time indexes into the SRT file, then integrate the SRT file into a DVD-compliant format and burn it to disk.

To walk through a sample project, I recorded an eight-second video of my father quoting from the Gettysburg address. I then roughed out an SRT file using approximate timings. Working with the video and the SRT file, I fine-tuned the timings into what you will see below. The rest of the commands I ran on the command line using utilities such as FFmpeg, spumux, dvdauthor, and growisofs.

The format of entries in an SRT file is simple. Each entry is preceded by the sequential number of that entry. The second line is a time index that uses the format HH:MM:SS,milliseconds. The first time index is the time to begin showing the text, and the second is, of course, the time to stop. Finally, you can display up to two lines of text.

00:00:00,190 --&gt 00:00:01,800
Four score and seven years ago

00:00:01,820 --&gt 00:00:05,280
our fathers brought forth on this
continent a new nation, conceived

00:00:05,300 --&gt 00:00:07,580
in liberty and dedicated to the
proposition that all men are

00:00:07,600 --&gt 00:00:08,280
created equal.

The video to which I applied this SRT file was an AVI I recorded with my digital camera and transferred to Linux. For me, because I run Ubuntu, transferring the file was simple -- I just connected my camera to the computer with a USB cable. Gthumb popped up and asked if I wanted to import pictures. I selected the file that I wanted and clicked Import. Your method may vary depending on your distribution and camera.

You'll have to experiment with a video player such as VLC or MPlayer to get the timestamps in the SRT file just right. For VLC, click on the File menu, check the "Use a subtitles file" option, and enter the path and name of the file. In MPlayer, the easiest way to specify the subtitle file is to use the command line. Use the -sub option to display the subtitle file.

In both VLC and MPlayer, handling of SRT files is a little delayed. When running either program from the command line, I find that the subtitles do not display for several seconds. When they do display, they are timed well, but several seconds of speech may not have been captioned. With MPlayer, you can use the -subdelay option to change the subtitle delay. With VLC, when you specify the subtitle file, you may need to adjust the delay under the advanced settings. These delay options set the amount of time before subtitles are displayed. I find that a delay of -1 seems to fix the problem. Experiment with them until you find something that works.

Once you've determined the proper time indexes within the SRT file, you're ready to translate the AVI into an MPEG, the format you'll eventually burn to the DVD. Get to a command line, switch to the directory that contains your video file, and run the following command, making sure the change the filename to what you want:

ffmpeg -i test.avi -target ntsc-dvd test.mpeg

This is the simplest command that will accomplish the task, but FFmpeg offers many options, particularly on the output side. If you are planning to work with video a lot, you would benefit from investigating the options more thoroughly, as this article only touches on the most basic options. The -target option supports output in the European PAL format as well as North American NTSC, and even SECAM, used in France and Russia.

Take note of some information that FFmpeg spits out during processing. In particular, look for the following lines, and note the highlighted information.

Output #0, dvd, to 'test.mpeg':
Stream #0.0: Video: mpeg2video, yuv420p, 720x480, q=2-31, 6000 kb/s, 29.97 fps(c)
Stream #0.1: Audio: ac3, 48000 Hz, mono, 448 kb/s

Knowing the dimensions and the frame rate of the video is critical for the next step. You need to multiplex the subtitles file into the MPEG stream using spumux. One of this utility's more powerful features allows for several subtitles files to be muxed into an MPEG stream, which is handy if your target audience may have more than one native language.

Spumux is different from most *nix command-line utilities in that the parameters for the files that you are manipulating, the video and corresponding SRT file, are contained within an XML file. Also, spumux expects that any fonts that you are using for the project are contained within the .spumux folder in your home directory. If you don't have one, the program will error out. I have experimented with absolute paths within the XML file, but it didn't seem to make a difference.

Here is the NTSC version of the spumux XML file, with the important parts highlighted.

<textsub filename="test.srt"
fontsize="28.0" font="arial.ttf"
force="yes" movie-fps="29.97" subtitle-fps="29.97"
movie-width="720" movie-height="480"

Test.srt refers to the subtitle file. The SRT file, MPEG file, and the XML file should all be within the same directory. You can replace arial.ttf with the name of the font file that you are using. Just be sure to use a True-Type font and that there is a copy of the font in the .spumux folder in your home directory. The force option causes the subtitle to be displayed automatically. You can set it to no if you don't want this. Unless you know what you are doing, movie-fps and subtitle-fps should match each other and also match the fps information that you got from FFmpeg. Also make sure that movie-height and movie-width match what FFmpeg told you.

Once the files are in place and the configuration files are ready, run the command:

spumux -s0 -m dvd -P test-sub.xml < test.mpeg > test-sub.mpeg

The option -s0 instructs spumux to place the specified subtitle in the first subtitle slot; -s1 would be the second slot, and so on. The -m dvd option tells spumux that the output is for a DVD-formatted video. Spumux can handle other types too, such as SVCD. -P shows a progress meter. test-sub.xml is the name of the configuration file, test.mpeg is the name of the video file, and test-sub.mpeg is the name that you want the resulting file to have.

Now you have a file you can use to create the video for the DVD. I use dvdauthor, a powerful program capable of commercial-quality menus. There are a large number of online tutorials for the program, along with graphical front end programs for it. The dvdauthor home page is a good place to start, as it lists several helpful links. Here's a simple version of the XML file that controls dvdauthor:

<dvdauthor dest="DVD">
<vmgm />
<vob file="test-sub.mpeg"/>

This is probably the bare minimum file that you can get away with. Look online if you want examples of more complicated setups. Substitute test-sub.mpeg with the name of the subtitled file you created and run this command:

dvdauthor -x test-dvd.xml

Test-dvd.xml is the name of the file that I put the dvdauthor configuration in. Dvdauthor will run for a few minutes and put everything into a subfolder named DVD.

All that remains is to burn the video to DVD. Put a blank recordable DVD into your DVD recorder and run the command:

growisofs -Z /dev/scd0 -dvd-video DVD/

Replace /dev/scd0 with the device path to your DVD recorder. When growisofs is finished, your DVD should be playable in a standard DVD player.

The options that I have shown here only scratch the surface of what each program can do. Have fun and experiment. You might even try automating the process with Python or Perl scripts. If you're going to do a lot of subtitle editing, the gaupol editor looks promising. In any case, have fun with your home movies and impress your friends with your subtitling prowess.


  • Graphics & Multimedia