Multimedia Mean Machine
Part One: Mencoder

by Paul Arnote

You may not realize it, but when it comes to converting multimedia formats, you are sitting on a couple of powder kegs. They have the potential to make your multimedia conversion projects quick and simple — once you understand how to use the tools. These programs go by the names mencoder and ffmpeg. In this article, we will focus on learning how to use mencoder. We'll take a closer look at ffmpeg in a later article.

Mencoder is in the PCLinuxOS repository. Related to Mplayer, mencoder is the tool to encode multimedia files. In fact, both programs share the same online documentation at:

Mencoder is so powerful, and has so many options, that we will only be able to explore some of the more common and more frequently used options. If you want more information, I humbly refer you to either the web site mentioned above, or to the extensive man pages for Mplayer, which contain the information for mencoder.

As much as most users dislike the command line, mencoder is a command line utility. Face it — sometimes, the command line is the fastest and easiest way to get something done. Granted, someone probably could create a GUI for mencoder, but it definitely would be difficult, given the wide variety of options available for mencoder. Several attempts have been made. Some such attempts (that are in the PCLinuxOS repository) are: AcidRip, Kmenc15, Kalva, Kmediagrab, and rm-ogv-flv-to-avi.

You will find that there is little that mencoder cannot do. If you have the codec installed on your system, or can play the codec back on your system, chances are high that mencoder can convert between those installed formats.

To find out which video codecs mencoder supports on your system, enter:

mencoder -ovc help

It's easiest to think of the -ovc command line switch as "output video codec." The above command will list out all of the video codecs supported on your system.

Similarly, to find out which audio codecs mencoder supports on your system, enter:

mencoder -oac help

Much as its video counterpart, the above command is easiest to remember as "output audio codec," and will list out all of the audio codecs supported on your system.

Once you start to see a few command line examples, you should start to see the pattern, along with all the possibilities. For example, to convert an AVI file to FLV, you can use the following command:

mencoder -forceidx -of lavf -oac mp3lame -lameopts abr:br=56 -srate 22050 -ovc lavc -lavcopts vcodec=flv:vbitrate=250:mbd=2:mv0:trell:v4mv:cbp:last_pred=3 -vf scale=360:240 -o /path/to/output.flv /path/to/source.avi


forces the rebuilding of the index of the file.

-of lavf

specifies the output format to be encoded with the libavformat muxers (combining audio and video).

-oac mp3lame -lameopts abr:br=56 -srate 22050

specifies for the audio to be formatted as 56 kbps MP3, at a 22050 sample rate.

-ovc lavc -lavcopts vcodec=flv:vbitrate=250:mbd=2:mv0:trell:v4mv:cbp:last_pred=3 -vf scale=360:240

tells mencoder to encode the video using the libavcodec codecs, to specifically use the flv video format, with a video bitrate of 250 kbps, using macroblock decision algorithm 2 to produce the best rate distortion, to encode each macroblock of the video with MV=(0,0) and to use the better one, to find the optimal encoding for each 8x8 block, allowing 4 motion vectors per macroblock for better quality, to find the optimal coded block pattern to minimize distortion, the amount of motion predictors from the previous frame, and to scale the video to 360 x 240 pixels.

-o /path/to/output.flv /path/to/source.avi

tells mencoder to produce the specified output file from the specified source file.

See what I mean about mencoder having so many options? Don't worry if you don't completely understand all the options. The more you work with multimedia files, the more these things will make sense. In a way, it's like learning a new language. All of the options in the above command are explained in the Mplayer/mencoder man pages, and at the web site mentioned near the beginning of the article.

To convert an MPG file to an AVI file, try this short and simple command:

mencoder /path/to/file.mpg -o /path/to/file.avi -ovc lavc -oac lavc

You can expand the above command to include the type of audio codec you would like to use in the conversion process. The command:

mencoder /path/to/file.mpg -o /path/to/file.avi -ovc lavc -oac lavc -lavcopts acodec=pcm

will create the audio portion of the AVI file in uncompressed PCM format. Similarly, the command:

mencoder /path/to/file.mpg -o /path/to/file.avi -ovc lavc -oac lavc -lavcopts acodec=libmp3lame abitrate=192

will create the audio portion of the AVI file as MP3 audio at 192 kbps bit rate. If you use libmp3lame, you can also use -lameopts to specify additional options to use in the MP3 conversion process. The command:

mencoder /path/to/file.mpg -o /path/to/file.avi -ovc lavc -oac mp3lame -lameopts vbr=2:q=5

will create the audio track of the AVI file as variable rate MP3 file. The "q" parameter can be any value between 0 and 9.

Just as you can specify the audio codec to use in the finished file, you can also do the same thing with the video codec. The command:

mencoder /path/to/file.mpg -o /path/to/file.avi -ovc lavc -oac lavc -lavcopts acodec=libmp3lame:abitrate=160 vcodec=xvid

to compress the MPG file into an XVID AVI file, with MP3 audio at 160 kbps.

You can, instead of going through lavc (libavcodec), use XVID directly. To create an XVID file without going through lavc, use the following command:

mencoder -ovc xvid -oac mp3lame -o /path/to/output.avi /path/to/source.avi

