Gentoo Wiki



Create an MPEG, not an AVI

If for some reason, you need to create an mpeg instead, here's how.

MEncoder uses an "-of" flag which stands for output format. There are two options: avi and mpeg. The default is avi, so normally you don't need to use the flag. Just use "-of mpeg" to output to an mpeg header/file. By default, you probably don't need to use this as you can encode mpeg4 files (divx4/divx5) in avi files just fine.

mencoder movie.wmv -o movie.mpg -ovc lavc -oac mp3lame -of mpeg

If you get an error like this:

[mpeg1video @ 0x83b0c90]MPEG1/2 doesnt support 1000/1 fps

try to add -fps 25 to set manually a framerate. The problem can also be that you need to set "-lavcopts" so if the above doesn't work try this:

mencoder movie.wmv -o movie.mpg -ovc lavc -lavcopts vcodec=mpeg2video -oac copy -ofps XX -of mpeg

Where XX is the framerate of the wmv-file.

Author's Note: If you're having issues creating mpegs with mencoder, try ffmpeg instead. Plus, it's a lot easier.

Getting the latest win32codecs

If you're impatient or are having problems with your codecs for some reason, you can always download the latest package straight from the MPlayer website.

  1. Goto
  2. Download the "essential" package
  3. untar the files to /usr/lib/win32

If you're not having problems -- ignore this step.

Encode to NTSC or PAL Framerate

To encode the movie with the NTSC framerate of 29.97 frames per second:

NOTE: 29.97 is an approximation, more accurately, it's 30/1.001 (same applies to 23.976, it's more accurately 24/1.001). New versions of mencoder support fractional values, and thus can be used here

mencoder <filename.avi> -ovc lavc -oac mp3lame -o <output.avi> -ofps 30000/1001

To encode the movie with the PAL framerate of 25 frames per second:

mencoder <filename.avi> -ovc lavc -oac mp3lame -o <output.avi> -ofps 25

Note that you will have A-V sync problems if the framerate of the source does not match the output. See the docs for information about using telecine in these cases.

Maximum Encoding Length

So you want to try encoding a file to see if it works before encoding the whole thing? No problem. Do a sample conversion using "-endpos" to test it.

The example starts encoding 5 minutes into the movie, and records 8 minutes (ending at 13:00):

mencoder movie.wmv -o movie.avi -ovc lavc -oac mp3lame -ss 5:00 -endpos 8:00

See the manpage for more information

Generating AVIs with fixed output size (2-pass encoding)

If you want to generate AVIs with a fixed output size you have to determine the size of the audio track first. Therefore you need a multi-pass encoding:

  1. Encode audio track to determine its size.
  2. Analyze video track (first-pass).
  3. Reencode video track (second-pass).

First you have to choose the audio codec you want to use. If you don't want to reencode the audio track (you want to keep ac3) you can use "-oac copy" as described above in this Howto. In this example, we will encode VBR MP3 using lame, by piping the audio to lame using ffmpeg.

ffmpeg -i input_movie -v 0 -vn -f wav - | lame -h --vbr-new -V 4 - output_file.mp3

Once this finishes we get the size of our audio. Lets assume we want to fit the movie on 1CD = 700MB*1024 = 716800 kbytes. Subtract the size of the audio from this to get the size of the video. Put a negative sign in front of this value, and the encoder will automatically compute the required bitrate. Alternatively, you can manually compute the average bitrate by doing 8*videosize (kbytes)/time (s). Keep in mind that the AVI container will use 24 bytes per frame, which corresponds to 2-3 MB per hour of overhead, so subtract a bit more.

Now you know the new bitrate to use to get your output file on one or two CDs but you can't really tell what the quality of the output file will be. For example, if you are encoding a DVD at lower (1CD) bitrates you should scale it down to a lower resolution, as this will improve output quality. Mencoder offers a contributed tool named (can be found in /usr/share/doc/mplayer-*/TOOLS/) which can calculate recommended output resolutions. Look at this tool if you are interested - it even corrects the aspect ratio for you if you have a dvd as an input file.

The new output width and height should be divisible by 16, as this is the smallest blocksize possible. Anything else will waste bits.

Now we run the first pass to collect stats on the compressibility of the video.

mencoder -o /dev/null -oac copy -ovc lavc -lavcopts vcodec=mpeg4:vbitrate=863:vhq:turbo:vpass=1 -vf scale=576:320 input_movie

Note that we use "-oac copy" because -nosound can sometimes cause sync problems. In this example mencoder analyses an input_movie which will be reencoded to a MPEG4 DIVX file using 863 kbit/s and an output resolution of 576:320 pixels. Of course you can use your favorite output codec instead of DIVX. The parameter 'vhq' tells lavc to use 'very high quality'. 'vpass=1' tells it that this is the first pass of a two pass encoding, and 'turbo' greatly speeds up the first pass with minimal quality loss. After this step you don't have your new movie yet but a .log file (it can be named using '-passlogfile <filename>', but make sure to specify it in the 2nd pass) which is needed to reencode your movie in best quality.

Finally you can create your new output video using nearly the same command line as above:

mencoder -o output_file.avi -audiofile output_file.mp3 -oac copy -ovc lavc -lavcopts vcodec=mpeg4:vbitrate=863:vhq:vpass=2 -vf scale=576:320 input_movie

Beware that you now have to use 'vpass=2' and specify a real output file with '-o filename'. The previously encoded mp3 file is now merged with video stream. Make sure you do not change any video-related options for the second pass. This step uses your logfile from the first pass to generate the final video output_file.avi. You can then delete the logfile and any other intermediate files.

Stream Ripping

