This is a read-only archive. Find the latest Linux articles, documentation, and answers at the new!

Feature: Graphics & Multimedia

CLI Magic: Video conversion with mencoder

By Razvan T. Coloja on November 20, 2007 (4:00:00 PM)

Share    Print    Comments   

Mencoder is part of the MPlayer media player package. While MPlayer can play audio and video files, mencoder converts and manages multimedia files. The application has a ton of graphical user interfaces, but you can use it from the command line to produce video files in almost any format you want. Here's how.

 Among the file types mencoder can handle are MPEG/VOB, AVI, ASF/WMA/WMV, RM, QT/MOV/MP4, Ogg/OGM, MKV, VIVO, FLI, and FLV. The command syntax is straightforward despite a wealth of options. There's little mencoder can't do when it comes to multimedia conversion.

Here's a simple command that converts an MPG file to AVI format:

mencoder file.mpg -o file.avi -ovc lavc -oac lavc 
-ovc and -oac represent the options for the video and audio codecs that mencoder will use. To find out what video codecs are installed on your system, use mencoder -ovc help and mencoder -oac help.


Suppose you need a file with no compression on the audio part and decide to use PCM. You can specify the type of audio codec you want by using the acodec option:

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

When it comes to MP3 compression, you can also choose a bitrate using abitrate:

mencoder file.mpg -o file.avi -ovc lavc -oac lavc -lavcopts acodec=libmp3lame:abitrate=128 

You can use lameopts if you have libmp3lame installed and want to add extra options to the encoding process. You can also create files with variable bit rate audio compression:

mencoder file -o file.avi -ovc lavc -oac mp3lame -lameopts vbr=2:q=3 

where q can be any number between 0 and 9.

You can do the same thing with the video part of the file:

mencoder file.mpg -o file.avi -ovc lavc -oac lavc -lavcopts acodec=libmp3lame:abitrate=128 vcodec=xvid 

If you don't want to use video compression, try vcodec=copy. With that option, the frames will be copied one by one from the source file.

You can use xvid or divx directly, without going through lavc:

mencoder -ovc xvid -oac mp3lame -o destination.avi source.avi 

If you need customized quality, you can add a few options to the XviD compression:

mencoder -ovc xvid -oac mp3lame -xvidencopts bitrate=878 -o destination.avi source.avi 

The higher the bitrate, the better quality the video file will be. The downside is a larger file size.

Now let's get fancy and make an XviD copy of a DVD using two passes. During the first pass, mencoder analyzes the content of the file; on the second pass mencoder encodes the new file based on the information obtained. By using two passes you can produce a better compressed file, but you'll have to wait a little longer for it, and you'll probably see CPU usage at 90% during the conversion:

mencoder dvd:// -oac mp3lame -ovc xvid -xvidencopts pass=1 -o /dev/null
mencoder dvd:// -oac mp3lame -ovc xvid -xvidencopts pass=2:bitrate=800 -o xvidfile.avi

You can use whatever bitrate option you want. If you need to squeeze a DVD into a 700MB XviD file, you could use the following command, which forces the file size of the resulting AVI to 700MB.:

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

If you don't like the CPU being used to the max and want to leave resources for launching other applications, use the nice option, which will run the program with the lowest priority when it comes to process scheduling:

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

Suppose you have a folder full of small video files of different types and would like to merge them into one big movie for easy watching. First, rename them so that they're in the order you want them to appear in the final video, then use:

mencoder * -o output.avi

If you want to add a particular audio file to a movie, use:

mencoder source.avi -o destination.avi -ovc copy -oac mp3lame -audiofile file.wav (for uncompressed files)
mencoder source.avi -o destination.avi -ovc copy -oac copy -audiofile file.mp3 (for compressed files)

To convert a video file to run on a device running iPodLinux, use:

mencoder -ovc raw -ofps 15 -oac pcm -vf scale=176:-2,expand=176:132,format=bgr16 input.file -o output.avi

This produces a RAW AVI file with uncompressed audio data and scales it so it fits the Nano's tiny screen perfectly.

I have a Pocket PC that I sometimes bring with me on business trips. I take a couple of movies I haven't seen in a while and convert them to fit on a 512MB SD card:

mencoder -oac mp3lame -lameopts mode=3:preset=24 -ovc lavc -lavcopts vcodec=mpeg4:vhq:vbitrate=384:keyint=250 -vop expand="320:240" -o outputfile.avi inputfile.avi


mencoder input.avi -ovc lavc -lavcopts vcodec=mpeg4:vbitrate=200:abitrate=48 -vop scale=320:240 -oac copy -o output.avi

The difference here is that the latter command scales the file and the former fills the PDA's 320x240-pixel screen with the movie.

If you have a webcam and want to record the output, use:

mencoder tv:// -tv driver=v4l:device=/dev/video0:width=640:height=480:forceaudio -ovc lavc -oac lavc -lavcopts vcodec=mpeg4:acodec=mp3 -ffourcc divx -o test.avi