Since we are addressing XVID directly, via mencoder, we can also spedify other XVID options. For example, to specify a specific bit rate for our XVID AVI file, we can issue the following command:

mencoder -ovc xvid -oac mp3lame -xvidencopts bitrate=700 -o /path/to/output.avi /path/to/source.avi

The above command will create an XVID encoded AVI file, with the video bit rate set to 700 kbps. Naturally, the higher the bit rate, the better the quality of the video. But the trade-off for using a higher bit rate is that it also results in a larger file size.

You can, however, maintain higher quality while using lower bit rates (and a smaller file size) by using two-pass encoding. With two-pass encoding, the source file is scanned to analyze the content, and that information is written to a file. On the second pass, the video undergoes the actual encoding, using the information gleaned from the analysis of the file on the first pass. To do a two-pass XVID encoding of an AVI file, use the following command:

mencoder /path/to/source.avi -oac mp3lame -lameopts abr:br=128 -ovc xvid -xvidencopts pass=2:bitrate=550 -o /path/to/output.avi

Mencoder also allows you to transcode video directly from an input source, like a DVD. Use the following commands to create an 800 kbps XVID encoded AVI file:

mencoder dvd:// -oac mp3lame -ovc xvid -xvidencopts pass=1 -o /dev/null

mencoder dvd:// -oac mp3lame -ovc xvid -xvidencopts pass=2:bitrate=800 -o /path/to/output.avi

Adjust the video bit rate to your liking. Again, higher bit rates result in larger file sizes, but also with higher quality video. And a video bit rate of 800 kbps will give you fairly good quality video.

So would you like to create an XVID encoded AVI file, from a DVD, that will fit onto a 700 MB CD? Use the following command:

mencoder dvd:// -ovc xvid -oac mp3lame -xvidencopts bitrate=-700000 -o /path/to/output.avi

Setting the bit rate to a negative number (in this case, -700000) allows XVID to set the video and audio bit rates to fit within the size constraint specified.

If you choose to do any of the previous three video translation scenarios, be prepared to go have dinner or something. Either of them will require a fairly high CPU load, and you will not be able to use your computer for much else while the video is being converted. Of course, you can make mencoder behave "nicer" with the rest of your system, so you may be able to at least read your emails or browse the web while the conversion is underway. You can do that like this:

nice -n 19 mencoder dvd:// -ovc xvid -oac mp3lame -xvidencopts bitrate=-700000 -o /path/to/output.avi

Maybe you have a video that you want to add background music to. Enter this command:

mencoder -ovc copy -audiofile /path/to/the/mp3file.mp3 -oac copy /path/to/the/source_video.avi -o /path/to/the/output_video.avi

Or, perhaps you have a video file that you would like to resize. As we mentioned earlier, the higher the video bit rate, the larger the file. Well, another thing that contributes to the video file size is the size of the video image. In the following example, we can resize the video to 320 x 240.

mencoder /path/to/the/source_video.avi -ovc lavc -lavcopts vcodec=mpeg4:mbd=2:trell -oac copy -vf scale=320:240 -o /path/to/the/output_video.avi

Sometimes we have two (or more) video files we would like to join together. You can issue the following command:

mencoder -forceidx -ovc copy -oac copy -o /path/to/the/output_video.avi /path/to/the/source_video_part_1.avi /path/to/the/source_video_part_2.avi

You may also wish to only make a clip of a portion of a video. Again, mencoder comes to your aid. Use the command below, replacing the starting point (ss) and ending point (-endPos) in the command below with the values that work for your particular project.

mencoder -oac copy -ovc copy -ss 0 -endPos 54 /path/to/the/source_video.avi -o /path/to/the/output_video.avi

Did you receive a portable digital movie player as a gift, but it won't play Ogg/Theora video? Again, mencoder can come to your aid. Issue the command below to convert your OGV file into an AVI file that you can play on your movie player:

mencoder ./out-1.ogv -o ./file.avi -ovc lavc -oac pcm#x0d;

I'm sure you've noticed many videos on YouTube that have background music, as well as many older videos of live performances by your favorite band or bands. You can easily get an MP3 copy of the audio, simply by issuing this command:

mplayer -dumpaudio name-of-file.flv -dumpfile name-of-file.mp3

Now I know this situation has happened to everyone reading this article: you sit down to watch something on TV (it always happens when it's something you've really been wanting to see), and you start feeling sleepy. You just know that there is no way you are going to be able to stay awake to watch the rest of the television program. Once again, mencoder can come to your rescue. By issuing the following command, mencoder should record from your TV tuner card, and continue to record for two hours (sorry, but I don't have a working TV card to test this with):

mencoder tv:// -v -tv drive r=v4l:width =720:height=576:input=1:device =/dev/video0:immediate mode =0:/alsa:adevice=hw.0,0:outfmt=yv12 -o output.avi -af volume=10:0 -ovc lavc -lavcopts vcodec=mjpeg:aspect=4/3 -aspect 4:3 -oac pcm -endpos 02:00:00

PCLinuxOS Magazine

As you can see, the possibilities with mencoder are virtually endless. And, as you are likely to find out, there are usually multiple ways to achieve your goal. Mencoder truly is a powerful tool for your multimedia arsenal. In a later article, we'll take a look at the other powerful multimedia tool on your system: ffmpeg.