Topics:


I. Preparing to encode
   1. Identifying source material and framerate
   2. Selecting the quality you want
   3. Constraints for efficient encoding
   4. Cropping and scaling
   5. Choosing resolution and bitrate

II. Containers and codecs
   1. Where the movie will be played
   2. Constraints of DVD, SVCD, and VCD
   3. Limitations of AVI container

III. Basic MEncoder usage
   1. Selecting codecs & format
   2. Selecting input file or device
   3. Loading video filters
   4. Notes on A/V sync

IV. Encoding procedures
   1. Encoding progressive video
   2. Two-pass encoding
   3. Encoding interlaced video
   4. Deinterlacing
   5. Inverse telecine
   6. Capturing TV input
   7. Dealing with mixed-source content
   8. Low-quality & damaged sources

V. Optimizing encoding quality
   1. Noise removal
   2. Pure quality-gain options
   3. Questionable-gain options
   4. Advanced MPEG-4 features




II. Containers and codecs

II.1. Where the movie will be played

Perhaps the most important factor to choosing the format in which you
will encode your movie is where you want to be able to play it.
Usually this involves a tradeoff between quality and features, since
the formats supported by the widest variety of players are also the
worst in regards to compression.

If you want to be able to play your encode on standalone/set-top
players, your primary choices are DVD, VCD, and SVCD. There are also
extensions such as KVCD and XVCD which violate the standards but work
on many players and deliver higher quality. Modern players are
beginning to support MPEG-4 ("DivX") movies in AVI and perhaps other
containers as well, but these are often buggy and require you to
restrict your encodes to certain subsets of the full MPEG-4
functionality.

If you wish to be able to share your movies with Windows or Macintosh
users, without them having to install additional software, your
choices are very limited. The ancient MPEG-1 format with MP2 or PCM
audio is probably the only choice that is universally supported.
Interoperability with Windows/Mac also comes into play when deciding
how to encode and whether to scale to preserve aspect, since popular
media player applications for these systems do not honor the aspect
ratio encoding stored in MPEG-4 avi files.



IV.2. Two-pass encoding

The complexity (and thus the number of bits) required to compress the
frames of a movie can vary greatly from one scene to another. Modern
video encoders can adjust to these needs as they go and vary the
bitrate. However, they cannot exceed the requested average bitrate for
long stretches of time, because they do not know the bitrate needs of
future scenes.

Two-pass encoding solves this problem by encoding the movie twice.
During the first pass, statistics are generated regarding the number
of bits used by each frame and the quantization level (quality) at
which it was encoded. Then, when the second pass begins, the encoder
reads these statistics and redistributes the bits from frames where
they are in excess to frames that are suffering from low quality.

In order for the process to work properly, the encoder should be given
exactly the same sequence of frames during both passes. This means
that the same filters must be used, the same encoder parameters must
be used (with the possible exception of bitrate), and the same frame
drops and duplications (if any) must take place.

In theory it's possible to use -oac pcm or -oac copy during the first
pass to avoid spending time encoding the audio. However, this can
result in slight variations in which frames get dropped or duplicated,
so it may be preferable to encode the audio during the first pass as
well as the second. This also allows you to examine the final audio
bitrate and filesize, and to adjust the audio or video bitrate
slightly between passes if you don't meet your target size.

Here is an example:

  Encoding from an existing AVI file
  500 kbit/sec MPEG-4 video
  96 kbit/sec average-bitrate MP3 audio

  mencoder bar.avi -vf scale=448:336 -mc 0 -oac mp3lame -lameopts \
  abr:br=96 -ovc lavc -lavcopts vcodec=mpeg4:vbitrate=500:vpass=1

  mencoder bar.avi -vf scale=448:336 -mc 0 -oac mp3lame -lameopts \
  abr:br=96 -ovc lavc -lavcopts vcodec=mpeg4:vbitrate=500:vpass=2

If you do not want to overwrite the output from the first pass when
you begin the second, you can use the -o option to choose a different
output filename. Note the addition of the vpass option in this
example. If vpass is not specified, single-pass encoding is performed.
If vpass=1, a log file is written with statistics from the first pass.
If vpass=2, the log file is read and the second pass is encoded based
on those statistics. If you are short on disk space or don't want the
extra disk wear from writing the file twice, you can use -o /dev/null
during the first pass. However, sometimes it is beneficial to watch
the first-pass file before beginning the second pass to make sure
nothing went wrong in the encoding.

Next, an example using Xvid instead of libavcodec:

  Encoding from an existing AVI file
  500 kbit/sec MPEG-4 video
  Copying the existing audio stream unmodified

  mencoder foo.avi -vf scale=320:240 -mc 0 -oac copy -ovc xvid \
  -xvidencopts bitrate=400:pass=1

  mencoder foo.avi -vf scale=320:240 -mc 0 -oac copy -ovc xvid \
  -xvidencopts bitrate=400:pass=2

The options used are slightly different, but the process is otherwise
the same.
