Compiler Environment:
- Mac OS monterey (12.3.1)
- android-ndk (21.0.6113669)
-
ffmpeg-4.2.2
Download ffmpeg: wget https://ffmpeg.org/releases/ffmpeg-4.2.2.tar.bz2, or download it from the official git.
Note: The ndk version I am using is 21.0.6113669. I have tried version 24.0.8215888. The lack of certain files will cause the compilation to fail. It should be possible if the larger version is 21, but the specific reason has not been verified.
Note: After Android NDK r17c, Google officially removed GCC and no longer supports GCC. New versions of NDK are compiled using CLANG. So we use CLANG for compilation here.
1. Configure script
Add cross_prefix_clang parameter
Open (note: do not double-click to run) the configure file in the root directory of ffmpeg-4.2.2, search for CMDLINE_SET, you can find the following code, and then add a command line option: cross_prefix_clang
CMDLINE_SET="
$PATHS_LIST
ar
arch
as
assert_level
build_suffix
cc
objcc
cpu
cross_prefix
# 新增命令行参数
cross_prefix_clang
custom_allocator
cxx
dep_cc
doxygen
env
extra_version
.
.
.
2. Modify the compilation tool path settings,
Search ar_default="${cross_prefix}${ar_default}"
ar_default="${cross_prefix}${ar_default}"
cc_default="${cross_prefix}${cc_default}"
cxx_default="${cross_prefix}${cxx_default}"
nm_default="${cross_prefix}${nm_default}"
将中间两行改成
ar_default="${cross_prefix}${ar_default}"
cc_default="${cross_prefix_clang}${cc_default}"
cxx_default="${cross_prefix_clang}${cxx_default}"
nm_default="${cross_prefix}${nm_default}"
Create a new compilation configuration script
Create a new shell script in the root directory and name it: build_android_clang.sh
#!/bin/bash
set -x
# 目标Android版本
API=24
CPU=armv7-a
#so库输出目录,自己喜欢放哪里就放哪里
# OUTPUT=$(pwd)/android/$CPU
OUTPUT=./output/$CPU
# NDK的路径,根据自己的NDK位置进行设置
NDK=/Users/kbq/Library/Android/sdk/ndk/21.0.6113669
# 编译工具链路径
TOOLCHAIN=$NDK/toolchains/llvm/prebuilt/darwin-x86_64
# 编译环境
SYSROOT=$TOOLCHAIN/sysroot
function build
{
./configure \
--prefix=$OUTPUT \
--target-os=android \
--arch=arm \
--cpu=armv7-a \
--enable-asm \
--enable-neon \
--enable-cross-compile \
--enable-shared \
--disable-static \
--disable-doc \
--disable-ffplay \
--disable-ffprobe \
--disable-symver \
--disable-ffmpeg \
--sysroot=$SYSROOT \
--cross-prefix=$TOOLCHAIN/bin/arm-linux-androideabi- \
--cross-prefix-clang=$TOOLCHAIN/bin/armv7a-linux-androideabi$API- \
--extra-cflags="-fPIC"
make clean all
# 这里是定义用几个CPU编译
make -j12
make install
}
build
The third step starts compilation
Open the cmd terminal and cd to the directory where FFmpeg is located
Type ./build_android_clang.sh
Waiting for the compilation to be completed, you will get two directories, include and lib, in the ffmpeg/android/armv7-a directory, which are header files and so library files respectively.
Note: If the error message zsh: permission denied: ./build_android_clang.sh appears, it means the file permissions are incorrect. Execute chmod u+x build_android_clang.sh