Encoding video for iPhone with mencoder
It's surprising how hard it is to find good, solid information about mencoder settings for the iPhone. I finally got a decent script that takes care of a few common scenarios I care about, and figured I'd share.
First, you need to get a decent build of mencoder. If you're working on linux, head over to the mplayer download page, grab the source, and build your own. I'm doing this on Windows, so I grabbed the binaries from sourceforge (svn rev 26446), since I didn't feel like monkeying with msys.
Now, let me start you with something that should definitely work, and then we'll talk about customizing:
mencoder INPUT -o output.mp4
-oac faac -faacopts mpeg=4:object=2:raw:br=128
-of lavf -lavfopts format=mp4
-ovc x264 -x264encopts nocabac:level_idc=30:bframes=0
Breaking it down:
- -vf scale=480:-10 - Scale width to 480, set height appropriately, but keep a multiple of 16.
- -vf harddup - Insert duplicate frames to maintain frame rate (Note: not sure if this is really needed - can the iPhone handle variable framerate?)
- -x264encopts nocabac - iPhone only supports baseline profile, which does not allow CABAC
- -x264encopts level_idc=30 - iPhone only supports up to level 3.0
- -x264encopts bframes=0 - Baseline profile does not allow B-frames
mencoder.exe input.avi -o output.mp4 -vf scale=480:-10,harddup -lavfopts format=mp4 -faacopts mpeg=4:object=2:raw:br=128 -oac faac -ovc x264 -sws 9 -x264encopts nocabac:level_idc=30:bframes=0:global_header:threads=auto:subq=5:frameref=6:partitions=all:trellis=1:chroma_me:me=umh:bitrate=500 -of lavf
The extra options:
- -sws 9 - I think I just grabbed this from somewhere - wtf?
- global_header - Uses single global header instead of repeating every IDR - saves space
- threads=auto - Enables multi-threaded encoding (faster, gives up a touch of quality)
- subq=5 - Quarterpixel motion estimation
- framref=6 - Use up to 6 reference frames
- partitions=all - Consider all macroblock partition sizes
- Two-pass encoding
- Different settings for progressive DVD content, telecined DVD content, interlaced content, etc.
- Manage this all via a python script