FFmpeg for Android

Madhavan Malolan
madhavanmalolan
Published in
3 min readNov 6, 2017

This resource has been moved to GitHub

Oneline integration

Building from source

Doubts and issues

Success Stories

Archive :

TL;DR : One line integration!

Add this to your build.gradle
Access FFmpeg using the Controller.getInstance().run() method. The above example is to mute a input_file.mp4 and store it in output_file.mp4

More details at :

It took me a good amount of effort to get ffmpeg working over JNI for Android. Most other posts on the web seemed to be out dated on this matter. So, here are the quick steps that i followed to get it up and running.

After implementing the steps here, you should be able to perform all the commands that you can run from the command line tool. Idea borrowed from halfninja
Note : This tutorial has been written working with FFMPEG 2.8.4. However, it has been verified to work with 3.x too.

You will need :

Android NDK

Download the .zip file for your operating system from the official site

Unzip the downloaded ndk archive. For the remainder of the tutorial, we’ll assume the path to the unzipped directory (android-ndk-<version>, eg. android-ndk-r12b) to be /path/to/android-ndk

FFMPEG source

Get the latest ffmpeg source. The remainder of this tutorial assumes that the ffmpeg directory resides at /path/to/ffmpeg

git clone https://git.ffmpeg.org/ffmpeg.git ffmpeg

Building FFMPEG

cd /path/to/ffmpeg

Modify the configure script.

Open the file /path/to/ffmpeg/configure and find & replace the following lines

with

Create a build script

Create a file here called build_ffmpeg.sh (/path/to/ffmpeg/build_ffmpeg.sh) with the following contents :
Make sure you change the ndk paths in the script appropriately

Build

After you have saved the contents of build_ffmpeg.sh run the script.

chmod a+x build_ffmpeg.sh

./build_ffmpeg.sh

On successful completion you should see the android/ directory in the /path/to/ffmpeg directory.
The android/ directory should contain arm/include and arm/lib

Compiling source

Copy the following files from /path/to/ffmpeg to the jni/ directory

        cmdutils.c 	
cmdutils.h
ffmpeg.c
ffmpeg.h
ffmpeg_filter.c
ffmpeg_opt.c
ffmpeg_opt.h
logjam.h

In the same jni directory, create a ffmpeg_controller.c

and also create a header file in the same directory com_example_Controller.h

Writing the android make files (Android.mk)

Note there are 2 Android.mk

Create a file Android.mk in the jni directory with the following content :

Note replace /path/to with the appropriate path to the ffmpeg’s parent dir.

And create another Android.mk at /path/to/ffmpeg/android/arm/ with the following contents.

cd /path/to/app/jni
/path/to/ndk/ndk-build

Accessing from Java

Create a file com.example.Controller java file with the following content

You can call the function testFFMPEG as

Controller.testFFMPEG();

Modify the testFFMPEG directory to appropriately suit your requirements.
If you want to run the command "ffmpeg a b c d e", your argument to the run function will be {"a","b","c","d","e"}

Readymade solution

--

--