Bold dream
Imagination is limitless. So is stupidity.

Converting video for Android

July 15th 2009 in Android

A few weeks ago I got myself a HTC Magic – an Android based mobile device that, among many things, has a phone in it. But not the phone is the part that I’m going to talk about – it’s the video player.

According to the android spec only h.263, h.264 and MPEG-4 SP are supported. It seems that Xvid or whatever the most videos on the net are encoded with is unsupported (I tested it). Also, the optimal size of the video is 480×352. What I wanted was the ability to watch clips and movies on the phone so I set on a mission to convert a simple .avi file to the needed .mp4 h.264 format.

The first try was ffmpeg… FAIL! The ffmpeg shipped with Ubuntu doesn’t seem to be a proper one, some codecs are missing and the overall complexity of the ffmpeg turned me away. I started looking for something else…

…and I found HandBrake. Nice little application that comes even with a GUI. Superb… Or so I though. I tried using the GUI, but it kept crashing after a few minutes of progress. Then I toyed around with the command line version. After a bit of digging around the Web and the help pages of the app I managed to come up with a script to do all of the work. Here it is:

HandBrakeCLI \
    --input "$1" --output "$2" --format mp4 --encoder x264 \
    --x264opts level=30:bframes=0:cabac=0:ref=1:vbv-maxrate=1500:vbv-bufsize=2000:analyse=all:me=umh:no-fast-pskip=1 \
    --vb 320 --rate 23.976 --aencoder faac \
    --mixdown stereo --ab 96 --arate 44100 \
    --maxHeight 352 --maxWidth 480 --two-pass

As you can see I’m using the verbose version of the different options, because I believe that no matter how large the program is – 3 lines or 3M lines – it should be readable.

Usage of the script is simple as:

./encode input.avi output.mp4

What it does is that it will convert the video to mp4, encoded to h.264 with a few options that will make the android device play it smoothly, and it will resize the video for optimal viewing, preserving the aspect ratio.

Oh, and it’s multithreaded as well, so it will take advantage of all the cores/CPUs available.

If you can think of an improvement – please let me know in the comments. Hope it will help someone.




required



required - won't be displayed


Your Comment:

In a recent talk at QCon labeled Null References: The Billion Dollar Mistake1, Sir Charles Antony Richard Hoare himself – the inventor of Null (and QuickSort, and many other things that shaped our industry) states that Null was/is a bad idea.

What is Null?

Here is an explanation from wikipedia.

Null is a special pointer value (or other [...]

Previous Entry

A few days ago I found a nice ringtone for SMS notifications, but couldn’t find a way to use it on my HTC Magic (except using a 3rd party app from the store). After a bit of digging I got it:

On the SD card there must be a directory structure as follows:

sdcard
⌊media
[...]

Next Entry

Blogroll