Cross Compiling FFmpeg with x264 for Android

Recently we were working on a feature where we had to combine an image and audio to create a video on mobile devices. In iOS this can be done using AVAssetExportSessionthough – for detail see this link However, we could not find any native solution for this problem in Android.

FFmpeg is one such tool to tackle this problem but it is not available for Android officially. We tried few existing Android ports of FFmpeg but they were either outdated or didn’t work for us. So we planned to fold our sleeves and compile the library for Android.

Compiling libraries on Linux system is a fairly common task, download the source code of the library and run three commands:

./configure 
make 
make install

As we wanted to use the FFmpeg library on Android devices, we can’t just use the executable generated on the Linux machine directly because Android devices have different CPU architecture, different instruction sets and modified Linux kernel (OS). So we needed to cross compile FFmpeg library for Android

Cross Compilation ?

The process of building executable binaries on one machine, and running them on another machine when the CPU architecture or the Operating System are different is called “cross compilation”.

A special compiler is needed for doing cross compilation and is called “cross compiler”, and sometimes just “toolchain”. Cross compiler is necessary to compile for different platforms from one machine.

We do cross compilation because the platform for which we are compiling could be infeasible for a compiler to run on, such as for a microcontroller of an embedded system because those systems contain no operating system.

Compiling FFmpeg for Android:

Android NDK comes with a cross compiler. On Linux machines you can locate it at following path

[Android-directory]/ndk/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/bin/

First we configured x264 library so that we can include x264 encoders with FFmpeg binary.

./configure \
--cross-prefix=arm-linux-androideabi- \
--sysroot="$NDK_SYSROOT" \
--host=arm-linux \
--enable-pic \
--enable-static \
--disable-cli

See the explanation to some of the options below:

--sysroot=”Android/ndk/platforms/$ANDROID_API_VERSION/arch-arm”

sysroot option provides the logical root directory for headers and libraries. This is useful when you don’t want the headers/libraries in the standard paths affect your build. As we want to use Android cross compiler so we need to set the path of Android cross compiler as sysroot.

--cross-prefix=arm-linux-androideabi-

cross prefix used for locating compilation tools in PATH env variable e.g in place of gcc it will use arm-linux-androideabi-gcc

--host=arm-linux

host system for which we are compiling. Here we are compiling for 2 different Android architectures namely armv7 and armv7-a neon

--enable-pic

position-independent code commonly used for shared libraries

--enable-static

build static library

Then we simply ran the make command using -j option to utilise all cores available

make -j$(nproc)

Then we configured FFmpeg for armv7-a neon devices

./configure \
--target-os=linux \
--cross-prefix=arm-linux-androideabi- \
--arch=arm \
--cpu=armv7-a \
--sysroot="$NDK_SYSROOT" \
--disable-avdevice \
--disable-decoder=h264_vdpau \
--enable-libx264 \
--enable-gpl \
--prefix=../build/armeabi-v7a-neon \
--extra-cflags='-I../x264 -DANDROID -I${ANDROID_NDK_ROOT_PATH}/sources/cxx-stl/system/include -march=armv7-a -mfloat-abi=softfp -mfpu=neon' \
--extra-ldflags='-L../x264 -Wl,--fix-cortex-a8 -L../android-libs -Wl,-rpath-link,../android-libs' \
--extra-cxxflags='-Wno-multichar -fno-exceptions -fno-rtti'

See the explanation to some of the options below:

--target-os=linux

since we are compiling it on linux

--arch=arm

architecture we are compiling for

--cpu=armv7-a

cpu we are compiling for

--enable-libx264

enabling x264 encoders

--enable-gpl

enabling gpl license required to include x264 lib (by default FFmpeg is lgpl licensed)

--prefix=../build/armeabi-v7a-neon

setting build prefix path

--extra-cflags='-I../x264 -DANDROID -I${ANDROID_NDK_ROOT_PATH}/sources/cxx-stl/system/include -march=armv7-a -mfloat-abi=softfp -mfpu=neon'

environment/makefile variables to be passed for compilation and included x264 library

Some of the commands we used after successfully compiling FFmpeg for Android:

ffmpeg -y -i [INPUT FILE PATH] -c:a copy -c:v libx264 -preset:v ultrafast -profile:v baseline -level 3.0 -r 30 [OUTPUT FILE PATH];

converts video to x264 encoding with frame rate 30 and setting video profile to baseline 3.0

ffmpeg -y -i [INPUT FILE PATH] -c copy -bsf:v h264_mp4toannexb -f mpegts [OUTPUT FILE PATH]

converts video to ts(transport) stream, which we can further use to merge multiple videos

I wrote a shell script for the same, follow the steps in readme to compile it yourself. https://github.com/hiteshsondhi88/ffmpeg-android/

 

Expense Tracking Made Easy with Our Android Application – Expense Tracker

Most of us are oblivious of our spending behavior and more often than not end up asking questions like ‘How can I improve my spending habits’? Or ‘how can I hold back unwanted expenses’? Let’s accept it, mainstream population is unmindful of their spending behavior and typically end up overspending (of course some people claim to know where their money is going). It is difficult to fully dispute this claim, but there is a fair degree of skepticism. None of us is a human calculator after all (unless you are not using a traditional pen and paper approach for your book-keeping).

The truth is, without proper analysis we only have a vague sense of where our money goes and this where the real need of an expense tracking mechanism becomes essential.  Needless to say, not just any expense tracking application would do. It can be argued that there are many ways to track expenses and one should pick the one that suits the requirements. But at the same time the method should be hassle free and not cumbersome or time consuming. And there is no better way to be able to track your expense on your mobile while you are ‘On the Go’.

After having used a variety of Android applications for expense tracking and management, we were itching to develop our own version of an expense tracking and management app, as we felt there were many apps that lacked user friendly UI, ease of use, were over burdened with features, etc.

What we are able to achieve with our Android application is a simple, clutter free, intuitive yet powerful expense tracking application that addresses the need for monitoring daily expenses.

The application is out in the Android Marketplace and into the hands of many people with Android devices wanting an easy way to track and manage their expenses.

About Expense Tracker and Features

Expense tracking ‘On the Go’ becomes a whole lot easier with this easy to use, intuitive and simple daily expense management application. ‘Expense Tracker’ helps you to manage your money 7 days a week, 24 hours a day.

Expense Tracker Expense TrackerExpense Tracker

Just as the expenses do not stop, neither should money management. And with money being a limited commodity (for most of us), it is important to use it judiciously. ‘Expense Tracker’ is developed to help everyone who has the need for financial management and that includes everyone who indulges in economic activity of any nature.

Features:

1)    Set reminder for expense entries
2)    Track exp on daily, weekly, monthly and yearly basis
3)    Graphical representation
4)    Mark expense entries as favorite and later add it as an expense from favorite
5)    Take a picture of the expense receipt and save it for future reference
6)    Save expense as voice recording
7)    Log location of the expense automatically
8)    Date and time of the expense is logged automatically
9)    Expenses can be modified to a previous date

Application Link - https://market.android.com/details?id=com.vinsol.expensetracker