FFmpeg on Android
Solution 1
Here are the steps I went through in getting ffmpeg to work on Android:
- Build static libraries of ffmpeg for Android. This was achieved by building olvaffe's ffmpeg android port (libffmpeg) using the Android Build System. Simply place the sources under /external and
make
away. You'll need to extract bionic(libc) and zlib(libz) from the Android build as well, as ffmpeg libraries depend on them. Create a dynamic library wrapping ffmpeg functionality using the Android NDK. There's a lot of documentation out there on how to work with the NDK. Basically you'll need to write some C/C++ code to export the functionality you need out of ffmpeg into a library java can interact with through JNI. The NDK allows you to easily link against the static libraries you've generated in step 1, just add a line similar to this to Android.mk:
LOCAL_STATIC_LIBRARIES := libavcodec libavformat libavutil libc libz
Use the ffmpeg-wrapping dynamic library from your java sources. There's enough documentation on JNI out there, you should be fine.
Regarding using ffmpeg for playback, there are many examples (the ffmpeg binary itself is a good example), here's a basic tutorial. The best documentation can be found in the headers.
Good luck :)
Solution 2
For various reasons, Multimedia was and is never easy in terms of achieving the task without compromising on efficiency. ffmpeg is an effort in improving it day by day. It supports different formats of codecs and containers.
Now to answer the question of how to use this library, i would say that it is not so simple to write it here. But i can guide you in following ways.
1) Inside the ffmpeg directory of source code, you have output_example.c or api_example.c. Here, you can see the code where encoding/decoding is done. You will get an idea as to which API's inside ffmpeg you should call. This would be your first step.
2) Dolphin player is a open source project for Android. Currently it is having bugs but developers are working continuously. In that project you have the whole setup ready which you can use to continue your investigation. Here is a link to the project from code.google.com or run the command "git clone https://code.google.com/p/dolphin-player/" in a terminal. You can see two projects named P and P86 . You can use either of them.
Extra tip i would like to offer is that when you are building the ffmpeg code, inside build.sh you need to enable the muxers/demuxers/encoders/decoders of the formats you want to use. Else the corresponding code will not be included in the libraries. It took a lot of time for me to realize this. So thought of sharing it with you.
Few Basics : When we say a video file, ex : avi, it is combination of both audio and video
Video file = Video + Audio
Video = Codec + Muxer + Demuxer
codec = encoder + Decoder
=> Video = encoder + decoder + Muxer + Demuxer(Mpeg4 + Mpeg4 + avi +avi - Example for avi container)
Audio = Codec + Muxer + Demuxer
codec = encoder + Decoder
=> Audio = encoder + decoder + Muxer + Demuxer(mp2 + mp2 + avi + avi - Example for avi container)
Codec(name is deriverd from a combination of en*co*der/*dec*oder) is just a part of format which defines the algorithms used to encode/decode a frame. AVI is not a codec, it is a container which uses Video codec of Mpeg4 and Audio codec of mp2.
Muxer/demuxer is used to combine/separate the frames from a file used while encoding/decoding.
So if you want to use avi format, you need to enable Video components + Audio components.
Ex, for avi, you need to enable the following. mpeg4 Encoder, mpeg4 decoder, mp2 encoder, mp2 decoder, avi muxer, avi demuxer.
phewwwwwww...
Programmatically build.sh should contain the following code:
--enable-muxer=avi --enable-demuxer=avi (Generic for both audio/video. generally Specific to a container)
--enable-encoder=mpeg4 --enable-decoder=mpeg4(For video support)
--enable-encoder=mp2 --enable-decoder=mp2 (For Audio support)
Hope i idid not confuse you more after all this...
Thanks, Any assistance needed, please let me know.
Solution 3
After a lot of research, right now this is the most updated compiled library for Android that I found:
https://github.com/bravobit/FFmpeg-Android
- At this moment is using
FFmpeg release n4.0-39-gda39990
- Includes FFmpeg and FFProbe
- Contains Java interface to launch the commands
- FFprobe or FFmpeg could be removed from the APK, check the wiki https://github.com/bravobit/FFmpeg-Android/wiki
Solution 4
The most easy to build, easy to use implementation I have found is made by theguardianproject team: https://github.com/guardianproject/android-ffmpeg
Solution 5
I've done a little project to configure and build X264 and FFMPEG using the Android NDK. The main thing that's missing is a decent JNI interface to make it accessible via Java, but that is the easy part (relatively). When I get round to making the JNI interface good for my own uses, I'll push that in.
The benefit over olvaffe's build system is that it doesn't require Android.mk files to build the libraries, it just uses the regular makefiles and the toolchain. This makes it much less likely to stop working when you pull new change from FFMPEG or X264.
Related videos on Youtube
Jag
Updated on December 06, 2020Comments
-
Jag over 3 years
I have got FFmpeg compiled (libffmpeg.so) on Android. Now I have to build either an application like RockPlayer or use existing Android multimedia framework to invoke FFmpeg.
Do you have steps / procedures / code / example on integrating FFmpeg on Android / StageFright?
Can you please guide me on how can I use this library for multimedia playback?
I have a requirement where I have already audio and video transport streams, which I need to feed to FFmpeg and get it decoded / rendered. How can I do this on Android, since IOMX APIs are OMX based and cannot plug-in FFmpeg here?
Also I could not find documentation on the FFmpeg APIs which need to be used for playback.
-
Axarydax over 13 yearsthis is interesting, I'm curious too
-
Swathi EP almost 13 yearshow did you compile ffmpeg to get .so files? can you please share the steps you followed. I am working on windows with cygwin-1.7.9 and ndk r5. Please help me.
-
slhck over 11 yearsHere's a relatively new FFmpeg for Android: sourceforge.net/projects/ffmpeg4android
-
RAJESH over 11 years@slhck i have downloaded the ffmpeg code from the above link and have tried to compile it but i am not able to get the .so files . it shows lot of issues ..
-
TharakaNirmana over 11 yearsplease help me with: stackoverflow.com/questions/14157030/…, I do not know where to include this function and run!.....
-
dbro about 10 yearsI've put together simple build scrips for compiling the latest FFmpeg (and also librtmp) for Android: github.com/OnlyInAmerica/FFmpeg-Android
-
steveh almost 10 yearsbefore i start down this path, can anyone tell me what the decode performance is like on a typical tablet like a dual-core 1 GHz Cortex-A9 with ULP GeForce for say a 640x480 video? thnaks
-
Madhavan Malolan over 6 yearsWrapped the library into an easy to integrate lib. github.com/madhavanmalolan/ffmpegandroidlibrary
-
Android dev over 5 years@Jag Can you share the steps /procedures/code/ example on integrating FFmpeg on Android?
-
Yuriy Solovyov over 12 yearsNick, your project is not compiling on OS X 10.7 libx264.a(common.o): In function
x264_param_parse': common.c:(.text+0x2864): undefined reference to
_DefaultRuneLocale' collect2: ld returned 1 exit status make: *** [x264] Error 1 -
Rob Lourens about 12 yearsThere are quite a few links to this answer for building ffmpeg for Android. Is this still the best solution? The Android Build System link is broken - what is that supposed to be? There are a bunch of toolkits to aid in building with the NDK. However they all fail with various build errors for me, and seem a little old. Is there any reason why someone can't just post a built static ffmpeg lib?
-
Rob Lourens about 12 yearsTo answer my own question, I found this repo to be the most useful for building ffmpeg and JNI wrappers - github.com/andynicholson/android-ffmpeg-x264
-
idish almost 12 yearsHey, I would like to thank you very much for that info, you really helped me very much, Is it possible for you to help me if I will need some later on? Thank you!
-
idish almost 12 yearsCan I add you please via skype/MSN or any other chat platform please? I have a few questions about it, thank you.
-
Sandeep almost 12 yearsSure..!! But my on-line presence is a bit low.. Unless it is very much required I do not log in to skype. You can mail me for any important things. Email : [email protected]
-
Guy over 10 yearsNot sure, I am guessing that it is, nothing in new iOS version comes to mind that might break this. When I posted this, I still had 10.7 or 10.6
-
Mr.G over 10 yearsdo u know, how can i convert 3gp to audio, using JNI implementation
-
X.Y. over 10 yearsWhat's your experience with guardianproject?
-
Arsen Sench over 7 yearsThis wrapper is very very very very very slow. 200 images to video is taking 50-60 seconds . . . but normally ffmpeg handles that task in 4-5 seconds.
-
Ajeet over 7 yearsThis project is not working anymore. Do you have some other resources?
-
Akash Dubey about 7 years@ArsenSench do you have other solution ?
-
sappu over 6 yearsSeems you have compiled FFmpeg v2.8.4, is there any plans to upgrade FFmpeg? We are looking for the android solution having the latest (May be 3.2 or 3.4) version of FFmpeg.
-
Madhavan Malolan over 6 yearsYes. I do intend to move it to 3.x github.com/madhavanmalolan/ffmpegandroidlibrary/milestone/1 You can try to modify the build script here and compile for 3.4 github.com/madhavanmalolan/ffmpegandroidlibrary/wiki/…
-
sappu about 6 yearsThanks @Madhvan. I am building ffmpeg library on windows. Just wondering what all needs to be changed in github.com/madhavanmalolan/ffmpegandroidlibrary/wiki/… in order to build?
-
Krutika Chotara over 3 yearsI have used this solution and its working perfect but video quality is too bad and only 3 mb reduced. Is there any other solution ?
-
jay patoliya over 3 yearsInstead of ==> -vcodec libx264 you can use 1) -vcodec mpeg4 (use a higher bitrate than the default) 2) -b:v 200k 3) or choose a -qscale:v value from 1-31 where a lower value results in a higher bitrate and therefore usually better quality
-
radiolondra about 3 yearsYou could give a try to this: github.com/tanersener/ffmpeg-kit