7. Encoding with MEncoder

For the complete list of available MEncoder options and examples, please see the man page. For a series of hands-on examples and detailed guides on using several encoding parameters, read the encoding-tips that were collected from several mailing list threads on mplayer-users. Search the archives for a wealth of discussions about all aspects of and problems related to encoding with MEncoder.

7.1 Encoding 2 or 3-pass MPEG-4 ("DivX")

2-pass encoding: the name comes from the fact that this method encodes the file twice. The first encoding (dubbed pass) creates some temporary files (*.log) with a size of few megabytes, do not delete them yet (you can delete the AVI). In the second pass, the 2-pass output file is created, using the bitrate data from the temporary files. The resulting file will have much better image quality. If this is the first time you heard about this, you should consult some guides available on the Net.

This example shows how to encode a DVD to a 2-pass MPEG-4 ("DIVX") AVI. Just two commands are needed:
    rm frameno.avi - remove this file, which can come from a previous 3-pass encoding (it interferes with current one)
    mencoder dvd://2 -ovc lavc -lavcopts vcodec=mpeg4:vpass=1 -oac copy -o movie.avi
    mencoder dvd://2 -ovc lavc -lavcopts vcodec=mpeg4:vpass=2 -oac copy -o movie.avi

3-pass encoding: this is an extension of 2-pass encoding, where the audio encoding takes place in a separate pass. This method enables estimation of recommended video bitrate in order to fit on a CD. Also, the audio is encoded only once, unlike in 2-pass mode. The schematics:

  1. Remove conflicting temporary file:

    rm frameno.avi

  2. First pass:

    mencoder <file/DVD> -ovc frameno -oac mp3lame -lameopts vbr=3 -o frameno.avi

    An audio-only avi file will be created, containing only the requested audio stream. Don't forget -lameopts, if you need to set it. If you were encoding a long movie, MEncoder prints the recommended bitrate values for 650Mb, 700Mb, and 800Mb destination sizes, after this pass finishes.

  3. Second pass:

    mencoder <file/DVD> -oac copy -ovc lavc -lavcopts vcodec=mpeg4:vpass=1:vbitrate=<bitrate>

    This is the first pass of video encoding. Optionally specify the video bitrate MEncoder printed at the end of the previous pass.

  4. Third pass:

    mencoder <file/DVD> -oac copy -ovc lavc -lavcopts vcodec=mpeg4:vpass=2:vbitrate=<bitrate>

    This is the second pass of video encoding. Specify the same bitrate as in the previous pass unless you really know what you are doing. In this pass, audio from frameno.avi will be inserted into the destination file.. and it's all ready!

Example for 3-pass encoding:

    rm frameno.avi - remove this file, which can come from a previous 3-pass encoding (it interferes with current one)
    mencoder dvd://2 -ovc frameno -o frameno.avi -oac mp3lame -lameopts vbr=3
    mencoder dvd://2 -ovc lavc -lavcopts vcodec=mpeg4:vpass=1 -oac copy -o movie.avi
    mencoder dvd://2 -ovc lavc -lavcopts vcodec=mpeg4:vpass=2 -oac copy -o movie.avi

7.2 Encoding to MPEG format

MEncoder can create MPEG (MPEG-PS) format output files. It's probably useful only with libavcodec's mpeg1video codec, because players - except MPlayer - expect MPEG1 video, and MPEG1 layer 2 (MP2) audio streams in MPEG files.

This feature is not very useful right now, aside that it probably has many bugs, but the more importantly because MEncoder currently cannot encode MPEG1 layer 2 (MP2) audio, which all other players expect in MPEG files.

To change MEncoder's output file format, use the -of mpeg option.

Example:
  mencoder -of mpeg -ovc lavc -lavcopts vcodec=mpeg1video -oac copy <other options> media.avi -o output.mpg

7.3 Rescaling movies

Often the need to resize movie images' size emerges. Its reasons can be many: decreasing file size, network bandwidth, etc. Most people even do rescaling when converting DVDs or SVCDs to DivX AVI. This is bad. Instead of even you doing so, read the Preserving aspect ratio section.

The scaling process is handled by the 'scale' video filter: -vf scale=width:height. Its quality can be set with the -sws option. If it's not specified, MEncoder will use 0: fast bilinear.

Usage:

    mencoder input.mpg -ovc lavc -lavcopts vcodec=mpeg4 -vf scale=640:480 -oac copy -o output.avi

7.4 Stream copying

MEncoder can handle input streams in two ways: encode or copy them. This section is about copying.

7.5 Fixing AVIs with broken index or interleaving

Easiest thing. We simply copy the video and audio streams, and MEncoder generates the index. Of course this cannot fix possible bugs in the video and/or audio streams. It also fixes files with broken interleaving, thus the -ni option won't be needed for them anymore.

Command: mencoder -idx input.avi -ovc copy -oac copy -o output.avi

7.5.1 Appending multiple AVI files

As a side-effect, the broken AVI fixer function enables MEncoder to append 2 (or more) AVI files:

Command: cat 1.avi 2.avi | mencoder -noidx -ovc copy -oac copy -o output.avi -

