Saturday, July 10, 2010

Encoding videos for Apple iPad/iPod, using mencoder

After much searching, poking, iterating, I found a magical mencoder command-line that encodes videos in the right format for the ipad. Debugging this is hard because, if you get something wrong, iTunes simply silently fails to open the video file, giving no specific feedback as to what's wrong. I felt like a monkey on a typewriter...

So here are the options, broken by group. Set the output format to MP4:

-of lavf -lavfopts format=mp4

Scale the video to width 1280, height to match the aspect ratio (if your source video is low resolution, eg DVD, leave this off; you can also crop (-vf crop=W:H:X:Y; run mplayer -vf cropdetect,scale to have it guess the crop for you), or de-interlance (I like -vf pp=lb), or any other video filter):

-vf scale=1280:-3

Use x264 codec for video, with a zillion options (tweak that crf=XX if you want higher quality; lower values of XX give higher quality, but take more space):

-ovc x264 -x264encopts crf=28:vbv_maxrate=1500:nocabac:global_header:frameref=3:threads=auto:bframes=0:subq=6:mixed-refs=0:weightb=0:8x8dct=1:me=umh:partitions=all:qp_step=4:qcomp=0.7:trellis=1:direct_pred=auto

Use faac codec for audio:

-oac faac -faacopts br=160:mpeg=4:object=2:raw -channels 2 -srate 48000

I'm sure many other command-line combinations would work; this was just the first combo I found that works.

If you need to encode for the iPod Touch, just change the -vf scale=1280:-3 to -vf scale=640:-3 (at least this works for recent iPod Touch models).

You can also use the nifty mp4art tool to insert cover art after the encode finishes; this lets you set the image for the movie when browsing on the iPad/iPod or in iTunes, which is vital if young kids will be launching the movies! I use PNG images, which work fine.

4 comments:

  1. Have you tried Handbrake?

    http://trac.handbrake.fr/wiki/CLIGuide

    ReplyDelete
  2. Shalin: I do like Handbrake -- both the GUI and CLI are nice. And, right out-of-the-box it encodes just fine for iPad/iPod/iPhone.

    I had started down the path of using it, but I'm much more comfortable with mencoder's command-line, and I also didn't like the deinterlace/decomb results with Handbrake (I like mencoder's simple linear -vf pp=lb). Of course, internally they are both using the same core software to do the encode, so it's really a case of "use whichever you're more comfortable with".

    ReplyDelete
  3. Thanks. This post helped me a lot! Now I can go direct from DVD to an mp4 that I can sync with iPad. I had to use the deinterlace option. I made a few other slight tweaks. Here is my complete command line:

    mencoder dvd://1 -o "[your filename].mp4" -of lavf -lavfopts format=mp4 -vf pp=lb,crop=720:480:0:0 -ovc x264 -ovc x264 -x264encopts crf=30:vbv_maxrate=2500:nocabac:global_header:frameref=3:threads=auto:bframes=0:subq=6:mixed-refs=0:weightb=0:8x8dct=1:me=umh:partitions=all:qp_step=4:qcomp=0.7:trellis=1:direct_pred=auto -oac faac -faacopts br=128:mpeg=4:object=2:raw -channels 2 -srate 48000

    ReplyDelete
  4. Hi, thanks a lot. I wouldn't have been patient enough to figure it out :)

    ReplyDelete