Playing HEVC/H.265 on Android with ffmpeg, libx265 and Vitamio

After testing out quite a few other peoples attempts at building and using ffmpeg on Android I settled on the Vitamio build (again as have done the same process before) as it offers the best mix of features and also has a reasonably active community (even if I have to use translate from Chinese!).

First download the latest builds of Vitamio-FfmpegVitamioBundle and OpenSSL-Vitamio (Use git clone for these).

Download and install the latest Android NDK

Note that I run OSX so these tips are for that though should be near identical for linux.

Change into the directory for OpenSSL-Vitamio and run ndk-build

cd OpenSSL-Vitamio

Now we need to make sure that environment variables are available to support our build for the build_android script and then run the build script

export SSL=/path/to/OpenSSL-Vitamio
export ANDROID_NDK=/path/to/ndkinstall

For some reason my build was failing for arm7 due to linking of the ssl libs, to fix this I had to remove the –enable-openssl from the ffmpeg setup options in

Stabilising / deshaking GoPro (or other) videos with ffmpeg and libvidstab

If anyone has shot on GoPro, particularly the helmet mounts you would be very familiar with the issue of camera shake. Users of YouTube may have also noticed that they have quite a nice stablisation filter.

Now thanks to Georg Martius you there is a great filter that you can include in your ffmpeg workflow for stablising video and it does quite a decent job

1. First download the source

git clone
cd vid.stab
cmake .
make install

2. configure you ffmpeg with –enable-libvidstab

./configure --prefix=/usr/local --enable-gpl --enable-nonfree --enable-libass --enable-libfdk-aac --enable-libfreetype --enable-libmp3lame --enable-libopus --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libxvid --enable-libvidstab
make install

3. Using libvidstab is a 2 pass process as first you need to detect the stabilisation issues, for example using default settings. Note on the first pass you don’t really need an output file as the real data get’s written to a special file

ffmpeg -i myvideo.mp4 -vf vidstabdetect output.mp4
ffmpeg -i myvideo.mp4 -vf vidstabtransform myvideo_stabilisted.mp4

There are of course a lot more options you can use and see those here:



Building ffmpeg with libx265 for h265/hevc encoding

ffmpeg now has x265 support and while it is still early days for the codec this is great news as there are also a number of players out there now too, not to mention many devices now have the CPU to play back the codec. Note that quite a lot of services now support h265/hevc input and as it is such a small footprint it can make quite a good file for transfer to cloud encoding.

Setup on OS X (likely be similar for linux and will do that at some point)

  1. Make sure you have cmake e.g. >brew install cmake and you will also need yasm
  2. You will also need mercurial to clone x265 >brew install mercurial
  3. Also if you already have ffmpeg installed using something like brew then uninstall that first >brew uninstall ffmpeg

Anyhow here are the simple steps:

1. Make sure you have a current build of ffmpeg checked out of git along with any other libs you are using e.g. libx264

2. Download the libx265 repository and build as per the instructions here: (note I assume no one still uses Windows for dev!)

hg clone
cd x265/build/linux
make install

3. On your ffmpeg configure it with –enable-libx265 (it is disabled by default). Sample from my configure below

./configure --prefix=/usr/local --enable-gpl --enable-nonfree --enable-libass --enable-libfdk-aac --enable-libfreetype --enable-libmp3lame --enable-libopus --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libxvid --enable-libvidstab --enable-libx265
make install

4. You should now be ready to go, e.g.

ffmpeg -i myvideo.MP4 -c:v libx265 encodetest/myvideo.mkv

Update: now with mp4 support

ffmpeg -i anchorman2-trailer-ffmpeg.mp4 -y -s 640x360 -c:v libx265 -c:a libfdk_aac -profile:a aac_he -b:v 200k -b:a 32k anchorman2_640x360_x265.mp4

In some cases you may get an error like:

Error while opening encoder for output stream #0:0 - maybe incorrect parameters such as bit_rate, rate, width or height

And you will see above it:

x265 [info]: using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX
x265 [error]: Sample Aspect Ratio width must be greater than 0

There is a bug at present (2014-04-08) that requires the SAR to be set in the header, note that you can fix this by doing a pre-encode and write the SAR / DAR header by forcing the aspect -aspect 16:9 e.g.

ffmpeg -i anchorman2-trailer.mp4 -y -c:v libx264 -c:a copy -aspect 16:9 -crf 0 anchorman2-trailer-ffmpeg.mp4

Some things to note:

  • Moderately slow! It is now much faster than it used to be and I am getting 17fps without tweaking which is pretty good
  • Playback of a 1080P HD clip used 400% on my i7 based laptop, size was
  • Compared to a 2000kbps x264 encode of the same file quality was very good!