Tuesday, December 6, 2011


FFmpeg4Android is a way your application can run FFmpeg commands,  only Java, no need for C code, or NDK.

Adding FFMpeg4Android support to your app:

New and more simplified and faster FFmpeg4Android using direct JNI call (instead of using a library)
This is the most strait forward way to go if you want maximum flexibility to your app.
This way, FFmpeg commands can be called from your app, from every type of Android Object, or Java Object e.g Service, Fragment, Activity, Thread etc, simply call the JNI directly.

Download the ffmpeg4android_demo full source code from here:

FFmpeg4Android Download

Here is a document that explains how to add FFmpeg4Android support to your app:

 Commands Examples

This commands verified to work with FFmpeg4Android:

This are only some basic examples, a lot more is possible...

Video Compress:

// simple regular commad
ffmpeg -y -i /sdcard/videokit/in.mp4 -strict experimental -s 160x120 -r 25 -vcodec mpeg4 -b 150k -ab 48000 -ac 2 -ar 22050 /sdcard/videokit/out.mp4


// compress with h264 (to support chrome)

ffmpeg -y -i /sdcard/videokit/in.mp4 -strict experimental -vcodec libx264 -preset ultrafast -crf 24 -acodec aac -ar 44100 -ac 2 -b:a 96k -s 320x240 -aspect 4:3 /sdcard/videokit/out3.mp4

// As complex command, don't forget to use setCommandComplex(complexCommand)

// Use this format to support files that contains spaces and special characters

String[] complexCommand = {"ffmpeg","-y" ,"-i", "/sdcard/video kit/in.mp4","-strict","experimental","-s", "160x120","-r","25", "-vcodec", "mpeg4", "-b", "150k", "-ab","48000", "-ac", "2", "-ar", "22050", "/sdcard/video kit/out.mp4"};


Audio Compression


String commandStr = "ffmpeg -y -i /sdcard/vk2/in.wav -ar 44100 -ac 2 -ab 64k -f mp3 /sdcard/videokit/out.mp3";


Video Rotate (90 degrees cw):

ffmpeg -y -i /sdcard/videokit/in.mp4 -strict experimental -vf transpose=1 -s 160x120 -r 30 -aspect 4:3 -ab 48000 -ac 2 -ar 22050 -b 2097k /sdcard/video_output/out.mp4


Video Crop:

ffmpeg -y -i /sdcard/videokit/short.mp4 -strict experimental -vf crop=100:100:0:0 -s 320x240 -r 15 -aspect 3:4 -ab 12288 -vcodec mpeg4 -b 2097152 -sample_fmt s16 /sdcard/videokit/out.mp4


Extract Picture from Video:

ffmpeg -y -i /sdcard/videokit/in.mp4 -strict experimental -an -r 1/2 -ss 00:00:00.000 -t 00:00:03 /sdcard/videokit/filename%03d.jpg


Extract Sound from Video:

ffmpeg -y -i /sdcard/videokit/in.avi -strict experimental -acodec copy /sdcard/videokit/out.mp3

ffmpeg -y -i /sdcard/videokit/in.mp4 -strict experimental -vn -ar 44100 -ac 2 -ab 256k -f mp3 /sdcard/videokit/out.mp3


Re-encode Audio in Video:

ffmpeg -y -i /sdcard/in.mp4 -strict experimental -vcodec copy -acodec libmp3lame -ab 64k -ac 2 -b 1200000 -ar 22050 /sdcard/out.mp4

Change Video Resolution:

ffmpeg -y -i /sdcard/in.mp4 -strict experimental -vf transpose=3 -s 320x240 -r 15 -aspect 3:4 -ab 12288 -vcodec mpeg4 -b 2097152 -sample_fmt s16 /sdcard/out.mp4

Cut time segment from Video:

ffmpeg -ss 00:00:01.000 -y -i /sdcard/videokit/in.mp4 -strict experimental -t 00:00:02.000 -s 320x240 -r 15 -vcodec mpeg4 -b 2097152 -ab 48000 -ac 2 -b 2097152 -ar 22050 /sdcard/videokit/out.mp4

Transcode Audio:

ffmpeg -y -i /sdcard/videokit/big.wav /sdcard/videokit/small.mp3


 //  test with watermark.png 128x128, add it to /sdcard/videokit/

