1. Introduction
MEncoder is a part of MPlayer package, a movie player for Linux that can play most MPEG, VOB, AVI, OGG/OGM, VIVO, ASF/WMA/WMV, QT/MOV/MP4, FLI, RM, NuppelVideo, yuv4mpeg, FILM, RoQ, PVA, Matroska files, supported by many native, XAnim, RealPlayer, and Win32 DLL codecs. MEncoder (MPlayer’s Movie Encoder) is a simple movie encoder, capable of encoding the wide range of fileformats and decoders of MPlayer, to all the codecs of FFmpeg’s libavcodec.
2. Encoding a DVD
2.1 MEncoder Command Line
MEncoder command line for DVD encoding has the following format:
mencoder dvd://title# [options].
The title# is number specifying DVD titles to encode, and with [options] parameters you can control many aspects of encoding process. Some options for DVD encoding are discussed later in this article.
2.2 Cropping and Scaling
Native DVD resolution is 720×480 for NTSC, and 720×576 for PAL, but there is an aspect flag that specifies whether it is full-screen (4:3) or wide-screen (16:9). Many wide-screen DVDs are not strictly 16:9, and will be either 1.85:1 or 2.35:1. This means that there will probably be some black border bands in the video that will need to be cropped out.
To crop a video, you can use -vf crop[=w:h:x:y] video filter, that crops the given part of the image and discards the rest. The w and h parameters specify cropped image width and height, and x and y parameters specify position of the cropped picture.
MPlayer provides a crop detection filter that can help you determine the crop rectangle. Run MPlayer with -vf cropdetect and it will print out the crop settings to remove the borders. You should let the movie run long enough that the whole picture area is used, in order to get accurate crop values. For example, running:
$ mplayer dvd://1 -vf cropdetect
will give output similar to this:
[CROP] Crop area: X: 2..719 Y: 68..505 (-vf crop=704:432:10:72).
[CROP] Crop area: X: 2..719 Y: 68..505 (-vf crop=704:432:10:72).
...
Because MPEG-4 uses 16×16 macroblocks, you will want to make sure that each dimension of the video you are encoding is a multiple of 16 or else you will be degrading quality. You can do this by rounding the width and height of the crop rectangle down to the nearest multiple of 16.
To scale the video image, you can use -vf scale=[w:h:[...]] video filter (for a complete list of scale filter parameters, consult the mencoder manual). Parameters w and h specify scaled width and height of an image.
You can test your video filters options before the actual encoding, by passing the to the mplayer for unencoded video playback. For example:
$ mplayer dvd://1 -vf crop=704:432:10:72,scale=352:216
2.3 Specifying Encoding Segment
If you want to encode only a section of the DVD, you can use -ss time and -endpos time|size mencoder options. The -ss option seeks to given time position, and -endpos option stops encoding after a given time or encoded byte size. For example:
-ss 01:00:00– start encoding one hour into the movie-endpos 60– encode only 60 seconds-endpos 100mb– encode only 100mb of movie-ss 05:00 -endpos 10:00– start 5 minutes into the movie and encode 10 minutes of video
2.4 Selecting Audio Channels and Subtitles
If your DVD has several audio channels and/or subtitles, and you would like to encode a non-default one, you will need to specify audio channel and subtitle to mencoder. To display informations about the DVD and available channels and subtitles, you can use mplayer with option -v.
$ mplayer dvd://1 -v
It will display many informations about a source DVD, an several lines of text about audio channels and subtitles, for example:
[open] audio stream: 0 audio format: ac3 (5.1) language: en aid: 128
[open] audio stream: 1 audio format: ac3 (stereo) language: en aid: 129
[open] audio stream: 2 audio format: ac3 (stereo) language: en aid: 130
...
[open] subtitle ( sid ): 0 language: en
[open] subtitle ( sid ): 1 language: is
[open] subtitle ( sid ): 2 language: pt
[open] subtitle ( sid ): 3 language: el
...
To select an audio channel, you can use either -aid id or -alang code mencoder option. The -aid option takes id number of the audio stream (128, 129, etc.), and -alang takes two letter language code (en, pt, el, etc.) as a parameter.
Selecting a subtitle is similar to selecting audio channel. Available options are -sid id or -slang code. The -sid option takes id number of the subtitle (0, 1, etc.), and -slang takes the two letter language code as a parameter (en, is, etc.).
-sid 128– selects audio channel with id 128-alang en -sid 2– selects English audio channel and subtitles with id 2
2.5 Encoding the Audio
To encode the audio, you need to specify the audio codec with -oac codec mencoder option. To get a list of available codecs you can use -oac help option. We will use lavc codec, to encode audio with libavcodec codec. To pass some additional information to lavc codec about type of codec and bitrate, we will use -lavcopts mencoder option. That option has several parameters, acodec for specifying codec and abitrate for specifying audio bitrate in kbps. To specify file name of encoded file, there is an -o file option. To encode only audio, we will use frameno video codec, that can be used for audio only encoding. For example:
$ mencoder dvd://1 -ovc frameno -oac lavc -alang en -lavcopts acodec=mp3:abitrate=128 -o frameno.avi
-ovc frameno– sets output video codec to frameno, so that video will not be encoded-oac lavc– sets output audio codec to libavcodec-alang en– sets the audio language to english-lavcopts acodec=mp3:abitrate=128– sets libavcodec audio codec to mp3, and audio bitrate to 128 kbps-o frameno.avi– sets output file name to frameno.avi
Note: Do not change the name of frameno.avi output file, because mencoder will use it later when video will be encoded.
When audio encoding is finished, mencoder will output recommended video bitrates for encoding to various sizes. Take a note about bitrate, as it will be used in next step of encoding the video. Output will be similar to something like this:
Recommended video bitrate for 650MB CD: 553
Recommended video bitrate for 700MB CD: 606
Recommended video bitrate for 800MB CD: 713
Recommended video bitrate for 2 x 650MB CD: 1248
Recommended video bitrate for 2 x 700MB CD: 1355
Recommended video bitrate for 2 x 800MB CD: 1569
2.6 Encoding the Video
For the video encoding we will use libavcodec, because it is superior to DivX 4 or 5 and at least as good as XviD. For a playback in Windows you will need DivX 5 codec installed. To gain a better quality of encoding, we will do a two-pass encoding.
To specify the output video codec, we will use -ovc codec option (in this case we will use lavc as codec), and -lavcopts to specify encoding parameters. Command for the first pass:
$ mencoder dvd://1 -ovc lavc -lavcopts vcodec=mpeg4:vbitrate=1250:vhq:autoaspect:vpass=1 -oac copy -vf crop=704:432:10:72 -o /dev/null
-ovc lavc– sets output video codec to lavc-lavcopts vcodec=mpeg4:vbitrate=1250:vhq:autoaspect:vpass=1– sets mpeg4 encoding, bitrate to 610, high quality encoding, automatically set aspect ratio, and encoding pass one-oac copy– set output audio codec to copy, so that mencoder will use previously encoded sound in frameno.avi file-vf crop=704:432:10:72- sets video filtering options, previously discussed here-o /dev/null– sets output file to be /dev/null, since we don’t need actual first pass encoding, only a log file that mencoder will create for second pass encoding process
For the second pass encoding, the command is similar to the previous command:
$ mencoder dvd://1 -ovc lavc -lavcopts vcodec=mpeg4:vbitrate=1250:vhq:vpass=2:autoaspect -oac copy -vf crop=704:432:10:72 -o movie.avi
-lavcopts vcodec=mpeg4:vbitrate=1250:vhq:vpass=2:autoaspect- options are same as in previous command, only vpass parameter is set to 2-o movie.avi– sets the file name for output, that will be the final encoded movie
Note: You can add other options for scaling, subtitles, and partial encoding, discussed earlier in the article, and use values for bitrate that you get while encoding audio, if you want a specific size of final encoded movie.
3. Conclusion
Well, these are basic instructions for encoding DVD to AVI format. There are many other options available, so if you want even more control over the process, read a manual for MPlayer, and use this article as a guideline. Comments are welcome.
Very nice article. Thank you!
Is there a way to extract both audio streams? or extract just the second audio with – sid and then combine it to one avi?
Thanks!