linux的NDK交叉编译FFmpeg

390 阅读1分钟

一、ffmpeg下载

1.命令下载:

   wget http://www.ffmpeg.org/releases/ffmpeg-4.0.2.tar.bz2
   

2.解压:

  tar -xjf ffmpeg-4.0.2.tar.bz2 

二、ffmpeg帮助文档

1. 显示帮助文档./configure --help

image.png

2. 导出帮助文档:./configure --help -> ffmpeg_help.txt

三、编译FFmpeg

注意:

1.权限 root 必须是root

2.手动创建输出的文件夹 /android/arm

1. ./configure 【报错,需要关闭 asm 汇编相关】

image.png

2. ./configure --disable-x86asm 【生成 Makefile 已经帮你关闭 asm 】

四、编译anroid能用的静态库。

ffmpeg版本:ffmpeg-4.0.2

ndk版本:android-ndk-r17c

编译好的armeabi-v7a的静态库:download.csdn.net/download/lz…


#!/bin/bash

NDK_ROOT=/root/liuzhaoliang/ndk/android-ndk-r17c

# 交叉编译GCC所在路径
TOOLCHAIN=$NDK_ROOT/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64


#从as的 externalNativeBuild/xxx/build.ninja,  下面的配置,可以压制警告的意思
FLAGS="-isystem $NDK_ROOT/sysroot/usr/include/arm-linux-androideabi -D__ANDROID_API__=21 -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16 -mthumb -Wa,--noexecstack -Wformat -Werror=format-security  -O0 -fPIC"
INCLUDES=" -isystem $NDK_ROOT/sources/android/support/include"


PREFIX=./android/arm

# 2.--enable-small 优化大小 非常重要,必须优化才行的哦
# 3.--disable-programs 不编译ffmpeg程序(命令行工具),我们是需要获取静态、动态库
# 4.--disable-avdevice 关闭avdevice模块,此模块在android中无用
# 5.--disable-encoders 关闭所有编码器(播放不需要编码)
# 6.--disable-muxers 关闭所有复用器(封装器),不需要生成mp4这样的文件,所以关闭
# 7.--disable-filters 关闭所有滤镜
# 8.--enable-cross-compile 开启交叉编译(ffmpeg是跨平台的,注意:并不是所有库都有这么happy的选项)
# 9.--cross-prefix 看右边的值就知道是干嘛的,gcc的前缀..(前缀可以找到后续的所有)
# 10.disable-shared / 寻找ndk里的库文件
# 12.--extra-cflags 会传给gcc的参数,每个版本,每个平台都不一样 【最多问题的点】
# 13.--arch  --target-os 

# \后面不能有空格
# \后面不能有注释

./configure \
--prefix=$PREFIX \
--enable-small \
--disable-programs \
--disable-avdevice \
--disable-encoders \
--disable-muxers \
--disable-filters \
--enable-cross-compile \
--cross-prefix=$TOOLCHAIN/bin/arm-linux-androideabi- \
--disable-shared \
--enable-static \
--sysroot=$NDK_ROOT/platforms/android-21/arch-arm \
--extra-cflags="$FLAGS $INCLUDES" \
--extra-cflags="-isysroot $NDK_ROOT/sysroot/" \
--arch=arm \
--target-os=android

make clean

make install

五、android集成编译好的静态库

1. 在app的build.gradle里添加


externalNativeBuild {
    cmake {
        abiFilters 'armeabi-v7a'
    }
}

ndk{
    abiFilters 'armeabi-v7a'
}

2.把静态库和头文件添加到cpp目录下

image.png

3. 编写CMakeLists.txt

# 导入头文件
include_directories("include")

# 批量导入所有源文件
file(GLOB allcpp *.h *.c *.cpp)

add_library( # Sets the name of the library.
             native-lib

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             ${allcpp} )


#导入库文件
# CMAKE_SOURCE_DIR cmk的路径
#CMAKE_ANDROID_ARCH_ABI 获取当前平台的架构。
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -L${CMAKE_SOURCE_DIR}/${CMAKE_ANDROID_ARCH_ABI}")


find_library( # Sets the name of the path variable.
              log-lib

              # Specifies the name of the NDK library that
              # you want CMake to locate.
              log )


target_link_libraries( # Specifies the target library.
                       native-lib
                        # 第一种处理方式必须按照下面的顺序
                        avformat avcodec avfilter avutil swresample swscale

                        # 第二种方法,忽略顺序问题
                        #-Wl,--start-group
                        # avformat avcodec avfilter avutil swresample swscale
                        #-Wl,--end-group
                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )