Search:  
Gentoo Wiki

MEncoder

Contents

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 man mencoder or the official documentation:

Subpages

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 encode use flag enabled when emerging media-video/mplayer to make sure MEncoder is installed.

Note: If you are serious about doing encoding, always emerge the latest ~arch versions of MPlayer and its libraries (xvid, etc) for best support and speed. Though considered "testing" in the portage tree, development versions are usually very sane and will work well.

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:

mencoder movie.wmv -o movie.avi -ovc lavc -oac lavc

I'll explain each option being passed:

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 midentify movie.avi. You can also run file movie.avi on the new file for a more basic output. Or, if you have media-video/transcode installed, you can use tcprobe -i movie.avi 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.

Note: Development versions of MEncoder have experimental support for writing directly to other container formats, using -of lavf and -lavfopts. See the man page for more info.

MPlayer documentation:

Wikipedia:

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:

mencoder -ovc help

'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.

Video codecs

lavc

MEncoder documentation:

FFmpeg documentation:

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.

 mencoder <filename.avi> -ovc lavc -oac lavc -ffourcc DX50 -o <output.avi>

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 header 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. You would also be well off adding that as a default in your mencoder config.

 ffourcc=DX50

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.

 $ man mencoder
 /\(\-lavcopts

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

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 or in your mencoder config file.

Use the acodec option for choosing your audio codec.

 mencoder movie.wmv -o movie.avi -ovc lavc -oac lavc -lavcopts acodec=libmp3lame

Note: acodec=mp3 has been deprecated in favour of acodec=libmp3lame in libavcodec. Use of -oac mp3lame and -lameopts instead of the libavcodec mp3 encoder is encouraged: far more options are available when using the mp3lame encoder directly.

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, very fast.

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.

 mencoder movie.wmv -o movie.avi -ovc lavc -oac lavc -lavcopts acodec=libmp3lame:abitrate=128

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

Your other 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. You'll be hard pressed to find a better option.

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

 mencoder movie.wmv -o movie.avi -ovc lavc -oac lavc

lavc Two-Pass Encoding

MEncoder documentation:

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

 mencoder dvd:// -ovc lavc -lavcopts vpass=1 -oac copy -o movie.avi
 mencoder dvd:// -ovc lavc -lavcopts vpass=2 -oac copy -o movie.avi

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

 mencoder dvd:// -ovc lavc -lavcopts vpass=1 -oac mp3lame -o movie.avi
 mencoder dvd:// -ovc lavc -lavcopts vpass=2 -oac mp3lame -o movie.avi

XviD

MEncoder documentation:

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:

mencoder <filename.avi> -ovc xvid -oac mp3lame -o <output.avi>

XviD Encoding Options

MEncoder Documentation:

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: http://forums.gentoo.org/viewtopic.php?t=280564

XviD One-Pass Encoding

Using bitrate will encode the video to a constant bitrate, or CBR.

mencoder -ovc xvid -oac mp3lame -xvidencopts bitrate=687 -o <output.avi> <input.avi> 

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.

mencoder -ovc xvid -oac mp3lame -xvidencopts fixed_quant=4 -o <output.avi> <input.avi>

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."

Taken and enriched from The Unofficial XviD FAQ

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

mencoder dvd:// -oac mp3lame -ovc xvid -xvidencopts pass=1 -o /dev/null
mencoder dvd:// -oac mp3lame -ovc xvid -xvidencopts pass=2:bitrate=800 -o <filename.avi>
mencoder <input.avi> -ovc xvid -oac mp3lame -xvidencopts pass=1 -o /dev/null
mencoder <input.avi> -ovc xvid -oac mp3lame -xvidencopts pass=2:bitrate=-700000 -o <output.avi> 

Mencoder uses the data gathered from the first pass via the divx2pass.log file (so stay in the same dir).

Audio codecs

MEncoder documentation:

First, see what audio codecs you can encode to:

 mencoder -oac help

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.

mencoder movie.wmv -o movie.avi -ovc lavc -oac copy

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.

mencoder movie.wmv -o movie.avi -ovc lavc -oac pcm
Note: One advantage to using PCM audio would be if you want to make minimal changes to the video (for example, changing video framerate) and none to the audio, but still need to encode the file for it to make your changes (where -ovc copy -oac copy won't work).

mp3lame

CBR/ABR/VBR encoding using lame.

mencoder movie.wmv -o movie.avi -ovc lavc -oac mp3lame

You can pass more options using -lameopts foo=bar

mencoder movie.wmv -o movie.avi -ovc lavc -oac mp3lame -lameopts abr:br=192
mencoder movie.wmv -o movie.avi -ovc lavc -oac mp3lame -lameopts vbr=2:q=3

See the man page for lots more options.

Note: libavcodec also includes an MP3 codec.

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 man mencoder, and search for lavcopts to see what audio options you have.

mencoder movie.wmv -o movie.avi -ovc lavc -oac lavc

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.

mencoder movie.wmv -o movie.avi -ovc lavc -oac lavc -lavcopts acodec=libmp3lame

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

mencoder movie.wmv -o movie.avi -ovc lavc -oac lavc -lavcopts acodec=libmp3lame:abitrate=96

For VCD and SVCD:

mencoder movie.wmv -o movie.avi -ovc lavc -oac lavc -lavcopts acodec=mp2:abitrate=224

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 ~/.mplayer/ directory.

MPlayer will use /etc/mplayer.conf first, then ~/.mplayer/config.

MEncoder will use ~/.mplayer/mencoder.conf.

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:

ovc=xvid=1

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

oac=mp3lame=1

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

xvidencopts=vhq=4:fixed_quant=4

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:

tv=driver=v4l2:device=/dev/video0:fps=29.97:norm=ntsc:amode=1:width=640:height=480

Rip or play DVDs using the English audio track:

alang=en

Set the mp3 quality rate to a higher value

For mp3lame:

oac=mp3lame=1
lameopts=q=3

For lavc:

oac=lavc=1
lavcopts=acodec=mp3:abitrate=256

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 ~/.mplayer/mencoder.conf, we can define profiles which make this easier. e.g.

   [mpeg4]
   profile-desc="MPEG4/MP3 encoding"
   ovc=lavc=1
   lavcopts=vcodec=mpeg4
   oac=mp3lame=1
   [x264]
   profile-desc="x264 encoding"
   ovc=x264=1
   oac=mp3lame=1

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:

mencoder movie.wmv -profile mpeg4 -o output.avi

GUI frontends to MEncoder

See the related projects page for a more extensive list. These here are already in portage:

Console frontends to MEncoder

Online MEncoder parameter generators

See also

Credits

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

Retrieved from "http://www.gentoo-wiki.info/MEncoder"

Last modified: Mon, 22 Sep 2008 04:00:00 +0000 Hits: 313,298