# Basic usage of 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 here and especially for older things also here for a wealth of discussions about all aspects of and problems related to encoding with MEncoder.

## Selecting codecs and container formats

Audio and video codecs for encoding are selected with the `-oac` and `-ovc` options, respectively. Type for instance:

    mencoder -ovc help

to list all video codecs supported by the version of MEncoder on your machine. The following choices are available:

Audio Codecs:

Audio codec name | Description
mp3lame | encode to VBR, ABR or CBR MP3 with LAME
lavc | use one of `libavcodec`'s audio codecs
faac | FAAC AAC audio encoder
toolame | MPEG Audio Layer 2 encoder
twolame | MPEG Audio Layer 2 encoder based on tooLAME
pcm | uncompressed PCM audio
copy | do not reencode, just copy compressed frames

Video codecs:

Video codec name | Description
lavc | use one of `libavcodec`'s video codecs
xvid | Xvid, MPEG-4 Advanced Simple Profile (ASP) codec
x264 | x264, MPEG-4 Advanced Video Coding (AVC), AKA H.264 codec
nuv | nuppel video, used by some realtime applications
raw | uncompressed video frames
copy | do not reencode, just copy compressed frames
frameno | used for 3-pass encoding (not recommended)

Output container formats are selected with the `-of` option. Type:

    mencoder -of help

to list all containers supported by the version of MEncoder on your machine. The following choices are available:

Container formats:

Container format name | Description
lavf | one of the containers supported by `libavformat`
avi | Audio-Video Interleaved
mpeg | MPEG-1 and MPEG-2 PS
rawvideo | raw video stream (no muxing - one video stream only)
rawaudio | raw audio stream (no muxing - one audio stream only)

The AVI container is the native container format for MEncoder, which means that it's the one that is best handled, and the one for which MEncoder was designed. As noted above, other container formats are usable, but you may experience problems when using them.

`libavformat` containers:

If you selected `libavformat` to do the muxing of the output file (by using the `-of lavf`), the appropriate container format will be determined by the file extension of the output file. You may force a particular container format with `libavformat`'s `format` option.

`libavformat` container name | Description
mpg | MPEG-1 and MPEG-2 PS
asf | Advanced Streaming Format
avi | Audio-Video Interleaved
wav | Waveform Audio
swf | Macromedia Flash
flv | Macromedia Flash video
rm | RealMedia
au | SUN AU
nut | NUT open container (experimental and not yet spec-compliant)
mov | QuickTime
mp4 | MPEG-4 format
dv | Sony Digital Video container
mkv | Matroska open audio/video container

As you can see, `libavformat` allows MEncoder to mux into a considerable variety of containers. Unfortunately, as MEncoder was not designed from the beginning to support container formats other than AVI, your should really be paranoid about the resulting file. Please check to be sure that the audio/video synchronization is OK and that the file can be played correctly by players other than MPlayer.

### encode to Macromedia Flash format

Creating a Macromedia Flash video suitable for playback in a web browser with the Macromedia Flash plugin:

    mencoder input.avi -o output.flv -of lavf \
        -oac mp3lame -lameopts abr:br=56 -srate 22050 -ovc lavc \
        -lavcopts vcodec=flv:vbitrate=500:mbd=2:mv0:trell:v4mv:cbp:last_pred=3

## Selecting input file or device

MEncoder can encode from files or directly from a DVD or VCD disc. Simply include the filename on the command line to encode from a file, or `dvd://``titlenumber` or `vcd://``tracknumber` to encode from a DVD title or VCD track. If you have already copied a DVD to your hard drive (you can use a tool such as dvdbackup, available on most systems), and wish to encode from the copy, you should still use the `dvd://` syntax, along with `-dvd-device` followed by the path to the copied DVD root. The `-dvd-device` and `-cdrom-device` options can also be used to override the paths to the device nodes for reading directly from disc, if the defaults of `/dev/dvd` and `/dev/cdrom` do not work on your system.

When encoding from DVD, it is often desirable to select a chapter or range of chapters to encode. You can use the `-chapter` option for this purpose. For example, `-chapter` `1-4` will only encode chapters 1 through 4 from the DVD. This is especially useful if you will be making a 1400 MB encode targeted for two CDs, since you can ensure the split occurs exactly at a chapter boundary rather than in the middle of a scene.