Note: This expects 1.avi and 2.avi to use the same codecs, resolution, stream rate etc, and at least 1.avi must not be broken. You may need to fix your input AVI files first, as described above.

7.6 Encoding with the libavcodec codec family

libavcodec provides simple encoding to a lot of interesting video and audio formats (currently its audio codecs are unsupported). You can encode to the following codecs:

The first column contains the codec names that should be passed after the vcodec config, like: -lavcopts vcodec=msmpeg4

An example, with MJPEG compression:
    mencoder dvd://2 -o title2.avi -ovc lavc -lavcopts vcodec=mjpeg -oac copy

7.7 Encoding from multiple input image files (JPEGs, PNGs or TGAs)

MEncoder is capable of creating movies from one or more JPEG, PNG or TGA files. With simple framecopy it can create MJPEG (Motion JPEG), MPNG (Motion PNG) or MTGA (Motion TGA) files.

Explanation of the process:
  1. MEncoder decodes the input image(s) with libjpeg (when decoding PNGs, it will use libpng).
  2. MEncoder then feeds the decoded image to the chosen video compressor (DivX4, XviD, ffmpeg msmpeg4, etc...).

Examples

The explanation of the -mf option can be found below in the man page.

Creating a DivX4 file from all the JPEG files in the current dir:
  mencoder -mf on:w=800:h=600:fps=25 -ovc divx4 -o output.avi \*.jpg

Creating a DivX4 file from some JPEG files in the current dir:
  mencoder -mf on:w=800:h=600:fps=25 -ovc divx4 -o output.avi frame001.jpg,frame002.jpg

Creating a Motion JPEG (MJPEG) file from all the JPEG files in the current dir:
  mencoder -mf on:w=800:h=600:fps=25 -ovc copy -o output.avi \*.jpg

Creating an uncompressed file from all the PNG files in the current dir:
  mencoder -mf on:w=800:h=600:fps=25:type=png -ovc rawrgb -o output.avi \*.png

Note: Width must be integer multiple of 4, it's a limitation of the RAW RGB AVI format.

Creating a Motion PNG (MPNG) file from all the PNG files in the current dir:
  mencoder -mf on:w=800:h=600:fps=25:type=png -ovc copy -o output.avi \*.png

Creating a Motion TGA (MTGA) file from all the TGA files in the current dir:
  mencoder -mf on:w=800:h=600:fps=25:type=tga -ovc copy -o output.avi \*.tga

7.8 Extracting DVD subtitles to a Vobsub file

MEncoder is capable of extracting subtitles from a DVD into Vobsub fomat files. They consist of a pair of files ending in .idx and .sub and are usually packaged in a single .rar archive. MPlayer can play these with the -vobsub and -vobsubid options.

You specify the basename (i.e without the .idx or .sub extension) of the output files with -vobsubout and the index for this subtitle in the resulting files with -vobsuboutindex.

If the input is not from a DVD you should use -ifo to indicate the .ifo file needed to construct the resulting .idx file.

If the input is not from a DVD and you do not have the .ifo file you will need to use the -vobsubid option to let it know what language id to put in the .idx file.

Each run will append the running subtitle if the .idx and .sub files already exist. So you should remove any before starting.

Examples

Copying two subtitles from a DVD while doing 3-pass encoding
  rm subtitles.idx subtitles.sub
  mencoder dvd://1 -vobsubout subtitles -vobsuboutindex 0 -sid 2 -o frameno.avi -ovc frameno -oac mp3lame -lameopts vbr=3
  mencoder dvd://1 -oac copy -ovc divx4 -divx4opts pass=1
  mencoder dvd://1 -oac copy -ovc divx4 -divx4opts pass=2 -vobsubout subtitles -vobsuboutindex 1 -sid 5

Copying a french subtitle from an MPEG file
  rm subtitles.idx subtitles.sub
  mencoder movie.mpg -ifo movie.ifo -vobsubout subtitles -vobsuboutindex 0 -vobsuboutid fr -sid 1

7.9 Preserving aspect ratio

DVDs and SVCDs (i.e. MPEG1/2) files contain an aspect ratio value, which describes how the player should scale the video stream, so humans won't have egg heads (ex.: 480x480 + 4:3 = 640x480). However when encoding to AVI (DivX) files, you have be aware that AVI headers don't store this value. Rescaling the movie is disgusting and time consuming, there has to be a better way!

There is.

MPEG4 has an unique feature: the video stream can contain its needed aspect ratio. Yes, just like MPEG1/2 files (DVD, SVCD). Regretfully, there are no video players outside which support this attribute. Except MPlayer.

This feature can be used only with libavcodec's mpeg4 codec. Keep in mind: although MPlayer will correctly play the created file, other players will use the wrong aspect ratio.

You seriously should crop the black bands over and below the movie image. See the man page for the usage of the cropdetect and crop filters.

Usage:

$ mencoder sample-svcd.mpg -ovc lavc -lavcopts vcodec=mpeg4:autoaspect -vf crop=714:548:0:14 -oac copy -o output.avi