The command records anything output by /dev/video0 in 640x480 resolution, using DivX with MP3 audio as an output result.

As you can see, you can use mencoder to convert almost any type of video file in several ways. It works fast, it works well, and I wouldn't change it for any other application, be it GUI-friendly or not.

However, if you feel you need a graphical interface to convert videos, I suggest one of the following: KMencoder, Konverter, Kmenc15, GMencoder, AcidRip or MenGUI.

Razvan T. Coloja has published more than 150 Linux and IT-related articles in print and online magazines. He is an editor for a Romanian magazine and one of the maintainers and editors of, a Romanian Linux/OSS portal and community.

Share    Print    Comments   


on CLI Magic: Video conversion with mencoder

Note: Comments are owned by the poster. We are not responsible for their content.

Thank you so much!

Posted by: Anonymous [ip:] on November 20, 2007 05:24 PM
I hate to read through the mencoder/mplayer man pages for simple fixes. Thanks!



Posted by: Anonymous [ip:] on November 20, 2007 06:01 PM
The command line is NOT magic. Anyone who views or characterizes the command line as magic needs to look for another way of introducing their article/man page abstract.


Re: NOT CLI Magic

Posted by: Anonymous [ip:] on November 20, 2007 07:46 PM
Jeez... buddy up your anti-depressant


Re: NOT CLI Magic

Posted by: Anonymous [ip:] on November 20, 2007 08:25 PM
"CLI Magic" is actually the title of a series of articles related to the command line. That's several years they appears on, and if i remember correctly they were introduced by Joe Barr. Assuming that "CLI Magic" is the title of the article, and that the command line is kind of magic, would be like suggesting the author should introduce a way for the reader to check the man page... plain silly.


Re: NOT CLI Magic

Posted by: Anonymous [ip:] on November 21, 2007 03:38 AM
Thank you oh ye masterful font of all knowledge and understanding for enlighten us poor inferior fools that enjoy reading article that explore the totally unnecessary command line interfaces to various evil Linux programs. May the father in Redmond forgive us our transgressions.


CLI Magic: Video conversion with mencoder

Posted by: Anonymous [ip:] on November 20, 2007 06:11 PM
Has anyone had any success with AcidRip lately? I used it ALOT for several years and now it seems perpetually broken. Research into the problem indicates (according to one forum) that AcidRip is passing an extra colon to the command line but I have no idea how to fix this.

I really like the a/v software as I prefer to watch my video when and where I want such as on my PalmPilot during lunch or stripping out all the previews and menus so we can watch the movie right NOW. After owning a DVD for a few months I don't want to be forced to watch the FBI warnings, previews, and whatever another time... Oh - and the DVD player button lockout so I can't skip these features is REALLY annoying. Best of all the kids are using a copy of the $20 while the original is safely put away.

Thanks Linux supporters and programmers!!!


CLI Magic: Video conversion with mencoder

Posted by: Anonymous [ip:] on November 20, 2007 07:04 PM
I've been looking for samples to do video conversions etc; this is a life saver and definitely a keeper.


Re: CLI Magic: Video conversion with mencoder

Posted by: Anonymous [ip:] on November 20, 2007 10:03 PM
There's a lot of examples in the official Mplayer/Mencoder documentation which come with the sources, or packaged by your distribution of choice (and many of the ones described in the article are blatantly taken from there too ;)).


CLI Magic: Video conversion with mencoder

Posted by: Anonymous [ip:] on November 21, 2007 04:15 AM
Cool article. Thanks!


I would add one thing though...

Posted by: Anonymous [ip:] on November 21, 2007 05:42 AM
Mencoder is brilliant and has been my video mangler of choice for a long time now. There are very rare cases, though, when it falls short and for me, in every one of those cases using the ffmpeg commandline util has filled the gap. Sure, each can use the other, but sometimes it is like using a hammer to turn a screwdriver and just putting the hammer down and grabbing the screwdriver makes it all so simple.

Mencoder's only "weakness" is its flexibility. That combined with the idiosyncrasies of several video formats means you can spend a while trying different approaches to a problem before you hit the one that works best - you certainly learn a bit about digital video along the way...

Regardless, with both in your toolbox, there is not a single video format, source or destination, that you cannot well and truly pwn.


Re: I would add one thing though...

Posted by: Anonymous [ip:] on November 24, 2007 08:50 PM
I second this. Furthermore, I try ffmpeg first.


vcodec=copy wrong!

Posted by: Anonymous [ip:] on November 21, 2007 09:41 AM
it's '-ovc copy', not 'vcodec=copy'. also, on some systems '-ovc xvid' might not be available. first try 'mencoder -ovc help' to see which video codecs are available. it's also works for audio codecs, '-oac help'.


CLI Magic: Video conversion with mencoder

Posted by: Anonymous [ip:] on November 26, 2007 10:32 AM
Great article, thanks. Too bad the css eats the nd of each command line (at least on my screen).


This story has been archived. Comments can no longer be posted.

Tableless layout Validate XHTML 1.0 Strict Validate CSS Powered by Xaraya