If you have a supported TV capture card, you can also encode from the TV-in device. Use `tv://``channelnumber` as the filename, and `-tv` to configure various capture settings. DVB input works similarly.

## Encoding two pass MPEG-4 ("DivX")

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 or rather just not create any video by redirecting it into `/dev/null` or on Windows into `NUL`). In the second pass, the two 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.

### copy audio track

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

    mencoder dvd://2 -ovc lavc -lavcopts vcodec=mpeg4:vpass=1 -oac copy -o /dev/null
    mencoder dvd://2 -ovc lavc -lavcopts vcodec=mpeg4:mbd=2:trell:vpass=2 \
        -oac copy -o output.avi

### encode audio track

Two pass encode of a DVD to an MPEG-4 ("DivX") AVI while encoding the audio track to MP3. Be careful using this method as it may lead to audio/video desync in some cases.

    mencoder dvd://2 -ovc lavc -lavcopts vcodec=mpeg4:vpass=1 \
        -oac mp3lame -lameopts vbr=3 -o /dev/null
    mencoder dvd://2 -ovc lavc -lavcopts vcodec=mpeg4:mbd=2:trell:vpass=2 \
        -oac mp3lame -lameopts vbr=3 -o output.avi

## Encoding to Sony PSP video format

MEncoder supports encoding to Sony PSP's video format, but, depending on the revision of the PSP software, the constraints may differ. You should be safe if you respect the following constraints:

- Bitrate: it should not exceed 1500kbps, however, past versions supported pretty much any bitrate as long as the header claimed it was not too high.

- Dimensions: the width and height of the PSP video should be multiples of 16, and the product width * height must be <= 64000. Under some circumstances, it may be possible for the PSP to play higher resolutions.

- Audio: its samplerate should be 24kHz for MPEG-4 videos, and 48kHz for H.264.

### encode for PSP

    mencoder -ofps 30000/1001 -af lavcresample=24000 -vf harddup -oac lavc \
        -ovc lavc -lavcopts aglobal=1:vglobal=1:vcodec=mpeg4:acodec=libfaac \
        -of lavf -lavfopts format=psp \
        input.video -o output.psp

Note that you can set the title of the video with `-info name=`MovieTitle``.

## Encoding to MPEG format

MEncoder can create MPEG (MPEG-PS) format output files. Usually, when you are using MPEG-1 or MPEG-2 video, it is because you are encoding for a constrained format such as SVCD, VCD, or DVD. The specific requirements for these formats are explained in the VCD and DVD creation guide section.

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

Example:

    mencoder input.avi -of mpeg -ovc lavc -lavcopts vcodec=mpeg1video \
        -oac copy other_options -o output.mpg

Creating an MPEG-1 file suitable to be played on systems with minimal multimedia support, such as default Windows installs:

    mencoder input.avi -of mpeg -mpegopts format=mpeg1:tsaf:muxrate=2000 \
        -o output.mpg -oac lavc -lavcopts acodec=mp2:abitrate=224 -ovc lavc \
        -lavcopts vcodec=mpeg1video:vbitrate=1152:keyint=15:mbd=2:aspect=4/3

Same, but using `libavformat` MPEG muxer:

    mencoder input.avi -o VCD.mpg -ofps 25 -vf scale=352:288,harddup -of lavf \
        -lavfopts format=mpg -oac lavc -lavcopts acodec=mp2:abitrate=224 -ovc lavc \
        -lavcopts vcodec=mpeg1video:vrc_buf_size=327:keyint=15:vrc_maxrate=1152:vbitrate=1152:vmax_b_frames=0

### Hint:

If for some reason the video quality of the second pass did not satisfy you, you may re-run your video encode with a different target bitrate, provided that you saved the statistics file of the previous pass. This is possible because the statistics file's primary goal is to record the complexity of each frame, which doesn't depend heavily on bitrate. You should note, though, that you'll get the best results if all passes are run with target bitrates that do not differ very much.

## Rescaling movies

Often the need to resize movie images emerges. The reasons can be many: decreasing file size, network bandwidth, etc. Most people even do rescaling when converting DVDs or SVCDs to DivX AVI. If you wish to rescale, 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 is not specified, MEncoder will use 2: bicubic.

Usage:

    mencoder input.mpg -ovc lavc -lavcopts vcodec=mpeg4:mbd=2:trell \
        -vf scale=640:480 -o output.avi

## Stream copying

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

