2010年4月21日星期三

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
-vf scale=480:-10,harddup
-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
Here's what I'm currently using, for a bit higher quality:
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
Other things I do, to come in a future blog post:
Two-pass encoding
Different settings for progressive DVD content, telecined DVD content, interlaced content, etc.
Manage this all via a python script
Thanks to Chris Prince for his encoding settings, which helped me find my original braindead mistake of poor scaling target size.

http://blog.jharding.org/2008/05/encoding-video-for-iphone-with-mencoder.html

Im trying to come up with a high quality Iphone 3G profile, without regard for any other apple devices, taking the most the Iphone can handle.

I see the Iphone 3G is supposed to support:

Baseline Profile Level 3.0
10 MBit/s, 10 MBits buffer
720x480 at 30fps (6 stored)
720x576 at 25fps (5 stored)
Restrictions:
No B-slices
No CABAC
No pixel aspect ratio support
No interlaced
For iTunes sync, average bitrate over whole file must be < 5 MBit/s (iPod classic/nano, not touch/iphone

I have been studying the parameters that x.264 allows. As a test piece Im using the original pngs from Big Buck Bunny - Apricot movie. I have come up with the following:

mencoder mf://*.png -mf w=1920:h=1080:fps=24:type=png \
-vf scale=720:400,harddup -of rawvideo -ovc x264 -o test1.264 -ofps 24 \
-x264encopts level=30:no-cabac:bframes=0:trellis=0:ref=5:mixed-refs:no-fast-pskip:bime:vbv-maxrate=10000:\
vbv-bufsize=10000:me=umh:merange=24:subme=7:partitions=all:direct=auto:threads=auto:deblock:keyint=240:bitrate=5000:\
pass=1 -nosound -endpos 100

mencoder mf://*.png -mf w=1920:h=1080:fps=24:type=png \
-vf scale=720:400,harddup -of rawvideo -ovc x264 -o test1.264 -ofps 24 \
-x264encopts level=30:no-cabac:bframes=0:trellis=0:ref=5:mixed-refs:no-fast-pskip:bime:vbv-maxrate=10000:\
vbv-bufsize=10000:me=umh:merange=24:subme=7:partitions=all:direct=auto:threads=auto:deblock:keyint=240:bitrate=5000:\
pass=2 -nosound -endpos 100

2 条评论:

  1. inbeV7 llzfnbdxfvjm, [url=http://ydicnflggavd.com/]ydicnflggavd[/url], [link=http://ofvavdqrunad.com/]ofvavdqrunad[/link], http://fzxibrsnsonw.com/

    回复删除