So I bought myself a nice Android phone in april. It’s a HTC Desire, a real “tweakers’ phone”. It runs Android 2.1 – and before the end of the year, HTC should have an update to Android 2.2 available.

It is an impressive piece of work, and when I compare it to the iPhone or Windows Mobile based phones some of my friends/collegues carry with them, it clearly gives me a lot of freedom – I do not have any desire to “root” or “jailbreak” my phone in order to make it do what I want. I dream of putting Slackware on it, sometimes, but then I usually wake up fast… It’s a real Linux phone (with a SSH and a VNC client installed from the Android Market!), and the HTC Sense interface makes it just perfect and enjoyable as it is.

It goes without saying that I took a flat-fee Internet subscription; this kind of phone is not fully functional unless it is always online. I have been running a bandwidth metering app which told me that I transfered 440 MB in my first month. That would result in a pretty heavy bill if I did not have unlimited traffic.

I may write some more about my experiences with the Desire in future posts (so may exciting things to tell!), but for this one I want to talk about video playback on Android. Android has decent multimedia support out of the box, and audio as well as video players can make use of the standard codec libraries that the Android platform offers.

The “native format” I think you may call it that, for the Android seems to be H.264 video and AAC audio in a MP4 container. There are some limitations however, as people found out when trying unsuccessfully to play MP4  video files on their Android device that would not have had playback issues on a normal PC or mediabox.

Android only supports the H.264 baseline profile, meaning that some of the fancy encoding tricks that give you great video quality at lower bitrates (but require more processing power on playback) can not be used for videos you want to play on your Android device.

And rest assured, you do want to watch videos on the 800 x 480 pixel WVGA screen of the Desire! The AMOLED display has fantastic vibrant colours.

So, in order to create a video file that fits the display dimensions perfectly (without the scaling/resizing which would result in an ugly picture) and contains fully compatible MP4 video, I once again turned to my faithful companion… ffmpeg.

Here is a ffmpeg command line for you. This will take a video file (any supported format will work as input) and produce an Android-compliant MP4 file with WVGA dimensions (800 by 480 pixels). The input file will be transcoded in a two-pass process in order to achieve the best possible results. The transcoding will take a long time, so find something else to do in the meantime.

$ ffmpeg -i [inputfile] -threads 0 -vcodec libx264 \
    -vpre slow_firstpass -vpre baseline -b 480k -r 13 \
    -acodec aac -ab 128k -sameq \
    -pass 1 -f rawvideo -an -y /dev/null && \
    ffmpeg -i [inputfile] -threads 0 -vcodec libx264 \
    -vpre slow -vpre baseline -b 480k -r 13 \
    -acodec aac -ab 128k -ac 2 -sameq -pass 2 [outputfile].mp4

If you are on Slackware, then you get lucky. My ffmpeg packages (the variant that supports AAC audio encoding) are right here: http://slackware.org.uk/people/alien/restricted_slackbuilds/ffmpeg/

Have fun experimenting!

Eric