Mencoder

Introduction
This howto intends to introduce to you some of the simple commands to use MPlayer's MEncoder program to convert files from one format to another.

This is not meant to be an advanced or exhaustive guide. For in-depth details see the man page for mencoder or the official documentation:


 * MPlayer Documentation
 * Basic usage of MEncoder
 * Encoding with MEncoder

See also MEncoder Tips and Tricks

Installation
Complete instructions on how to install MEncoder and what USE flags to use can be found in MPlayer.

In short, make sure you have at least the USE flag enabled when emerging  to make sure MEncoder is installed.

The Basics
Now that you have mencoder installed, it's time to convert some files.

A common misconception is that since MPlayer can playback so many audio/video codecs and containers, that it can also encode to them as well. The list of codecs to encode media is too long, but some open source ones are not included (FLAC and Theora, for example).

Not to worry though -- you still have a lot of options, even if you want to encode a file for playback on Windows or Mac.

Before jumping into details, let's look at the basics for re-encoding a video file from a format MPlayer can play to an MPEG4. Here is a very simple example:

Here's an explanation each option being passed:


 * movie.wmv the filename of the original movie file you want to re-encode. It can be any file format that mplayer can play (mplayer can play almost anything).


 * -o  the filename to save the encoded file to. On older version of MPlayer, if you do not provide an argument, mencoder will output to "test.avi".


 * -ovc  OVC stands for 'output video codec'. This is the library you want to use to encode the video.


 * -oac  OAC stands for 'output audio codec'. This is the library you want to encode the audio portion with.

Believe it or not -- that's about all you need to know about re-encoding a file. The example above will encode the video to DivX (mpeg4) video and the audio track to mp2.

If you want to see how another file has been encoded, just run. You can also run on the new file for a more basic description. Or, if you have installed, you can use  as well.

Codecs and Formats
Clearing up even more confusion, video codecs and formats are not the same thing.

MPEG-4 is a video format. You can create MPEG-4 videos with some of the optional codecs. DivX, XviD and lavc are the codecs that actually create the videos for you. There are lots more video formats besides MPEG-4, and lavc is one codec that can encode to a lot of those formats.

MPEG-2 is another video format. DVD videos are stored in that format. That's why you can rip a DVD with similar quality but smaller size to the newer MPEG-4.

Before you start encoding your files though, it's a good idea to decide which video format you want. MPEG-4 will be used as the default in here, since it results in highly compressed files with very good quality.

Multimedia Containers
A multimedia container is what you will put your encoded audio and video into. Some container formats include AVI, ASF, Ogg Media, Matroska, and MOV. They are called containers because you can put all kinds of stuff into them. For instance, just because a file has an extension of .avi does not mean that it is an MPEG4. It can really be any kind of file that AVI has support for, both audio and video. That's why you can encode movies with so many codecs and still put them in the same wrapper.

Let's take a brief look at some of the more common multimedia containers you'll run into. Again, even though MPlayer can play back many of these, it can't encode to all of them. That won't prove to be a limitation though.

AVI (Audio Video Interleave), although it was created by Microsoft, is open and well supported. mplayer and mencoder can play and encode AVI files. AVI is quite common, and so you'll see a lot of our examples use this one.

Matroska is an open-source video container, similar to AVI, except that it has many more advanced options and settings that can be included in the metadata. mplayer and mencoder can play and read Matroska files, but not create them. Matroska audio and video files use filename extensions .mka and .mkv, respectively.

ASF (Advanced Streaming Format) is another wrapper format, designed by Microsoft, used mostly for streaming media. Technically, anything that can be put into an AVI can also be contained in an ASF, but generally speaking, most use Windows Media Video and/or Windows Media Audio codecs.

Ogg or Ogg bitstream format is also an open-source multimedia container, part of the Xiph project. OGM is an extension of Ogg bitstream format to support some proprietary video codecs. Like Matroska, mplayer can play, but not create Ogg and OGM videos.

MPlayer documentation:
 * Supported formats
 * Limitations of the AVI container

Wikipedia:
 * Comparison of container formats

Available Codecs
The first thing to do is to see what video codecs we can encode with. This command will give you the list of options:

'ovc' stands for output video codec.

The output of the command depends largely on how you compiled mplayer, and what USE flags you employed.

Since there are a lot to choose from, we'll look at a few of them briefly. If you don't know what to use, use -ovc lavc with the default options.

lavc
MEncoder documentation:
 * Encoding with the libavcodec codec family
 * Using libavcodec for VCD/SVCD/DVD Encoding
 * Video Codecs: FFmpeg/libavcodec

FFmpeg documentation:
 * Table of Contents
 * Supported Video Codecs
 * Supported Audio Codecs

lavc / libavcodec is part of the ffmpeg project's library.

From the ffmpeg homepage: "libavcodec is a library containing all the FFmpeg audio/video encoders and decoders. Most codecs were developed from scratch to ensure best performances and high code reusability." Using lavc as your video codec gives you lots of options, including lots of formats you can encode to.

Here's an example that will create a DivX AVI using lavc to create mpeg4 for video, and your audio to mp2. mpeg4 and mp2 are the default codecs used when no options are passed.

If you don't want to tweak your video settings, then you can use that example and you'll be fine with most players.

The -ffourcc DX50 option is included because although the video produced is an MPEG4, the default value is FMP4, which is not widely recognised. A header with DX50 claims that the video is DivX 5 (hence MPEG4) compatible and should therefore play on any media player that can play MPEG4 video. It may be worth it to adding this as a default in your mencoder config, particularly if you mostly encode this type of video. In your mencoder config file

lavc Encoding Options
If you are interested in passing some video encoding options with lavc, you will use the -lavcopts along with the variables you wish to change. See man mencoder and look for the lavcopts section. To skip to that section while viewing the man page, type: (or you can just scroll there).

Author's Note: After a lot of tweaking, encoding with lavc seems to be the least CPU intensive option, for both audio and video.

Since you can use lavc for encoding both video and audio (-ovc lavc -oac lavc), you can pass options for both audio and video in the same configuration line in -lavcopts. You can also set them as default in your mencoder config file.


 * acodec= 

Used to choose your audio codec, for example:

If size is not an issue and speed is, you might want to consider encoding the audio portion to mp2 instead of mp3. The files will be slightly larger, but it will put less of a strain on your processor, and therefore speed up encoding. Encoding a WAV to MP2 is also very fast.


 * abitrate= 

abitrate specifies the size of the audio bitrate in kbps. The default is 224. Changing this may or may not affect the filesize too much, depending on the range of sounds your movie uses.


 * vcodec= 

vcodec is where you decide which video codec to use. The default here is the high-quality mpeg4 (DivX 4/5).

Other vcodec options are: mjpeg (motion jpeg), ljpeg (lossless jpeg), h263, h263p, msmpeg4 (DivX 3), msmpeg4v2 (MS MPEG4v2), wmv1 (Windows Media Video, version 1), wmv2 (Windows Media Video, version 2), rv10 (an old RealVideo codec), mpeg1video, mpeg2video, huffyuv, ffvhuff, asv1, asv2 and ffv1.

If you don't know what you're doing, stick to mpeg4. It's a good and flexible option that is probably the best supported format by players.

Since MPEG4 is the default, you do not need to add any special options to -lavcopts for vcodec:

lavc Two-Pass Encoding
MEncoder documentation:
 * Encoding two pass MPEG-4 ("DivX")

Two pass encode of a DVD to an MPEG-4 ("DivX") AVI while copying the audio track.

Two pass encode of a DVD to an MPEG-4 ("DivX") AVI while encoding the audio track to MP3.

XviD
MEncoder documentation:
 * Encoding with the XviD codec
 * Video Codecs: XviD

XviD can create some very high-quality mpeg4 videos.

"XviD is a free software MPEG-4 ASP compliant video codec, which features two pass encoding and full MPEG-4 ASP support, making it a lot more efficient than the well-known DivX codec. It yields very good video quality and good performance due to CPU optimizations for most modern processors."

From the XviD User FAQ, "DivX is proprietary software, with an opaque development cycle that follows only a commercial logic. XviD is Free Software (licensed under the GNU GPL), open to third-party contributions and aims for standards compliance, portability and interoperability, high processing speed and superior quality."

Here again is our classic sample, encoding the video portion to mpeg4 using xvid, and the audio to mp3 using lame:

XviD Encoding Options
MEncoder Documentation:
 * Encoding setting examples

When encoding to XviD, you have to decide between doing one or two passes, and choose a quantizer using the -xvidencopts argument.

The choice is yours to run one or two passes on the original file. Quality will always be better on two passes, but will take longer as well. If speed is your main concern, then one pass will be fine. As always, experiment with your options and then decide on which you like best.

If you use one pass you can use bitrate or fixed_quant. If you do a two pass encoding you can only choose bitrate. If you don't specify one of these, mencoder will throw an error.

For more xvid encoding options and their summaries, see this post on the Gentoo forums.

XviD One-Pass Encoding
Using bitrate will encode the video to a constant bitrate, or CBR.


 * bitrate=  Higher = better quality with larger files, and lower = less quality with smaller files. With some optimizations you can get near DVD quality with around 800, VCD quality around 400-500 and internet streaming quality around 80-150. To get high quality on a 720x480 sized movie, you will need ~800kb/s. mencoder's default is 687 kbits/s, but you may have to specify that value based on your version.

You can use the bitrate option with one or two pass encodings. However, there is one other option to pass to XviD that only applies to one pass encodings: fixed_quant.


 * fixed_quant=<1-31> is similar to a "quality level" of the encoding which will switch between the modes automagically to obtain best size/quality ratio. Ignore this option unless you have specific needs. 1 is the highest quality, and will result in very large file if you use it. 31 is the lowest, and the video will look really crappy if you use that. A happy, high-quality medium would be around 4 or 5. Going above that, you will be wasting your time for little improvement.

XviD Two-Pass Encoding
Ripping a DVD with a two-pass encoding will result in higher quality than in simply one pass.

