Re-encoding video for Android

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

10 thoughts on “Re-encoding video for Android

  1. Wow,

    a week ago I just got a Nexus One. Before that I had iphone 3G and I completely agree with your thoughts. Android is much more friendly and doesn’t require any magic like root, jailbreak, snowbreeze, or etc. And it is much more powerful device. (I already upgraded mine to unofficial 2.2 froyo, I had to do this mostly because of russian layout absence in 2.1. It is indeed much faster). I played with your handbrake package to convert videos for my device. But I couldn’t figure right dimensions. Now it is very simple. Thanks a lot once again.


  2. ” I dream of putting Slackware on it, sometimes,…” It would be interesting to see how that works out 🙂


  3. Pingback: Tweets that mention Alien Pastures » Re-encoding video for Android -- Topsy.com

  4. Good choice, I have the android htc magic and I have been very happy with it – I modded it with the excellent cyanogen mod – watch xda forums for new community roms for your device. Thanks for all your work on slackware Alien- you are one reason its the best!


  5. Hi,
    For some days I looking for a solution for the encoding.
    I use pulse phone (android eclair), and this ffmeg encoding is work well! Thank you!


  6. Wondering if you’re annoyed already by the way in which HTC decides to release firmware updates for their devices and details about what exactly they are updating and if you’ve found any way around it. 🙂


  7. @comp_ –

    No, why would I be annoyed? I have an unbranded HTC Desire, and I received my first Over-The-Air update in june, and the second one last weekend got me Android 2.2.

    So, actually I am very pleased with the support for this phone by HTC.

    I have witnessed first-hand how providers mess with their customers by offering branded phones. Vodafone in the Netherlands pushed an update of their own containing nothing but their “vodafone 360” junk software … but no Android 2.2. And T-Mobile customers have to wait weeks until an upgrade for their branded phone is released. And who know what kind of spam that update will get?

    I watched how a friend of mine loaded a generic Desire Android image on to his T-Mobile branded phone. Then we proceeded to download and install the Android 2.2 update using the phone’s recovery mode… a really easy process, no rooting required, which would not be possible if this would have been a Windows-powered phone.

    Eric





Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.