MEncoder

From ArchWiki

Jump to: navigation, search
Article summary
Provides an overview of the video encoding/decoding tool provided by MPlayer.
Available in languages
English
Related articles
DVD Ripping
Dvd2Avi
MPlayer

MEncoder is part of the mplayer package. See MPlayer for details.

Contents

Basics

The basic syntax for a conversion is

mencoder original_video.mpg -o new_video.avi -ovc output_video_codec -oac output_audio_codec

So to convert movie.mpg to movie.avi with DivX video and MP2 audio, the command is

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

The default codecs without any options are MPEG4 for video and MP2 for audio.

This is basically how one converts a video. However, there are MANY more options available.

For input codecs, MEncoder can use any codec that MPlayer can play, so to verify whether it will work with your video, just try playing it in MPlayer.

To list output video codecs, run

$ mencoder -ovc help

Similarly, to list output audio codecs, run

$ mencoder -oac help

This information can also be found here where it better explained, although non-specific.

There are many more options available (e.g. you can define bit rates and expected sizes). The Gentoo wiki has a lot more information on the usage of MEncoder.

Example

This approach allows one to make a .mkv file with an H.264-encoded video and any number of Vorbis-encoded audio tracks.

We will use mencoder (part of mplayer package) for ripping and encoding and mkvmerge (part of mkvtoolnix package) for making the .mkv file itself.

Ripping and encoding the video

The H.264 encoder is usually used in two passes: the first reads informations about the movie, the second uses that information to encode. We will not extract any audio for now.

Commands follow; remember to replace the variables with the proper values:

# First pass: we are just collecting information, so the normal output is thrown away.
mencoder -dvd-device "$ISO" dvd://"$TITLE" -chapter "$CHAPTER" -o /dev/null -nosound -ovc x264 \
-x264encopts direct=auto:pass=1:turbo:bitrate=900:bframes=1:\
me=umh:partitions=all:trellis=1:qp_step=4:qcomp=0.7:direct_pred=auto:keyint=300 \
-vf scale=-1:-10,harddup
# Second pass: here we compress the video track using the information from the first step.
mencoder -dvd-device "$ISO" dvd://"$TITLE" -chapter "$CHAPTER" -nosound -ovc x264 \
-x264encopts direct=auto:pass=2:bitrate=900:frameref=5:bframes=1:\
me=umh:partitions=all:trellis=1:qp_step=4:qcomp=0.7:direct_pred=auto:keyint=300 \
-vf scale=-1:-10,harddup -o video.avi

This will create a video.avi file containing the video. You can play with the -x264encopts options and the -vf filters to improve the quality or reduce the file size. For example, a movie with a black border should be cropped with -vf crop=$X:$Y,scale=-1:-10,harddup with the proper values instead of $X and $Y (see cropdetect in the MEncoder manual). You may want to scale down the movie with -vf scale=$WIDTH:-10,harddup the width of the movie will become $WIDTH (keep $WIDTH a multiple of 16: 640, 480, or 320 are usually fine), the height will be correctly calculated in order to keep the aspect ratio.

You can also use any other of the filters MEncoder has to offer, like pullup,softskip or you can change the frame rate using -ofps. (If you do so, remember to use the same frame rate everywhere including in the commands to rip audio.)

It is important that you use harddup as the last filter: it will force MEncoder to write every frame (even duplicate ones) in the output. Also, it is necessary to use scale=$WIDTH,-10 with $WIDTH as -1 to keep the original width or a new, usually smaller, width: it is necessary since the H.264 codec uses square pixels and DVDs instead use rectangular pixels.

Ripping and encoding the audio

You can extract audio tracks as needed. Here we compress with the Vorbis algorithm, but you may want to check the MEncoder manual in order to see alternatives.

The command follows (replace the variables with desired values):

# Here we rip and compress the audio.
mencoder -dvd-device "$ISO" dvd://"$TITLE" -alang "$AUDIOLANG" -chapter "$CHAPTER" -ovc frameno \
-oac lavc -lavcopts acodec=vorbis:abitrate=224 -channels 2 -srate 48000 -o "$AUDIOLANG".avi

You should repeat the command for every audio track you want, so we will have .avi files with the audio track.

You may also want to use -channels 6 to exact all the channels of a 5.1 DVD or changing the bit rate. As with the video, you can use audio filters via -af but it is not necessary.

Making the final .mkv file

Putting it all together in a single file is simple. Add other audio tracks if needed:

mkvmerge -D audio.avi -A video.avi -o mymovie.mkv

The .mkv file will contain everything, so you can store your movie keeping all the audio track you want. Even if you are not interested in keeping multiple sound tracks, the H.264/Vorbis codec pair should ensure great quality.

Of course to make the work easier, you should write every command in a single file and execute it.

See Dvd2Avi for a sample script.

Personal tools