Tuesday, September 23, 2008

Command Line DVD Authoring: Part 2

This is part of a multi-part series on creating DVDs manually from the command line. It is not expected that regular users will generally be performing video editing or DVD authoring from the command line. Rather, this guide is intended for programmers who may be wishing to build a front-end for DVD authoring, and don't want to sift through miles of documentaion just to get the basics. This guide makes use of command-line utilities already freely available, but is not meant to be a complete set of documentation for any of these utilities. Instead, consider it a primer. The parts in this series are:

Part 1: Editing a Video File with MPlayer
Part 2: Converting a Video to DVD Format
Part 3: Making a DVD Menu
Part 3.1: Extracting Audio From A Video
Part 4: Building a DVD .iso File

It should be noted that while the programs themselves should remain relatively the same between Linux distros, the name of the packages themselves are likely to change. This tutorial was written using Ubuntu 8.04 as the reference OS, so if you use a different distro, your mileage may vary.

Part 2: Converting a Video to DVD Format

Part 1 dealt with using mplayer to create an edl (edit decision list) file. This part deals with converting a file to DVD format, with or without an edl file. In many cases, the video you want to convert does not need any editing. In these cases, you can skip the instructions specific to the -edl option.

There is a utility for MPlayer called MEncoder, which can generally convert between any two formats that MPlayer is able to play. Technically, MPlayer and MEncoder are part of the same project, but depending on your Linux distro, may need to be installed in separate packages. Many of the command-line options for the mplayer command are also available for the mencoder command, including the -edl option that we used to playback a video with an edl file. When using this option, you may want to keep in mind the -hr-edl-seek option. This is due to a common method of video compression using what is sometimes referred to as keyframe boundaries.

As you might expect, it is possible to store each separate frame of a video inside a file. This is basically the same thing that reel-to-reel projectors do, playing back each frame of a show one-by-one. This is a lossless method, meaning that since each frame is stored exactly as is, there will be no loss in quality. Unfortunately, it also takes up considerably more resources. The MPEG standard takes a different approach. A single frame of video will be stored, but then rather than storing the entire following frame, only the changes between the frames are stored. This is often performed using a lossy method, meaning that there might be some slight loss of quality in the compressed video. Every few seconds, this progression of storing only changes to a frame is thrown away, and the video starts again with a new frame, and then stores only the subsequent changes. These are known as keyframe boundaries, and they should be kept in mind when editing video.

Because of these boundaries, most video editing software will re-encode video each time an edit is made to the original. Even if the same compression method is used as before, it will be applied a second time, which is likely to result in a loss of more quality. Some editing software provides the ability to cut at keyframe boundaries, resulting in a file that is bit by bit identical to the original, except for where content was removed. Since no re-encoding was involved, there will be no additional loss to the video quality.

The cool thing about the -hr-edl-seek option is that rather than skipping over the part of the video in the edl file, it tells mencoder to decode each invidual frame, and then re-encode only the ones that are actually needed. This allows mencoder to create an edited version of the video more precisely than usual. It does have a couple of caveats, however. First of all, it is slower. But personally, I don't mind the performance trade. I'm probably going to queue up my shows to re-encode overnight anyway. Second, if you decide to try and use it with '-ovc copy' (a method that only copies the video frame by frame, rather than decoding and re-encoding), it may not work as well, if at all. Your mileage may vary.

