Mencoder on OS X

Hi there, I wrote this brief guide in case you have the same problem as me - wanting to convert a load of videos you have in the VP7 (or some other weird format) to XViD.

Step 1 - Setting it all up

You need to install MacPorts and have it correctly setup.

Update MacPorts:

sudo port selfupdate

Step 2 - Installing System Codecs

I had issues just installing mplayer, so I just installed XViD, Mad, x264 and some other useful libraries this way:

sudo port install xvid +universal x264 +universal libmad +universal yasm +universal git

Note the +universal part - this is imperative.

Step 3 - Installing Binary Codecs

Download and install the binary codecs from MPlayer's website.

Step 4 - Installing MPlayer

Download the MPlayer Source Code and put it in a folder. For example, "src" in your home directory.

Open a terminal and change to it:

cd src

Now we need to configure the package, but passing special commands so it doesn't try and compile for 64-bit

./configure --extra-cflags=-m32 --extra-ldflags=-m32

This will take a minute, so grab a beer. Once it's done, check that win32 and xvid are in the supported codecs list. They should be, so type:

make

This will compile MPlayer which will take a while, so grab a couple of beers...

Finally, install it:

sudo make install

Step 5 - Testing mencoder

Test mencoder:

mencoder -ovc help

You should get an output like this:

MEncoder 1.0rc4-4.2.1 (C) 2000-2010 MPlayer Team

Available codecs:
   copy     - frame copy, without re-encoding. Doesn't work with filters.
   frameno  - special audio-only file for 3-pass encoding, see DOCS.
   raw      - uncompressed video. Use fourcc option to set format explicitly.
   nuv      - nuppel video
   lavc     - libavcodec codecs - best quality!
   vfw      - VfW DLLs, read DOCS/HTML/en/encoding-guide.html.
   qtvideo  - QuickTime DLLs, currently only SVQ1/3 are supported.
   xvid     - XviD encoding
   x264     - H.264 encoding

Make sure xvid and vfw are there.

Step 6 - Converting video

Finally you want to use mencoder to convert your video:

mencoder "input.avi" -oac copy -ovc xvid -xvidencopts pass=1 \
	-o /dev/null && mencoder "input.avi" -oac copy -ovc xvid \
	-xvidencopts pass=2:bitrate=950 -o "output.avi"

The way I did it is to have a folder called "conv", so to convert a Simpsons video I did:

mencoder "The Simpsons S08E01 Treehouse of Horror VII.avi" \
	-oac copy -ovc xvid -xvidencopts pass=1 -o /dev/null && \
	mencoder "The Simpsons S08E01 Treehouse of Horror VII.avi" -oac copy -ovc xvid \
	-xvidencopts pass=2:bitrate=950 -o "conv/The Simpsons S08E01 Treehouse of Horror VII.avi"

Step 7 - Automating

I wrote a small perl script to automate converting a folder:

opendir(DIR, '.');

print 'mkdir conv'."\n";

foreach (readdir(DIR)) {
        if (/\.avi$/) {
                $out = "Conv/".$_;
                if (!-e $out) {
                        print 'mencoder "'.$_.'" -oac copy -ovc xvid -xvidencopts pass=1 ';
			print '-o /dev/null && mencoder "'.$_.'" -oac copy -ovc xvid ';
			print '-xvidencopts pass=2:bitrate=950 -o "conv/'.$_.'"'."\n";
                }
        }
}

closedir(DIR);

To use it:

perl conv.pl>conv.sh
sh conv.sh

Alternatively, use a shell script like this:

mkdir conv
for i in *.avi; do mencoder "$i" -oac copy -ovc xvid -xvidencopts pass=1 \
	-o /dev/null && mencoder "input.avi" -oac copy -ovc xvid \
	-xvidencopts pass=2:bitrate=950 -o "conv/$i"; done

Just paste the above in to a shell and it should work.