Single pass will take your clip and encode it at once. It takes each frame of the clip, checks that frame's compressibility, and then encodes it.

Two-pass uses the first pass to make an estimation of how well your clip compresses and then uses the compressibility data gathered during the first pass to really encode the clip during the second pass.

Which one to choose depends on what you desire from the result. Two-pass does a much better job at evenly distributing bits where they are needed and therefore gives you a much better looking end result. Generally what you do when you use this option is to calculate motion compensation and other non lossy compression related tweaks (qpel, trellis, gmc. bframe placement. etc) and then on the second pass where the compression is done. Single pass is really for those type of uses that can only be done with single-pass, like for instance real-time encoding a live feed, like a TV-capture or a security camera. Unless you absolutely have to go for single pass for a specific reason there really is no other way but two-pass."

Example adapted from The Unofficial XviD FAQ:

Here's an example of ripping a DVD using two passes:

A nice feature is that you can set a negative bitrate too, which tells mencoder to calculate the bitrate for you given a target size for the file. The negative number to use is that the target size, in KiloBytes. For example, to create a ~700MB file:

Mencoder uses the data gathered from the first pass via the divx2pass.log file created by that first pass, so stay in the same dir, (and don't do multiple first passes from the same directory).

Audio Codecs
MEncoder documentation:


 * Making a high quality MPEG-4 ("DivX") rip of a DVD movie: Audio
 * Using MEncoder to create VCD/SVCD/DVD-compliant files: Encoding Audio

First, see what audio codecs you can encode to:

copy
Frame copy, without re-encoding (useful for AC3)

If you're ripping a DVD and you want to keep the Dolby Digital stream, then use this option. Then, later you can pipe the output to your receiver with SPDIF.

pcm
Uncompressed PCM audio. It will sound nice, but the bulk of your file will be the audio portion. Essentially a useless option for movie encoding. If you want lossless, just do an AC3 dump (above), and you have the exact, original audio source, no transcoding. The advantage is that it is already compressed when you get it, so uncompressing it doesn't add quality, and only increases size, which cannot be wholly regained losslessly.

mp3lame
CBR/ABR/VBR encoding using lame.

You can pass more options using -lameopts option1=value:option2=value
 * abr sets average bitrate.
 * cbr uses a constant bitrate.
 * br=<0-1024> sets bitrate in kbps, for CBR and ABR only
 * q=<0-9> quality (0 - highest, 9 - lowest, VBR only)
 * q=<0-9> quality (0 - highest, 9 - lowest, VBR only)

See the man page for lots more options.

lavc
FFmpeg audio encoder (MP2, MP3)

By default encodes audio to MP2 and so the files will be slightly larger than if you use MP3.

See the man page for mencoder, and search for lavcopts to see what audio options you have.

If no options are passed in -lavcopts, then lavc will encode the audio to MP2.

Use the acodec argument with lavcopts to change the audio codec.

This example will encode the audio stream to MP3, using the ffmpeg libraries.

You can change the bitrate using abitrate= . The default is 224.

For VCD and SVCD:

Configuration Files
Now that you're a MEncoder master, you want to save time and not pass every single option to the command line when encoding files. That's simply done by creating config files in your directory.

MPlayer will use first, then.

MEncoder will use.

Each line of the config file will contain the argument you want to pass, an equal sign, and then each suboption separated by a colon.

For example, let's say you always want to rip your movies using -ovc xvid. This is a special example, actually, because -ovc doesn't have any suboptions -- just xvid. Anytime there are no suboptions to the argument, you will need to set the argument to 1. So, here's what you would put:

The same is true for your audio codec. If you wanted to always encode to mp3 using lame, you would add this:

If using xvid, you do have some suboptions you want to pass. Since the mencoder command line argument would be -xvidencopts :, something similar goes in your config:

Some more examples would be:

Record or watch TV using the v4l2 driver with device /dev/video0, output to 29.97 frames per second (NTSC standard), force stereo, and have the size be 640x480:

Rip or play DVDs using the English audio track:

Set the mp3 quality rate to a higher value

For mp3lame:

For lavc:

Profiles
Mencoder has a huge set of options, and it is easy to forget which options worked best in the past for specific tasks. In, we can define profiles which make this easier. e.g.

Here, we have defined two useful profiles -- one for MPEG4, and one that uses the new x264 codec.

One would use these profiles like this:

GUI frontends to MEncoder
See the http://mplayerhq.hu/design7/projects.html related projects page] for a more extensive list. These here are already in portage:


 * AcidRip - A gtk-perl mplayer/mencoder frontend for ripping DVDs
 * Freevo - Digital video jukebox (PVR, DVR)
 * Konverter - A KDE MEncoder frontend for video-conversion
 * OGMRip - Application and libraries for encoding DVDs into AVI/OGM/MKV files
 * K9Copy - A QT frontend with mencoder/transcode support

Console frontends to MEncoder

 * SimpleRip- Written in Perl using Term::ANSIColor

Online MEncoder parameter generators
 * SimpleRip

Final Notes
A special thanks to the MPlayer developers, who created (and still develop) an awesome piece of software. :)


 * Donations