学习音视频开发,记录FFmpeg编译过程. 编译环境: Ubuntu 20.04.2 LTS NDK:android-ndk-r22b
这边是下载当前的最新版本 FFmpeg5.1.1
3.配置脚本
3.1修改 configure 脚本
- 新增 cross_prefix_clang 参数
解压下载好的源码,进入根目录找到configure 文件,搜索CMDLINE_SET找到下方代码块,新增 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
...
- 修改编译工具路径
搜索 **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}"
pkg_config_default="${cross_prefix}${pkg_config_default}"
将中间两行cc_default,cxx_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}"
pkg_config_default="${cross_prefix}${pkg_config_default}"
- 新建编译配置脚本
在FFmpeg5.1.1根目录,新建build_android_arm64-v8a_clang.sh的shell脚本.
export NDK=/home/moyihen/Desktop/android-ndk-r22b
TOOLCHAIN=$NDK/toolchains/llvm/prebuilt/linux-x86_64
function build_android
{
./configure \
--prefix=$PREFIX \
--enable-neon \
--enable-hwaccels \
--enable-gpl \
--disable-postproc \
--disable-debug \
--enable-small \
--enable-jni \
--enable-mediacodec \
--enable-decoder=h264_mediacodec \
--enable-static \
--enable-shared \
--disable-doc \
--enable-ffmpeg \
--disable-ffplay \
--disable-ffprobe \
--disable-avdevice \
--disable-doc \
--disable-symver \
--cross-prefix=$CROSS_PREFIX \
--target-os=android \
--arch=$ARCH \
--cpu=$CPU \
--cc=$CC \
--cxx=$CXX \
--enable-cross-compile \
--sysroot=$SYSROOT \
--extra-cflags="-Os -fpic $OPTIMIZE_CFLAGS" \
--extra-ldflags="$ADDI_LDFLAGS"
make clean
make -j8
make install
echo "============================ build android arm64-v8a success =========================="
}
ARCH=arm64
CPU=armv8-a
API=21
CC=$TOOLCHAIN/bin/aarch64-linux-android$API-clang
CXX=$TOOLCHAIN/bin/aarch64-linux-android$API-clang++
SYSROOT=$NDK/toolchains/llvm/prebuilt/linux-x86_64/sysroot
CROSS_PREFIX=$TOOLCHAIN/bin/aarch64-linux-android-
PREFIX=$(pwd)/android/$CPU
OPTIMIZE_CFLAGS="-march=$CPU"
build_android
4.开始编译
打开命令终端,切换到FFmpeg,输入./build_android_clang.sh,等待编译完成,将会在 ffmpeg-5.1.1/android/armv7-a目录下得到 include 和 lib 两个目录,分别是 头文件 和 so库文件(静态库.a看需求自取).
编译armv7a和arm64v8a ,修改对应shell脚本参数.
ARCH=arm64
CPU=armv8-a
API=21
CC=$TOOLCHAIN/bin/aarch64-linux-android$API-clang
CXX=$TOOLCHAIN/bin/aarch64-linux-android$API-clang++
SYSROOT=$NDK/toolchains/llvm/prebuilt/linux-x86_64/sysroot
CROSS_PREFIX=$TOOLCHAIN/bin/aarch64-linux-android-
PREFIX=$(pwd)/android/$CPU
OPTIMIZE_CFLAGS="-march=$CPU"
ARCH=arm
CPU=armv7-a
API=21
CC=$TOOLCHAIN/bin/armv7a-linux-androideabi$API-clang
CXX=$TOOLCHAIN/bin/armv7a-linux-androideabi$API-clang++
SYSROOT=$NDK/toolchains/llvm/prebuilt/linux-x86_64/sysroot
CROSS_PREFIX=$TOOLCHAIN/bin/arm-linux-androideabi-
PREFIX=$(pwd)/android/$CPU
OPTIMIZE_CFLAGS="-mfloat-abi=softfp -mfpu=vfp -marm -march=$CPU "
5.集成FFmpeg验证编译状态
Android 支持C++的ndk项目,有2中创建方式: 1.新建支持C++项目 2.在原有不支持C++的Android项目,新增C++项目需要的配置.
5.1创建支持c++的项目
File => New => NewProject,选择Native C++类型创建项目.
5.2 配置ffmpeg动态库
把第4步编译的头文件和库文件复制到项目cpp和jniLibs目录.
5.3 配置CMakeLists.txt
cmake_minimum_required(VERSION 3.18.1)
project("ffmpegtest")
set(ffmpeg_lib_dir ${CMAKE_SOURCE_DIR}/../jniLibs/${ANDROID_ABI})
set(ffmpeg_head_dir ${CMAKE_SOURCE_DIR}/ffmpeg)
include_directories(${ffmpeg_head_dir}/include)
add_library(
ffmpegtest
SHARED
native-lib.cpp)
add_library(avcodec
SHARED
IMPORTED)
set_target_properties(avcodec
PROPERTIES IMPORTED_LOCATION
${ffmpeg_lib_dir}/libavcodec.so)
add_library(avfilter
SHARED
IMPORTED)
set_target_properties(avfilter
PROPERTIES IMPORTED_LOCATION
${ffmpeg_lib_dir}/libavfilter.so)
add_library(avformat
SHARED
IMPORTED)
set_target_properties(avformat
PROPERTIES IMPORTED_LOCATION
${ffmpeg_lib_dir}/libavformat.so)
add_library(avutil
SHARED
IMPORTED)
set_target_properties(avutil
PROPERTIES IMPORTED_LOCATION
${ffmpeg_lib_dir}/libavutil.so)
add_library(swresample
SHARED
IMPORTED)
set_target_properties(swresample
PROPERTIES IMPORTED_LOCATION
${ffmpeg_lib_dir}/libswresample.so)
add_library(swscale
SHARED
IMPORTED)
set_target_properties(swscale
PROPERTIES IMPORTED_LOCATION
${ffmpeg_lib_dir}/libswscale.so)
find_library(
log-lib
log)
target_link_libraries(
ffmpegtest
avcodec
avfilter
avformat
avutil
swresample
swscale
${log-lib})
5.4 编写测试代码
在默认创建native接口的MainActivity类新建ffmpegInfo()方法,按alt+enter选择Create JNI function for ffmpegInfo,快速生成ffmpegInfo()在native-lib.cpp的方法实现. 这边简单调用ffmpeg库的方法,测试编译库文件是否正常.注意include头文件要放在extern “C” {}里面,不然会报 error: undefined reference to ‘av_codec_iterate(void**)’ error: undefined reference to ‘av_codec_is_decoder(AVCodec const*)’
native-lib.cpp
extern "C" {
#include "ffmpeg/include/libavcodec/avcodec.h"
JNIEXPORT jstring JNICALL
Java_com_moyihen_ffmpegtest_MainActivity_ffmpegInfo(JNIEnv *env, jobject thiz) {
char info[40000] = {0};
void *p = nullptr;
const AVCodec *c_temp;
while ((c_temp = av_codec_iterate(&p))) {
if (av_codec_is_decoder(c_temp)) {
sprintf(info, "%sdecode => %s\n", info, c_temp->name);
} else {
sprintf(info, "%sencode => %s\n", info, c_temp->name);
}
}
return env->NewStringUTF(info);
}
}
- 测试正常 能正常打印编解码器列表
end
|