Converting video to perfectly compliant DVDs using MPlayer/MEncoder - part 2

by Michele Alessandrini, in November 2010 (article #396)

You may want to convert a video from a variety of formats to DVD, to be able to watch it on a stand-alone, DVD-only player. DVDs use a quite rigid format. Many user-friendly programs allow you to convert video to DVD format very easily and without having to know video-related aspects. But almost always, the results I got were not 100% DVD-compliant, and, while they could be correctly played in more sophisticated players, they led to bad surprises when moved to cheaper or older devices. It took me much time to correctly tune all the parameters, and I'd like to share this little bit of knowledge with people having the same problems. The solutions I propose here use the MPlayer/MEncoder pair of programs. In addition, some important concepts related to digital video are discussed.fetishroom.net

[toc maxlevel:4]

Introduction

In the first part of this article, we have seen many important concepts in the field of digital video, and the special cases concerning a video DVD. In this second part, those concepts will be put to work, by analyzing the MEncoder command-line syntax and by considering a few common practical examples.

MEncoder invocation

The following is an example of a MEncoder invocation, just to be able to explain some of the options. Much of it has been inspired by the documentation pages listed in the references, with subsequent improvements by me, based on my experience.

All the options are fully documented in the official MPlayer/MEncoder documentation, which you are encouraged to look at if you want to understand some of them in more depth.

Here is the example invocation, the "\" character at the end of every line means, in shell syntax, that the command is continued in the subsequent line, it is shown in this way to avoid excessively long lines. The actual shell commands are normally on a single line, in this case remove the "\" character.

mencoder inputFile.avi -noaspect -noautoexpand -nosub \
-o outputFile.mpg -of mpeg -ofps 25 -srate 48000 \
-ovc lavc -oac lavc \
-vf scale=720:576,harddup \
-af lavcresample=48000,channels=2 \
-lavcopts acodec=ac3:abitrate=192:\
vcodec=mpeg2video:vbitrate=8000:\
mbd=2:trell:\
vrc_maxrate=9000:vrc_buf_size=1800:\
keyint=15:vstrict=0:\
aspect=4/3:vpass=1:turbo \
-mpegopts format=dvd:tsaf:vaspect=4/3

The syntax is as follows:

  • "inputFile.avi" is the name of the file to be converted. Its name can be at any position in the command line.
  • "-noaspect" and "-noautoexpand" tell MEncoder to not apply the aspect ratio auto-adjustment, as MPlayer would do, otherwise the initial size of the video would be modified and the following adjustments would be wrong.
  • "-nosub" is to disable subtitles, if present, so they are not shown in resulting video (at your option).
  • "-o outputFile.mpg" is the name of the output file.
  • "-of mpeg" means we want the output format to be MPEG (as opposed to the default AVI).
  • "-ofps 25" means that the output file must have 25 fps. MEncoder will convert it if it's different.
  • "-srate 48000" means that the output file's audio will have a bit rate of 48000 samples per second, as resulting from the audio filters we will apply.
  • "-ovc lavc" and "-oac lavc" are the codecs used for video (-ovc, output video codec) and for audio (-oac, output audio codec). Here we're using the same codec "lavc", that is the "libavcodec", a library contained in MEncoder that can produce a variety of audio and video formats, among which the MPEG-2 for video and the AC-3 for audio. These are the formats we want for our video DVD, so that's why we're using the same library for both audio and video.
  • "-vf" is the option listing the video filters to be applied to the video before the conversion. The syntax is as follows: several filters can be listed, separated by comma. Every filter has in turn the syntax: NAME=PARAM:PARAM:..., that is the name of the filter and, if it has parameters, a "=" followed by its parameters separated by ":". In the reported example, there are two filters: the first one is the "scale" filter responsible of resizing the video. Its parameters are 720 and 576, that is the final width and height. The last one is the "harddup" filter (with no parameters). It is necessary for MPEG output and must be the last one. It tells the encoder to actually store a frame when it's duplicated, for example when adjusting the frame rate to a higher value. Otherwise, the encoder would simply put a flag to indicate that the frame has to be repeated, not storing the second copy of the frame, but this is not supported by the MPEG container, resulting in a wrong frame rate. We will see other filters in the examples below.
  • "-af" is the option listing the audio filters, in similar manner to "-vf" for video. The syntax is the same as "-vf". Here we apply two filters: a resample to 48 kHz and a conversion to 2 channels. This way we always have the desired format for the audio of the final DVD.
  • "-lavcopts" lists the options for the encoder, that is the crucial video and audio parameters to be applied to the conversion process. As the name says, these are the options for the "lavc" encoder, which we choose to use for the video and audio conversion. Because we use the same encoder for video and audio, this option lists the parameters for both audio and video. Here is an explanation of the parameters used, the syntax is the same as filters:
    • "acodec": audio codec to be used ("ac3").
    • "abitrate": audio bitrate in kbps (192).
    • "vcodec": video codec to be used ("mpeg2video").
    • "vbitrate": video bit rate in kbps (8000 kbps in this example, or 8 Mbps). For a 2-pass conversion, as in our case, the bit rate is variable and this is the wanted average bit rate. More on how to choose a bit rate will be explained below.
    • "mbd=2" and "trell" are options related to increasing the video quality.
    • "vrc_maxrate=9000" and "vrc_buf_size=1800" are related to the DVD constraints of maximum bit rate and maximum memory buffer size. We use slightly reduced values with respect to the theoretical limits because I noticed some rare problems if they're exactly the maximum allowed.
    • "keyint=15" means the maximum interval between two key frames, that is frames that are stored entirely, and not only as differences between consecutive frames. It seems it must be 15 for video DVDs.
    • "vstrict=0" is needed for DVD compatibility.
    • "aspect=4/3" sets the aspect ratio information for the player (4/3 in this case, can be 16/9 too).
    • "vpass=1" and "turbo" are related to the 2-pass compression method already explained. In this case, this is the first pass, where the encoder computes the quantity of information associated with each frame. "turbo" is to make the first pass sensibly faster, with minor quality loss. For the second pass, where the compressed video is actually created, the whole command line would be repeated replacing "vpass=1" with "vpass=2" and omitting "turbo", which has no effect on the second pass. Note: at the end of the first pass, you obtain an MPEG file that is already a well-formed file, and you can play it with MPlayer. But that file does not respect the DVD constraints, so do not use it as the final product.
  • "-mpegopts" dictates the options for the output file container, that we chose to be MPEG with a previous option. In this case, the output format must be DVD-compatible, with an extra option "tsaf" to enhance timing information. The aspect ratio is stored again, this time in the MPEG container.

Choosing a bit rate for the video

How to choose the appropriate bit rate for your final video? The answer depends on how much video you want to store on a single DVD, and how much quality you want to achieve.

A quick trick is telling MEncoder to use a very high bit rate, like the 8000 kbps (8 Mbps) of the previous example. Remember that the maximum theoretical limit for a DVD is 9 Mbps. If the maximum average bit rate needed by MEncoder is less than that, it will automatically use a minor value, and we don't have to worry to compute it (you will see the actual bit rate used at the end of conversion).

This is very often the case when the original video is already compressed, and so the information contained in it is already reduced. By letting MEncoder use the maximum bit rate needed, you are sure you are keeping the most of the original quality in the final video. Even if it happens that MEncoder must use the full bit rate, and so the resulting file will be quite big, the DVD capacity is not really a problem if you have, for example, the equivalent duration of a single movie.

On the other hand, you may want a stricter control over the final bit rate. This is true, for example, if you want to record more videos on a single DVD, or have a very long video, or you acquired an analog video via an acquisition card, in which case the video may contain a certain amount of video disturbances (called "noise", not in the sense of sound) that makes the needed bit rate grow sensibly.

Fundamentally, you can choose the bit rate you want and, unless you use very small values, chances are that the resulting quality will be perfectly satisfactory. The key point is how to mathematically correlate the bit rate, the video duration and the resulting file size. Let's explain that with some formulas.

First of all, the capacity of a standard single-layer DVD is 4,7 GB (4,7 GigaBytes). Here "Giga" means "billions" in the decimal sense (not in binary notation), and so it means 4,700,000,000 bytes.

To compute the size of the final video, we have to sum the audio and video bitrate, that are expressed in bits per second, divide it by 8 to obtain the bytes per second (remember that a byte is 8 bits), and finally multiply it for the number of seconds of the video, to obtain the total file occupation in bytes. This is expressed by the following formula:

  [ video bit rate ]   [ audio bit rate ]         [ video duration ]   [ file size ]
( |                | + |                | ) / 8 * |                | = |           |
  [  in bps        ]   [  in bps        ]         [  in seconds    ]   [ in bytes  ]

For example, if the video bit rate is 4 Mbps, its numeric value would be 4,000,000. The audio bit rate, according to the default settings we are using, is always 192,000.

You may want to invert the formula to compute the maximum bit rate, knowing the final file size you want. Or you can put the formula in a spreadsheet, and adjust the bit rate iteratively. These exercises are left to the reader. Pay attention to not make a final result of exactly 4.7 GB, leave a little margin for the MPEG container, the DVD file system and the usual mistakes.

Practical examples

Let's see some practical examples. In all cases, the steps to be executed are the following ones:

  1. Adjusting the aspect ratio, if needed, by setting the "crop" filter to cut some amount of video at left and right, or the "expand" filter to add black bands at the top and the bottom.
  2. Adjusting the resolution, if needed, by setting the "scale" filter to resize the video to a valid DVD resolution.
  3. Setting the right parameters for the actual conversion.

Example 1: DivX video at 640x352

Let's consider the example we used in the first part of the article when talking about the relation between aspect ratio and resolution. Video parameters were the following:

  • Resolution: 640x352.
  • Aspect ratio: 1.82 (in the example we concluded that the aspect ratio was the same as W/H, that is, the pixels were squared).
  • Frame rate: 25 fps.
  • Interlacing: no.

We previously noted that the aspect ratio was slightly bigger than 16/9 (1.78). To convert it to 16/9, we decide to crop some video at the edges so that the aspect ratio will be 16/9. With a height of 352 pixels, the width we want in order to be 16/9 is 352 * 16 / 9 = 626. So we have to crop the video to that size, and this is accomplished by using the filter "crop=626:352".

After the aspect adjustment the video must be resized to a valid resolution. Let's choose 720x576, the maximum available one, to not lose any details. The filter to be used is "scale=720:576".

About the bit rate, let's use the method explained above of choosing a very high bit rate and let MEncoder use the maximum required.

This example video does not require any more adjustments, so the command line given before for the first pass will be modified as follows:

mencoder inputFile.avi -noaspect -noautoexpand -nosub \
-o outputFile.mpg -of mpeg -ofps 25 -srate 48000 \
-ovc lavc -oac lavc \
-vf crop=626:352,scale=720:576,harddup \
-af lavcresample=48000,channels=2 \
-lavcopts acodec=ac3:abitrate=192:\
vcodec=mpeg2video:vbitrate=8000:\
mbd=2:trell:\
vrc_maxrate=9000:vrc_buf_size=1800:\
keyint=15:vstrict=0:\
aspect=16/9:vpass=1:turbo \
-mpegopts format=dvd:tsaf:vaspect=16/9

In comparison with the previous example, we changed the "-vf" filter section (note the filter order, it's crucial) and the aspect ratio, present in two places, by changing 4/3 to 16/9.

After the first pass, you must execute the second pass too, with the same command line, by only changing
"vpass=1" to "vpass=2" and removing the "turbo" parameter.

Note that in all the examples we will see, the audio conversion might have been omitted in the first pass, but I'll leave the same command line for clarity. Moreover, the time needed for the audio conversion is very short compared to the time used for video.

Example 2: analog video (TV, VHS, etc) acquired from an acquisition card

In this second example we will consider a video acquired from analog TV or VHS. By its nature, this video is always interlaced, and has an aspect ratio of 4/3.

The process of acquisition is different from card to card, and depending on the program you use. Often the acquisition card allows you to get video at different resolutions, by applying an hardware scaler. The program too may give you different options about the saved file resolution.

I suggest to acquire the video at the maximum resolution if you want to keep the maximum detail, that is 720x576. Another important option is in which format to save the video. The data may come uncompressed or nearly uncompressed from the acquisition card (maybe compressed through an hardware encoder), and the program almost certainly must convert it to a more compressed format on the fly, to be able to save it to disk. In fact, saving it uncompressed or slightly compressed is almost certainly too heavy-duty for the PC and the various hardware involved (the card, the motherboard bus, the disk).

Of course the less the video is compressed, the more the original quality is preserved. You should choose a format which is a trade-off between compression (that is, bit rate) and CPU load. As a rule of thumb, check the CPU occupation during the acquisition and be sure it is not near the 100%. You may also want to stop all the processes that might wake up in unexpected moments and use the CPU. After the acquisition, check carefully the saved video to be sure it's not corrupted in any place. If your computer is too slow, you can reduce its load by acquiring at a minor resolution (like 352x288). In this case, check if the video is still interlaced.

As a side note, MEncoder can be used for the acquisition task too, but the variety of available cards and of compression options makes this topic beyond our scope. For the braver ones, the lavc codec also offers some lossless video compression formats.

Note that the analog video, by its nature, always contains some amount of "noise", that is video disturbances, more or less depending on video quality, that makes the video require a bigger bit rate than an "ideal" video (the noise is quite random data, so it's hardly compressed). So be prepared to use a high bit rate. If you use the trick of giving a very high bit rate, chances are that it will be all used. Use the formulas to compute the maximum bit rate allowed if you have space constraints.

So, summarizing, we have the following features:

  • Resolution: 720x576.
  • Aspect ratio: 4/3.
  • Frame rate: 25 fps.
  • Interlacing: yes.

We don't need any filters (apart from "harddup"), because the aspect ratio and the resolution are already ok. What we need is adding the compression options regarding the interlaced video. This is accomplished by adding the "ildct" and "ilme" parameters to the "-lavcopts" option:

mencoder inputFile.avi -noaspect -noautoexpand -nosub \
-o outputFile.mpg -of mpeg -ofps 25 -srate 48000 \
-ovc lavc -oac lavc \
-vf harddup \
-af lavcresample=48000,channels=2 \
-lavcopts acodec=ac3:abitrate=192:\
vcodec=mpeg2video:vbitrate=8000:\
mbd=2:trell:\
vrc_maxrate=9000:vrc_buf_size=1800:\
keyint=15:vstrict=0:\
aspect=4/3:ildct:ilme:vpass=1:turbo \
-mpegopts format=dvd:tsaf:vaspect=4/3

As stated in the first example, this is the first pass only. After the first pass, you must execute the second pass too, with the same command line, by only changing "vpass=1" to "vpass=2" and removing the "turbo" parameter.

Example 3: DV video from a digital video-camera

As described in the first part of this article, DV video is the format used by the many digital video cameras using a tape cassette. The video is generally downloaded to the PC using the FireWire interface or sometimes USB, and the saved video has usually the "AVI" extension. The process of transferring the video via FireWire is simply a data transfer, and not a video conversion, so the same quality as in the camera is preserved. In other words, the resulting video is the same as that stored on the tape. On GNU/Linux systems, the video download can be accomplished with the "dvgrab" program, but the kernel must have the appropriate FireWire support (the last time I checked, dvgrab required the older FireWire subsystem, while newer kernels use the new one by default). On Windows, the Movie Maker program can be used, by choosing the "DV" format for the output.

As is the case for analog video, DV video is interlaced, but with a subtle difference: the field order is inverted with respect to analog television video. So, before converting it to MPEG, we need to use a special MEncoder filter to delay the fields by a semi-frame (the filter is called "phase" and we need the "b" option). Without that, the video watched on the television would be flickering. About the aspect ratio, it can be 4/3 or 16/9, it depends on the camera. The resolution is, again, 720x576.

Even if the format of the video is digital, it may contain some amount of noise, as described in the example 2. This is because cheap video-cameras are more sensible to factors like lighting and the like. So the same considerations about bit rate hold here.

So, summarizing, we have the following features:

  • Resolution: 720x576.
  • Aspect ratio: 4/3 or 16/9 (let's assume 16/9).
  • Frame rate: 25 fps.
  • Interlacing: yes (with inverted fields).

As said, we need to use the "phase=b" filter, and no other filters apart from "harddup". Moreover, we need to add the compression options regarding the interlaced video, like in the previous example. This is accomplished by adding the "ildct" and "ilme" parameters to the "-lavcopts" option:

mencoder inputFile.avi -noaspect -noautoexpand -nosub \
-o outputFile.mpg -of mpeg -ofps 25 -srate 48000 \
-ovc lavc -oac lavc \
-vf phase=b,harddup \
-af lavcresample=48000,channels=2 \
-lavcopts acodec=ac3:abitrate=192:\
vcodec=mpeg2video:vbitrate=8000:\
mbd=2:trell:\
vrc_maxrate=9000:vrc_buf_size=1800:\
keyint=15:vstrict=0:\
aspect=16/9:ildct:ilme:vpass=1:turbo \
-mpegopts format=dvd:tsaf:vaspect=16/9

As stated in the first example, this is the first pass only. After the first pass, you must execute the second pass too, with the same command line, by only changing "vpass=1" to "vpass=2" and removing the "turbo" parameter.

A script to automate the process

To avoid calling two different commands for the first and second conversion passes, I made a simple Bash script that calls MEncoder for the two passes, by using the same parameters and changing the pass-related options only. Here it is:

#!/bin/bash

if [ $# -ne 4 ] ; then
	echo "Usage: $0  inputFile  outputFile  bitRate  aspectRatio"
	exit 1
fi

function encodePass {
mencoder -noaspect -noautoexpand -nosub \
-o "$2" -of mpeg -srate 48000 \
-vf harddup -af lavcresample=48000,channels=2 -ovc lavc -oac lavc \
-lavcopts acodec=ac3:abitrate=192:vcodec=mpeg2video:vbitrate=$3:\
mbd=2:trell:vrc_maxrate=9000:vrc_buf_size=1800:keyint=15:vstrict=0:\
aspect=$4:threads=2:$PASS_OPT \
-mpegopts format=dvd:tsaf:vaspect=$4 \
-ofps 25 "$1"
}

PASS_OPT="vpass=1:turbo"
encodePass "$1" "$2" "$3" "$4"
PASS_OPT="vpass=2"
encodePass "$1" "$2" "$3" "$4"

It's quite flexible, you have to pass the input file, the output file, the bit rate in kbps and the aspect ratio as options, for example:

./encodedvd  input.avi  output.mpg  8000  16/9

Note, however, that it does not specify additional filters and interlacing options, you have to modify it according to the previous examples.

Note also that I added the "threads=2" parameter to the "-lavcopts" option. This is to make the encoder use multiple parallel threads (2 in this case) to take advantage of modern multi-core CPUs, and so to speed-up the conversion process. This, at least, theoretically, because I noticed that the library cannot parallelize so much, and so using 2 threads does not result in 200% processor use, but less. This is even worse when using 4 or more threads. Anyway, the speed improvement is still sensible, provided you really have multiple cores.

The conversion process

So, you are finally ready to launch the conversion. As a last step, consider some hints to improve your experience:

  • Use multiple threads, as said in the previous section, if you have multiple CPUs. The improvement is still sensible even if you have a single CPU that simulates 2 cores via Hyper-Threading.
  • Consider launching the script with "nice" prepended, to keep your computer responding well.
  • Be sure that your computer does not go stand-by automatically after some time of user's inactivity.

That said, launch the script and be prepared to wait a long time, because the conversion is very slow, even more than the actual video duration if your computer is not so fast. You may want to consider appending the shutdown command to the conversion script, to make the computer turn itself off after the conversion if you leave it unattended.

During the process, MEncoder prints quite a lot of information that is useful to monitor if the conversion is going well. There may be occasional error messages about duplicated or discarded frames. This is ok, unless they are too frequent, they indicate that MEncoder is making continuous efforts to keep the audio and the video in sync. Those messages, on the other hand, are frequent and regular if MEncoder must convert the frame rate, that's precisely the way it behaves to increase or decrease it.

While MEncoder produces the output video, you can watch it by invoking MPlayer in another shell, so you can immediately verify that the size, the aspect ratio and the like are ok, before waiting hours uselessly. This is because the MPEG container can be watched even if it's truncated, and because, as we said, the first pass still produces a valid video. But we also said that this video is not DVD-compliant, so don't be tempted to use the first pass as the final result!

If all goes well, after the second pass MEncoder prints the bit rate used for audio and video. The latter will be exactly the value you specified, or less if it was not all necessary.

About the final video, quickly watch it with MPlayer again, and be sure that the audio and video are in sync, especially towards the end. This is the most common error that can arise if the conversion encounters some problems.

And now? You think you're finished, right? No! The MPEG file is not ready to go on a DVD, you have to create the special file system and video structure dictated by the DVD standard. This is called "authoring the DVD". This is the last step, and it's the subject of the next chapter.

Authoring the final DVD

A DVD has a file system with a special structure. You can see it with a file manager: there is a "VIDEO_TS" directory, and inside a group of files with "VOB", "IFO" and other extensions. These files carry the video (split in several files) and the information about the chapters, the menus, etc. This special arrangement is to simplify the work of the modest hardware the players were to be built from, when the DVD was introduced.

The tool will we use to produce the right structure is "dvdauthor" (see the references in the first part of the article). This tool can produce a full DVD with menus, buttons, and the like, but these are quite intricate, so we will use it to simply store video on the DVD: you press "play" on your remote, the video starts. We will make chapters, however, so you can jump to the various video parts by pressing the "next" and "previous" buttons every player has.

There are many ways to use this tool. The way I use is writing a little XML file with the instructions for the program. So create a file with ".xml" extension, for example "mydvd.xml", open it with a text editor (not with a browser like your computer will likely do), and paste the following content:

<dvdauthor>
   <vmgm />
   <titleset>
      <titles>
         <video format="pal" />
         <audio lang="en" />
         <pgc>
            <vob file="myvideo.mpg" />
         </pgc>
      </titles>
   </titleset>
</dvdauthor>

Don't worry about the meanings of all tags. You can see that I set the video format to PAL, and I set the audio language to "en" (not so useful here because we have one language only). The important line is the "vob" tag, inside the "pgc" tag, that's where you have to put the actual file name of your video.

You can put more than one "vob" line inside the "pgc" tag, so if you have more than one video file, the final video will be the concatenation of all the files, with no pause between, like if it was one single video. Doing so, a chapter is produced for every file, so you can easily concatenate multiple videos and skip between them with the chapter buttons.

If, on the other hand, you want to add chapters inside a single MPEG file, you can add the "chapters" attribute to the "vob" tag; for example if you want to add chapters at 10', 20' and 30' you can write like this:

<vob file="myvideo.mpg" chapters="10:00,20:00,30:00" />

When the file is ready, go to the directory where your video is and execute "dvdauthor" with the following command line:

dvdauthor -o dvd -x mydvd.xml

This will create a "dvd" directory with the infamous DVD file system. Check the program output to be sure that it parsed the file correctly.

The (really) last step now is burning these files to the actual DVD. You can use your favourite burning program: be sure to select DVD video as the kind of project, and give it the files you just produced. Alternatively, you can again use a simple command line invocation to burn your dvd, by calling the "growisofs" program (make sure it's installed): insert an empty DVD, go to the "dvd" directory you just created, and type:

growisofs -Z /dev/dvd -dvd-video -dvd-compat .

(note the dot as last argument). That's all! After a while, the real DVD should now be hot in your hands.

Conclusion

I really hope that this tutorial will be useful to someone. I also hope that, after all the work, you are now able to watch your video in your old dvd player.

For every suggestion, fix, question, etc, don't hesitate to contact me, through the comment page or e-mail.

I would like to thank all the people that made this possible: the author of the various pieces of software, the countless people that posted documentation and hints on the Internet, and my friends and colleagues (like Giorgio Biagetti) that gave me a lot of insights on this topic.

About the author

Photo of the Author

Michele Alessandrini, 1974, Italian, is an electronic engineer with a passion for programming. He's been using GNU/Linux systems for about 10 years.

<m.alessandrini**(at)**teletu.it>