Skip to content

Tutorial : FFmpeg 3.4 for Android

Rajdeep Rajput edited this page Jul 20, 2022 · 6 revisions

FFmpeg 3.4 for Android

I wanted to compile FFmpeg 3.4 Version for Android using latest Android NDK. After many unsuccessful attempts and after digging deep into Internet, finally I was able to achieve it. Basic aim of this repository is that nobody has to ever go through the same frustrating efforts and one should be able to save their development time. I have used Android NDK r16b Stable (x86_64) on Ubuntu 16.04 (64-bit). For 32-bit Ubuntu OS, the below process will not work as latest NDK is available only for 64-bit and Google has discontinued their support for 32-bit Linux. So, I would suggest you to look for other options.

NOTE: Please do not attempt to compile it on Windows, your efforts will be in vain. There were many articles saying the same thing. But, I tried anyway as I did not want to setup VM and now I absolutely agree with, what people are saying out there on Internet.

Prerequisite:

With Android NDK r16b, now it is very easy to create Android Standalone Toolchain for compilation of libraries. For that, we will need "Python" to run "make_standalone_toolchain.py" file present inside NDK folder. Generally, Python ships along with Ubuntu OS, but in case it is not available, then please install it. By pressing "Ctrl + Alt + t", you can open terminal windows and then type "python" and hit enter. If it shows details about Python (e.g. version) like shown in below image, then it means, it is installed on your system.

Step 1: Download

Please download or extract below mentioned files in "home" location to avoid system administrator or root permission related errors (which I encountered).

FFmpeg Download Link: https://www.ffmpeg.org/download.html

The following command line in Ubuntu terminal will download FFmpeg. It will download latest version automatically:

Open the Ubuntu Terminal by pressing (Ctrl + Alt + t) and copy paste the following line in the terminal and it Enter button:

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

The successful cloning or downloading of FFmpeg should look like following image and in "home" location you should find "ffmpeg" folder.

If manually downloaded using download link, then you will need to extract the compressed data. Please extract data in the same location as shown in above image.

Then, Android NDK r16b can be downloaded from the following link:

https://developer.android.com/ndk/downloads/index.html

Above link will download a ZIP file. Please extract the NDK data in "home" location to avoid system administrator or root permission related errors. Please see below image for more details where to extract:

We are set to go!

Step 2: Create NDK Standalone Toolchain

For each architecture, you need to create separate NDK Standalone Toolchain. Here, I am going to create for arm, arm64, x86 and x86_64 and not mips. Please go to the NDK tools folder path "path to extracted NDK folder/build/tools". Run Terminal at this path location by right clicking the mouse in blank area and selecting option "Open in Terminal". Run the following commands in terminal according to required architectures:

NOTE: "P" letter of python in command line should be smaller.

For arm: python make_standalone_toolchain.py --arch arm --stl=libc++ --install-dir /home/(your computer name or user name)/my_toolchains/arm --force

Do same for the following architectures.

For arm64: python make_standalone_toolchain.py --arch arm64 --stl=libc++ --install-dir /home/(your computer name or user name)/my_toolchains/arm64 --force

For x86: python make_standalone_toolchain.py --arch x86 --stl=libc++ --install-dir /home/(your computer name or user name)/my_toolchains/x86 --force

For x86_64: python make_standalone_toolchain.py --arch x86_64 --stl=libc++ --install-dir /home/(your computer name or user name)/my_toolchains/x86_64 --force

After running above commands, it will take few minutes to create Standalone Toolchain. Once process is successfully completed, then inside "my_toolchains" folder there will folders for each architecture as shown below. Each folder will have its own files and folders which will be necessary to build or compile any 3rd party library.

With above commands, the API will be set automatically to default. Currently, it is 14 for 32-bit architectures and 21 for 64-bit architectures. Manually, the API can be set to the desired level (e.g. API level 24) by passing "--api 24" argument. See the below example:

For arm: python make_standalone_toolchain.py --arch arm --api 24 --stl=libc++ --install-dir /home/(your computer name or user name)/my_toolchains/arm --force

Step 3: Build FFmpeg for Android

Now copy all build scripts from this repository into FFmpeg folder. Please see below image. Link to build script files: https://github.com/ejoker88/FFmpeg-3.4-Android

Before running the "build_all.sh" script, please update the TOOLCHAIN path in all other build files "build_arm64-v8a.sh, build_armeabi.sh, build_armeabi-v7a.sh, build_x86.sh and build_x86_64.sh". It should point to the location of your toolchain folder with correct architecture path, which we just created in Step 2. Please do not forget to save the files after editing. Please see below image for more details:

Now run the build scripts with the following commands in Ubuntu terminal. Command should be run from FFmpeg folder, where build scripts were copied earlier. So, go to FFmpeg folder, right click anywhere in blank space and select "Open in Terminal".

Once terminal window opens, type in first command and hit enter and then second command and hit enter. Now, the build should start successfully without errors. Command 1: chmod +x build_all.sh Command 2: ./build_all.sh

That’s it! You should have FFmpeg shared libraries [*.so] for all architectures inside (FFmpeg folder path)/android/(Architecture)/(.so files).