Just as mplayer can play audio/video streams, mencoder can rip them for you as well.

See also: MPlayer/Rip streams

Microsoft ASX Audio Streams

ASX streams can't be ripped on the fly, but you can use mplayer to download them, and then use mencoder to encode them later.

This example will dump an asx stream to a file named "stream.dump":

mplayer -playlist <http url of stream.asx> -dumpaudio

Optionally you can add the -dumpfile option to specify a file name for the dumped stream for example:

mplayer -playlist <http url of stream.asx> -dumpaudio -dumpfile <dump.wav>

RealPlayer rtsp Video Streams

No special options needed here -- just pass in your video and audio encoding options.

This example will rip the video to mpeg4 using lavc, and the audio to mp3 using lame:

mencoder <rtsp url of trailer.rm> -ovc lavc -oac mp3lame -o <output.avi>

Sometimes RealPlayer files are actually playlists, so you will need to pass the -playlist option. One way to verify if this is the case or not, is if you can download the .ram file, and it is just a text file with a list of items to play.

$ wget <some site>/movie.ram
$ cat movie.ram
<some site>/movie.rm

If that's the case, you can download the file using the -dumpstream and -dumpfile options with mplayer. This is generally much faster than using mencoder to copy the file, since mplayer will seek in the stream and grab all of it quickly.

$ mplayer <some site>/movie.rm -dumpstream -dumpfile movie.avi

One very valuable tip is that sometimes you can play (rip) RealPlayer-encoded movies over more than one protocol. If pnm:// does not work for you, try the same URL with http:// or rtsp:// instead!

Hardcoded subtitles

You can hardcode subtitles directly into the video by specifying the 'sub' option during encoding

mencoder -ovc xvid -xvidencopts fixed_quant=5 -oac copy -sub -o output.avi input.avi

Convert Matroska to AVI

It's possible to extract one video, audio, and subtitle track from a Matroska file and place it into an AVI file. In this example, the AVI will hold an xvid video track with an MP3 audio track. The subtitle track actually becomes part of the video itself since an AVI doesn't support holding subtitle tracks.

First, use mplayer's -v option to acquire the video, audio, and subtitle ID (or language code) you wish to make the AVI from.

mplayer -v file.mkv

This should have given you the list of video IDs available, a list of audio IDs and languages available, and a list of subtitle IDs and languages available. Select the appropriate IDs (or languages) you wish to create an AVI file from and create the AVI file.

Here's an example using the audio and subtitle IDs. The xvid bitrate is set to 1200 kbits/second.

mencoder file.mkv -aid 0 -sid 0 -ovc xvid -oac mp3lame -lameopts br=192 -mc 0 -xvidencopts pass=1 -o /dev/null
mencoder file.mkv -aid 0 -sid 0 -ovc xvid -oac mp3lame -lameopts br=192 -mc 0 -xvidencopts pass=2:bitrate=1200 -o file.avi

Here's another example, this time using the audio and subtitle language codes.

mencoder file.mkv -alang jpn -slang eng -ovc xvid -oac mp3lame -lameopts br=192 -mc 0 -xvidencopts pass=1 -o /dev/null
mencoder file.mkv -alang jpn -slang eng -ovc xvid -oac mp3lame -lameopts br=192 -mc 0 -xvidencopts pass=2:bitrate=1200 -o file.avi

Note the use of the -mc option. Setting the value to 0 disables the A-V sync correction. If you're having audio/video sync issues, try changing the value. The -ss (start) and -endpos (end position) options also come in handy when trying to solve sync issues.

Below is a simple shell script that looks for Matroska video files in the current directory and converts them to AVI files. This script was written to convert a collection of anime episodes.


# This program looks through the current directory for files to convert from
# Matroska video files to AVI.

# Various parameters that could need adjusting for each file
AUDIO_LANG="-alang jpn"
SUBTITLE_LANG="-slang eng"
VIDEO_CODEC="-ovc xvid"
AUDIO_CODEC="-oac mp3lame"
XVID_PASS1_OPTIONS="-xvidencopts pass=1 -o /dev/null"
XVID_PASS2_OPTIONS="-xvidencopts pass=2:bitrate=$VIDEO_BITRATE"
GENERAL_MENCODER_OPTIONS="-mc 0" #Disabled A-V sync correction

This script is used to convert all Matroska video files (mkv) in the current\n\
directory into AVI files.\n\
 -h, --help                 Display this help message.\n\
 --remove-matroska-files    Remove the Matroska files after conversion.\n\
 --keep-passlog-files       Keep the passlog file.\n"

while [ "$#" -gt "0" ]
    case "$1" in
            echo "${USAGE}"
            exit 1

# Find all Matroska video files.
for MKV_FILE in *.mkv; do
	echo "Converting $MKV_FILE"
	# Remove .mkv and rename file with correct encoding information.
	MKV_NAME=`echo $MKV_FILE | \
		sed -e s/\.mkv$//g -e s/DVD\(H264\.AAC\)/\(Xvid\.MP3\)/g`
	echo "Running first pass."
		-passlogfile "$MKV_NAME.log"
	echo "Running second pass."
		-o "$MKV_NAME.avi" -passlogfile "$MKV_NAME.log"
	echo "$MKV_NAME.avi is ready!"
	if [ -z $KEEP_PASSLOG_FILES ]; then
		echo "Removing passlog file."
		rm "$MKV_NAME.log"
		echo "Removing original Matroska file."
		rm "$MKV_FILE"


HOWTO DVD to Matroska - Excellent guide for ripping DVDs using H.264

Retrieved from ""

Last modified: Sun, 07 Sep 2008 09:30:00 +0000 Hits: 88,767