- Video stream (option `-ovc copy`): nice stuff can be done :) Like, putting (not converting!) FLI or VIVO or MPEG-1 video into an AVI file! Of course only MPlayer can play such files :) And it probably has no real life value at all. Rationally: video stream copying can be useful for example when only the audio stream has to be encoded (like, uncompressed PCM to MP3).

- Audio stream (option `-oac copy`): straightforward. It is possible to take an external audio file (MP3, WAV) and mux it into the output stream. Use the `-audiofile `filename`` option for this.

Using `-oac copy` to copy from one container format to another may require the use of `-fafmttag` to keep the audio format tag of the original file. For example, if you are converting an NSV file with AAC audio to an AVI container, the audio format tag will be incorrect and it will have to be changed. For a list of audio format tags, check `codecs.conf`.

Example:

    mencoder input.nsv -oac copy -fafmttag 0x706D \
        -ovc lavc -lavcopts vcodec=mpeg4:mbd=2:trell -o output.avi

## Encoding from multiple input image files (JPEG, PNG, TGA, etc.)

MEncoder is capable of creating movies from one or more JPEG, PNG, TGA, or other image 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 is in the man page.

Creating an MPEG-4 file from all the JPEG files in the current directory:

    mencoder mf://*.jpg -mf w=800:h=600:fps=25:type=jpg -ovc lavc \
        -lavcopts vcodec=mpeg4:mbd=2:trell -oac copy -o output.avi

Creating an MPEG-4 file from some JPEG files in the current directory:

    mencoder mf://frame001.jpg,frame002.jpg -mf w=800:h=600:fps=25:type=jpg \
        -ovc lavc -lavcopts vcodec=mpeg4:mbd=2:trell -oac copy -o output.avi

Creating an MPEG-4 file from explicit list of JPEG files (list.txt in current directory contains the list of files to use as source, one per line):

    mencoder mf://@list.txt -mf w=800:h=600:fps=25:type=jpg \
        -ovc lavc -lavcopts vcodec=mpeg4:mbd=2:trell -oac copy -o output.avi

You can mix different types of images, regardless of the method you use — individual filenames, wildcard or file with list — provided of course they have the same dimensions. So you can e.g. take title frame from PNG file, and then put a slideshow of your JPEG photos.

Creating a Motion JPEG (MJPEG) file from all the JPEG files in the current directory:

    mencoder mf://*.jpg -mf w=800:h=600:fps=25:type=jpg -ovc copy -oac copy -o output.avi

Creating an uncompressed file from all the PNG files in the current directory:

    mencoder mf://*.png -mf w=800:h=600:fps=25:type=png -ovc raw -oac copy -o output.avi

Width must be integer multiple of 4, it is a limitation of the RAW RGB AVI format.

Creating a Motion PNG (MPNG) file from all the PNG files in the current directory:

    mencoder mf://*.png -mf w=800:h=600:fps=25:type=png -ovc copy -oac copy -o output.avi

Creating a Motion TGA (MTGA) file from all the TGA files in the current directory:

    mencoder mf://*.tga -mf w=800:h=600:fps=25:type=tga -ovc copy -oac copy -o output.avi

## Extracting DVD subtitles to VOBsub file

MEncoder is capable of extracting subtitles from a DVD into VOBsub formatted 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.

### Copying two subtitles from a DVD while doing two pass encoding

    rm subtitles.idx subtitles.sub
    mencoder dvd://1 -oac copy -ovc lavc -lavcopts vcodec=mpeg4:vpass=1 \
        -vobsubout subtitles -vobsuboutindex 0 -sid 2
    mencoder dvd://1 -oac copy -ovc lavc -lavcopts vcodec=mpeg4:mbd=2:trell:vpass=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 -nosound -ovc copy

## Preserving aspect ratio

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

There is

MPEG-4 has a unique feature: the video stream can contain its needed aspect ratio. Yes, just like MPEG-1/2 (DVD, SVCD) and H.263 files. Regretfully, there are few video players apart from MPlayer that support this MPEG-4 attribute.

This feature can be used only with `libavcodec`'s `mpeg4` codec. Keep in mind: although MPlayer will correctly play the created file, other players may 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 -vf crop=714:548:0:14 -oac copy -ovc lavc \
        -lavcopts vcodec=mpeg4:mbd=2:trell:autoaspect -o output.avi