String[] complexCommand = {"ffmpeg","-y" ,"-i", "/sdcard/videokit/in.mp4","-strict","experimental", "-vf", "movie=/sdcard/videokit/watermark.png [watermark]; [in][watermark] overlay=main_w-overlay_w-10:10 [out]","-s", "320x240","-r", "30", "-b", "15496k", "-vcodec", "mpeg4","-ab", "48000", "-ac", "2", "-ar", "22050", "/sdcard/videokit/out.mp4"}; 

Streaming simple:


* note that all streaming examples will need to add internet permission to the Android project Manifest file ( <uses-permission android:name="android.permission.INTERNET" />  )

// use this command on ffmpeg4android
ffmpeg -i /sdcard/videokit/2.mpg -strict experimental -f mpegts udp://

// You can use any player that supports streaming, on the target machine, to play the stream, in this case we used ffplay

ffplay -f mpegts -ast 1 -vst 0 -ar 48000 udp:// 

Stream raw video from camera preview:


getting the raw stream from the camera preview:

Parameters parameters = camera.getParameters();
imageFormat = parameters.getPreviewFormat();
if (imageFormat == ImageFormat.NV21) {
                Camera.Size previewSize = parameters.getPreviewSize();
                frameWidth = previewSize.width;
                frameHeight = previewSize.height;
                Rect rect = new Rect(0, 0, frameWidth, frameHeight);
                YuvImage img = new YuvImage(data, ImageFormat.NV21, frameWidth, frameHeight, null);
                try {

Encode and stream using FFmpeg4Android: 

"ffmpeg -f rawvideo -pix_fmt nv21 -s 640x480 -r 15 -i " + Environment.getExternalStorageDirectory().getAbsolutePath().toString() + "/yuv.data rtmp://host/stream.flv"

Stream on one device, receive a steam on second device and save it:

on the first device:

ffmpeg -i /sdcard/one3.mp4 -f mpegts udp://

on the second device:

String[] complexCommand = {"ffmpeg","-y" ,"-i", "udp://","-strict","experimental","-crf", "30","-preset", "ultrafast", "-acodec", "aac", "-ar", "44100", "-ac", "2", "-b:a", "96k", "-vcodec", "libx264", "-r", "25", "-b:v", "500k", "-f", "flv", "/sdcard/videokit/t.flv"};

* this needs internet permission in the manifest. 

H264 encoding:
ffmpeg -y -i /sdcard/Video/1.MTS -strict experimental -vcodec libx264 -preset ultrafast -crf 24 /sdcard/videokit/out.mp4

ffmpeg -y -i /sdcard/videokit/m.mkv -strict experimental -vcodec libx264 -preset ultrafast -crf 24 -sn /sdcard/videokit/m2.mkv

ffmpeg -y -i /sdcard/videokit/m2.mkv -i /sdcard/videokit/in.srt -strict experimental -vcodec libx264 -preset ultrafast -crf 24 -scodec copy /sdcard/videokit/mo.mkv

ffmpeg -y -i /sdcard/videokit/m2.mkv -i /sdcard/videokit/in.srt -strict experimental -scodec copy /sdcard/videokit/outm3.mkv

Convert Audio file to m4a
ffmpeg -i /sdcard/videokit/in.mp3 /sdcard/videokit/out.m4a

Encode h264 video and aac audio in one command
ffmpeg -y -i /sdcard/videokit/in.mp4 -strict experimental -vcodec libx264 -crf 24 -acodec aac /sdcard/videokit/out.mkv

Vintage filter

commandStr = "ffmpeg -y -i /sdcard/videokit/in.mp4 -strict experimental -vf curves=vintage -s 640x480 -r 30 -aspect 4:3 -ab 48000 -ac 2 -ar 22050 -b 2097k -vcodec mpeg4 /sdcard/videokit/curve.mp4";

Fade in and out transition
String[] complexCommand = {"ffmpeg","-y" ,"-i", "/sdcard/videokit/in.m4v","-acodec", "copy", "-vf", "fade=t=in:st=0:d=5, fade=t=out:st=20:d=5", "/sdcard/videokit/out.mp4"};

Join 2 files using of the same size using filter_complex
String[] complexCommand = {"ffmpeg","-y","-i", "/sdcard/videokit/in1.mp4", "-i", "/sdcard/videokit/in2.mp4", "-strict","experimental", "-filter_complex", "[0:0] [0:1] [1:0] [1:1] concat=n=2:v=1:a=1", "/sdcard/videokit/out.mp4"};

// concat videos with different codecs, and different sizes, different rate (limitation, needs the same aspect on both videos):

String[] complexCommand = {"ffmpeg","-y" ,"-i", "/sdcard/videokit/in.mp4","-i", "/sdcard/videokit/in.mp4","-strict","experimental","-filter_complex", "[0:v]scale=w=160:h=120[v1]; [1:v]scale=w=160:h=120[v2]; [v1][0:a][v2][1:a] concat=n=2:v=1:a=1 [v] [a]","-map","[v]","-map","[a]", "-b", "2097k", "-vcodec", "mpeg4", "/sdcard/videokit/out.mp4"};

Create a video from pictures

// note that this commans is sensative to the pictures size.
// all pictures should be with the same size, and correspond to s peticular video resolution.
// e.g hd video is: 1280x720, and this should be the pictures size 
commandStr = "ffmpeg -y -r 1/5 -i /sdcard/videokit/pic00%d.jpg /sdcard/videokit/out.mp4";

Advanced filtering
String[] complexCommand = {"ffmpeg","-y" ,"-i", "/sdcard/videokit/in.mp4","-strict","experimental", "-vf", "crop=iw/2:ih:0:0,split[tmp],pad=2*iw[left]; [tmp]hflip[right]; [left][right] overlay=W/2", "-vb", "20M", "-r", "23.956", "/sdcard/videokit/out.mp4"};

Increase video and audio speed
String[] complexCommand = {"ffmpeg","-y" ,"-i", "/sdcard/videokit/in.mp4","-strict","experimental", "-filter_complex", "[0:v]setpts=0.5*PTS[v];[0:a]atempo=2.0[a]","-map","[v]","-map","[a]", "-b", "2097k","-r","60", "-vcodec", "mpeg4", "/sdcard/videokit/out.mp4"};

Overlay 2 videos side by side
     String[] complexCommand = {"ffmpeg","-y" ,"-i", "/sdcard/Movies/sample.mp4","-i", "/sdcard/Movies/sample2.mp4", "-strict","experimental",
                "[0:v:0]pad=iw*2:ih[bg];" +
                "-s", "320x240","-r", "30", "-b", "15496k", "-vcodec", "mpeg4","-ab", "48000", "-ac", "2", "-ar", "22050",

  vintage + watermark
      String[] complexCommand = {"ffmpeg","-y" ,"-i", "/sdcard/Movies/sample.mp4","-strict","experimental",
                "movie=/sdcard/videokit/watermark002.png [watermark];" +
                "[in][watermark] overlay=main_w-overlay_w-10:10 [out_overlay];" +
                "-s", "320x240","-r", "30", "-b", "15496k", "-vcodec", "mpeg4","-ab", "48000", "-ac", "2", "-ar", "22050",

Watermark + audio replace
    String[] complexCommand = {"ffmpeg","-y" ,"-i", "/sdcard/Movies/sample.mp4","-i", "/sdcard/videokit/in.mp3", "-strict","experimental",
                "[1:a]atempo=1.0[a1];" +
                "movie=/sdcard/videokit/watermark002.png [watermark];" +
                "[0:v][watermark] overlay=main_w-overlay_w-10:10 [outv]",
                "-map", "[outv]", "-map", "[a1]",
                "-s", "320x240","-r", "30", "-b", "15496k", "-vcodec", "mpeg4","-ab", "48000", "-ac", "2", "-ar", "22050",

Looping on a picture and adding audio with audio encoding support
      String commandStr = "ffmpeg -y -loop 1 -i /sdcard/videokit/pic001.jpg -i /sdcard/videokit/in.mp3 -strict experimental -s 1270x720 -r 25 -aspect 16:9 -vcodec mpeg4 -vcodec mpeg4 -ab 48000 -ac 2 -b 2097152 -ar 22050 -shortest /sdcard/videokit/out2.mp4";

Supporting old ARM devices (ARMv5)
Regular FFmpeg4android library is complied for ARMv7 architecture which cover most devices on the market, and gives optimal performance.

In some cases you need to support old devices, and devices that do not comply to ARMv7.
In this case you can use the ARMv5 compilation.

For this reasons, We created FFmpeg4Android for old devices library, and client.

This version does not support complex filtering, and some other advanced FFmpeg capabilities, and
Its significantly slower then the main FFmpeg4Android version.

This version can support both ARMv7 and ARMv5, the device automaticly choose the best fit.
If you need only ARMv5 delete the armeabi-v7a folder from the libs directory.

   armeabi         -> contains libvideokit.so for ARMv5
   armeabi-v7a   -> Remove this folder if you need only ARMv5

Using complex commands
A complex command is a command that contains embeded elements (in most cases it uses quotations for that.
Here is an example:
ffmpeg -i /sdcard/videokit/in.mp4 -aspect 1:1 -vf split "[main][tmp];[tmp] crop=iw/2:ih:0:0, hflip[tp],[tp] pad=2*iw[left]; [main] crop=iw/2:ih:iw/2:0[right]; [left][right] overlay=W/2" -vb 20M -r 23.956 /sdcard/videokit/outs.mp4

To set a complex command you should create a String array and use the method:

Like this:
String[] complexCommand = {"ffmpeg","-y" ,"-i", "/sdcard/videokit/mo.mkv","-strict","experimental", "-vf", "crop=iw/2:ih:0:0,split[tmp],pad=2*iw[left]; [tmp]hflip[right]; [left][right] overlay=W/2", "-vb", "20M", "-r", "23.956", "/sdcard/videokit/out.mp4"};


Performance Tuning

  • Prefer mpeg4 codec (-codec mpeg4), since with this codec, ffmpeg4android can utilize all device cores.
Here is an example of command that use mpeg4 codec (in this case this command is doing a video compression):

String[] complexCommand = {"ffmpeg","-y" ,"-i", "/sdcard/video kit/in.mp4","-strict","experimental","-s", "160x120","-r","25", "-vcodec", "mpeg4", "-b", "150k", "-ab","48000", "-ac", "2", "-ar", "22050", "/sdcard/video kit/out.mp4"};

  • If its possible, reduce output video resolution (-s) and bitrate (-b), the lower the video quality is, the faster it will take to produce.
  • Use the libvideokit.so.fast, by replacing the /libs/armeabi-v7a/libvideokit.so with it, make sure you remove the .fast extension. Note that this performance optimized compilation is around 30% fast, but also 30% bigger. Currently libvideokit.so.fast is the default so file, if you need size optimized apk, rename libvideokit.so.small to libvideokit.so
  • Encoding using h264 can't utilize all device cores, and thus is much slower. If you must use it, use the -preset ultrafast flag.
  • Using the -threads flag can only degrade the performance, as ffmpeg4android automatically detects the number of core your device has, and set the optimal number of threads for the command used. 

The internals 
As mentioned before, FFmpeg4Android runs as an Android remote service, this service is actually a separate Android process, that encapsulate the FFmpeg native process, that runs directly on the device Linux.
The demo application command will convert vid.mp4 to vid_trans.mp4, it will rotate it 270 degrees, change its resolution to 320x240, change the aspect ratio to 3:4 and encode it with mpeg4 encoder, this is all with one command! (actually it does more, it defines the audio codec, and set the audio bit rate also). 
This is done at the native FFmpeg level, JNI is used to make the calls from the Remote service to the native FFmpeg process.

Android Studio Support

1. Export the gradle files from eclipse, and import it to Android studio.
2. Currently Android Studio does not support NDK so libs so, here is a very simple way to over come this limitation:

   2.a. Archive the FFmpeg4Android_os library libs folder to: armeabi.zip with the folder in zip like: lib/armeabi-v7a/xxx.so, note the the archive folder should be lib and not libs. the structure inside the zip file should be:

  2.b. Rename armeabi.zip to armeabi.jar and put armeabi.jar into libs folder.

  2.c. touch the 2 projects build.gradle files (make edit, and save them), then run the client app to the device via studio, it should work.

This workaround is based on this post from the Android Google Group.

Adding FFMpeg support to your app (FFmpeg4Android  as Library):

Remove ffmpeg4android app from your device (if you have it installed) and remove the /sdcard/videokit folder.
Add the FFMpeg4Android project library (source code) as a new Android project. 
Download the demo client source which use the FFMpeg4Android library, both are ADT projects.
Run the demo client to your device, you should be able to run the FFMpeg command and rotate the video in.mp4, the output video is /sdcard/videokit/vid_trans.mp4

Full installation document for FFmpeg4Android library and the demo client project.

Latest version release notes

2.3.00 (FFmpeg4Android)
* Lollipop support.
* Added fast native (1.1.10).
* Small UI changes.
* Change in package structure for easier integration.
* Simplification and restructure.
* Added ProgressBarWithNotificationExample.
* More UI improvements, and various bug, and warnings fixes.

10.0.01 (FFmpeg4Android Lib)
* Native upgraded to 1.1.12 performance improvements.
* Lollipop support.
* Added small & fast(default) natives.
* Some bug fix related to file close.
* Work folder moved to internal.
* Demo folder is separated from Work folder.
* copy license is separated from copy demo.
* Removed Toasts from license validation in some cases.