Sunday, 1 August 2010

Encoding x264 Video for the iPod Touch with mencoder

Update, 18-Aug-2012: I've changed to using an ffmpeg/mplayer combo to do this, as it's much more reliable and stable. See my new post for a breakdown.

I tend to view a lot of video content on my first-generation iPod Touch. It's great for travelling, with its these-days-paltry 16GB capacity able to store well over a dozen half hour episodes of whatever I'm following at the time.

While there are some wonderful graphical encoders out there (HandBrake springs to mind), what I was looking for was something I could script. I know that HandBrake has a command line interface, but I prefer mencoder's support for embedded subtitle streams, which is something I use often.

If you're on a fairly recent SVN of mplayer (say >= r31363), you'll find that mencoder now supports a profile in x264encopts. This is important! The default is high, which the iPod explicitly doesn't support. What you need is baseline, leading to something like this:

 -vf scale=480:-10,harddup# resize the video to whichever height is suitable for the max 480px width
 -sws 9# Choose a really good scaler (lanczos)
 -of lavf# use lavf for output
 -lavfopts format=mp4# specifically, mp4
 -oac faac# AAC audio for output
 -faacopts mpeg=4:object=2:raw:br=128# Audio coding parmeters
 -mc 0 -noskip# Really work at keeping A/V sync
 -ovc x264# x264 video
 -x264encopts nocabac: bframes=0: level_idc=30: global_header: threads=auto: subq=5: frameref=6: partitions=all: trellis=1: chroma_me: me=umh: bitrate=768: profile=baselineVideo coding parameters. Note the baseline
 -aid 0 -sid 0# Audio and subtitle tracks
 -subfont-text-scale 4# Better subtitle scaling for an iPod sized device

(or, for a more cut n' paste friendly version:

mencoder INPUT-FILE -o OUTPUT-FILE -vf scale=480:-10,harddup -sws 9 -of lavf -lavfopts format=mp4 -oac faac -faacopts mpeg=4:object=2:raw:br=128 -mc 0 -noskip -ovc x264 -x264encopts nocabac:level_idc=30:bframes=0:global_header:threads=auto:subq=5:frameref=6:partitions=all:trellis=1:chroma_me:me=umh:bitrate=768:profile=baseline -aid 0 -sid 0 -subfont-text-scale 4)

This transcodes the source in a single pass. Lots of people strongly advocate two-pass encoding, but really, I find the quality of a single pass encode good enough for the iPod screen. There's no denying that two-pass is higher quality, but the extra time to encode disposable content just isn't worth it in my opinion.

1 comment:

  1. Thank you, this is the first mencoder command line for ipod encoding that really worked for me.
    And believe me, I've tried a lot from different blog entries.