Once your edl file is set up, it's time to convert it into a type of MPEG2 file called a VOB, which is the format that DVD players use. If your video came from a TiVo box, there's a good chance it's already technically in MPEG2 format. If you recorded your show at "High Quality", it's probably at exactly the same quality as most DVDs anyway. (Disclaimer: If you use the TiVo service, it is probably a violation of your service agreement to strip the TiVo copy protection in order for the video to be used in this manner. If you decide to do that, you're on your own.) If your video came from another source, it will probably need to undergo some re-encoding.

What part of the world you live in will also come into play at this point. This has nothing to do with DVD regions, but in fact has to do with analog encoding methods. DVDs are digital, and should not technically be subject to analog restrictions. But American televisions are still designed to handle video differently than, say, British televisions, because of how video has been transmitted in those countries for decades. If you are in America (and some other parts of the world), you will probably want to optimize your shows for NTSC output. If you are in Britain (or some other parts of the world), you will probably want to go with PAL. It is also possible to set up your video for full-screen or wide-screen.

The following command line is my own personal command for making VOB files. It is shown here on multiple lines with backslashes, but I have it as a single line in a script, minus the backslashes:

mencoder -of mpeg -mpegopts format=dvd -srate 48000 -ofps 30000/1001 \
-ovc copy -oac copy -lavcopts vcodec=mpeg2video: \
vrc_buf_size=1835:keyint=18:vrc_maxrate=9800: \
vbitrate=4900:aspect=4/3:acodec=ac3:abitrate=192 \
"$INPUTFILE" -o "$OUTPUTFILE" -edl "$EDLFILE" \
-hr-edl-seek

Note: I use .mpeg2 for my output file extension, but what you choose for yours really doesn't matter. It's going to get renamed anyway.

My command was adapted from a series of commands available on the Gentoo wiki. I don't know what it is about Gentoo users, but they have managed to put together some excellent documentation on MPlayer and MEncoder, in many cases far superior to MPlayer and MEncoder's own documentation. Their original versions are as follows:

NTSC Widescreen:

mencoder -of mpeg -mpegopts format=dvd -srate 48000 -ofps 30000/1001\
-ovc lavc -oac lavc -lavcopts \
vcodec=mpeg2video:\
vrc_buf_size=1835:\
keyint=18:\
vrc_maxrate=9800:\
vbitrate=4900:\
aspect=16/9:\
acodec=ac3:abitrate=192 \
~/Videos/path/to/file-divx.avi -o ~/Videos/path/to/file-divx.mpeg2

NTSC Fullscreen:

mencoder -of mpeg -mpegopts format=dvd -srate 48000 -ofps 30000/1001\
-ovc lavc -oac lavc -lavcopts \
vcodec=mpeg2video:\
vrc_buf_size=1835:\
keyint=18:\
vrc_maxrate=9800:\
vbitrate=4900:\
aspect=4/3:\
acodec=ac3:abitrate=192 \
~/Videos/path/to/file-divx.avi -o ~/Videos/path/to/file-divx.mpeg2

PAL Widescreen

mencoder -of mpeg -mpegopts format=dvd -srate 48000 -ofps 25 \
-ovc lavc -oac lavc -lavcopts vcodec=mpeg2video:\
vrc_buf_size=1835:\
keyint=15:\
vrc_maxrate=9800:\
vbitrate=4900:\
aspect=16/9:\
acodec=ac3:abitrate=192 \
~/Videos/path/to/file-divx.avi -o ~/Videos/path/to/file-divx.mpeg2

PAL Fullscreen

mencoder -of mpeg -mpegopts format=dvd -srate 48000 -ofps 25 \
-ovc lavc -oac lavc -lavcopts vcodec=mpeg2video:\
vrc_buf_size=1835:\
keyint=15:\
vrc_maxrate=9800:\
vbitrate=4900:\
aspect=4/3:\
acodec=ac3:abitrate=192 \
~/Videos/path/to/file-divx.avi -o ~/Videos/path/to/file-divx.mpeg2

You'll notice that my version differs from the Gentoo version in a couple of ways. First of all, I make use of the aforementioned -edl and -hr-edl-seek options. Secondly, I switched my encoding method from the 'lavc' option to 'copy'. Since my files are typically encoded in a quality similar to the TiVo files that I mentioned, there is no need to re-encode the video; it's perfect as-is. I noticed that when I used the 'lavc' option, my video files ended up somewhere in the neighborhood of a third of the size of the originals (a quarter, if you count the commercials that were cut), and they suffered from a quality loss. Granted, I would have been able to fit far more video on a single disc like this, but I like my videos to be high-quality.

As far as the options go, well, I don't actually understand all of them. I just copied them from the Gentoo wiki. I do recognize a couple of them. The -ofps option specifies the frames per second. PAL uses 25, while NTSC uses 29.97. Yes, you read that right, NTSC has always used a decimal number of frames per second. As I understand it, it was "a brilliant technical hack that allows the color video signal to be shoehorned into a black and white signal without screwing up B&W TVs".

The -of option speficies the output format (use 'mencoder -of help' to see what's available for you), while -o specifies the output file. There isn't an actual flag for the input file, it just needs to appear somewhere in the command line. The -ovc and -oac options specify the output video and audio codecs, respectively. The rest of the options that you see are specific to DVDs and aspect ratios. I'm going to let you look them up. Personally, right now I'm happy that somebody did that part for me.

Now that you have your videos in the format that the DVD player is expecting, it's time to start authoring the disc itself. We'll get started with this in Part 3: Making a DVD Menu.

No comments:

Post a Comment

Comments for posts over 14 days are moderated

Note: Only a member of this blog may post a comment.