Mac下编译FFMPEG for Android

前面讲到了自定义NDK编译工具链为编译FFMPEG提供环境,

目录

编译环境

操作系统:mac os 10.14.2
NDK版本:android-ndk-r20
编译器:clang
FFMPEG版本:4.2.1

编译脚本

  • 整理了一个编译环境的脚本build.sh,放在FFMPEG目录下
#!/bin/sh

# Android NDK sources and standalone toolchain is put here
DEV=~/Library/Android/sdk
NDK_HOME=~/Library/Android/sdk/ndk-bundle
#自定义NDK toolchain目录
CHAIN_ENV=${NDK_HOME}/Chain/android-toolchain

# Set Android target arch
ANDROID_ARCH=arm

# All the built binaries, libs and their header will be installed here
PREFIX=~

# static or share libs dir
OUT_PUT=${PREFIX}/android-output

# Don't mix up .pc files from your host and build target
PKG_CONFIG_PATH=${PREFIX}/lib/pkgconfig

# Set Android target API level
ANDROID_API=21

# The cross-compile toolchain we use
TOOLCHAIN=${ANDROID_ARCH}-linux-androideabi

# The path of standalone NDK toolchain
# Refer to https://developer.android.com/ndk/guides/standalone_toolchain.html
NDK_TOOLCHAIN=${CHAIN_ENV}/${ANDROID_ARCH}


# Set Android Sysroot according to API and arch
SYSROOT=${NDK_TOOLCHAIN}/sysroot
# this one is the absolute, prebuilt path

# Binutils path
CROSS_PREFIX=${NDK_TOOLCHAIN}/bin/${TOOLCHAIN}
# this one is the absolute, prebuilt path

# Set build flags
# Refer to https://developer.android.com/ndk/guides/standalone_toolchain.html
CFLAGS="--sysroot=${SYSROOT} -I${SYSROOT}/usr/include -I${PREFIX}/include -fPIE -DANDROID -Wno-multichar"
CXXFLAGS=${CFLAGS}
CPPFLAGS="--sysroot=${SYSROOT} -I${SYSROOT}/usr/include -I${NDK_TOOLCHAIN}/include/c++/ -DANDROID -DNO_XMALLOC -mandroid"
LIBS="-lgcc"
LDFLAGS="-Wl,-rpath-link=-I${SYSROOT}/usr/lib -L${SYSROOT}/usr/lib -L${PREFIX}/lib -L${NDK_TOOLCHAIN}/lib"
#执行configure脚本,用于生成makefile
#--prefix : 安装目录
#--enable-small : 优化大小
#--disable-programs : 不编译ffmpeg程序(命令行工具),我们是需要获得静态(动态)库。
#--disable-avdevice : 关闭avdevice模块,此模块在android中无用
#--disable-encoders : 关闭所有编码器 (播放不需要编码)
#--disable-muxers :  关闭所有复用器(封装器),不需要生成mp4这样的文件,所以关闭
#--disable-filters :关闭视频滤镜
#--enable-cross-compile : 开启交叉编译(ffmpeg比较**跨平台**,并不是所有库都有这么happy的选项 )
#--cross-prefix: 看右边的值应该就知道是干嘛的,gcc的前缀 xxx/xxx/xxx-gcc 则给xxx/xxx/xxx-
#disable-shared enable-static 不写也可以,默认就是这样的。
#--sysroot: 
#--extra-cflags: 会传给gcc的参数
#--arch --target-os : 不给不行
./configure \
--enable-static \
--disable-shared \
--disable-doc \
--disable-ffmpeg \
--disable-ffplay \
--disable-ffprobe \
--disable-avdevice \
--disable-doc \
--disable-symver \
--prefix=${OUT_PUT}/ffmpeg/${ANDROID_ARCH} \
--target-os=android \
--arch=${ANDROID_ARCH} \
--enable-cross-compile \
--sysroot=${SYSROOT} \
--cross-prefix=${CROSS_PREFIX}- \
--extra-cflags="${CFLAGS}" \
--extra-ldflags="${LDFLAGS}" \
--extra-libs="${LIBS}" \
--extra-cxxflags="${CXXFLAGS}" 

make clean

make install
  • 增加执行权限并执行脚本

结果

在输出目录下找到我们编译的lib及include目录
在这里插入图片描述

动态库生成

如果想要动态库的话需要更改配置
配置–disable-static --enable-shared

为了生成的so可以被Android平台识别,修改configure文件

SLIBNAME_WITH_MAJOR='$(SLIBNAME).$(LIBMAJOR)'
LIB_INSTALL_EXTRA_CMD='$$(RANLIB)"$(LIBDIR)/$(LIBNAME)"'
SLIB_INSTALL_NAME='$(SLIBNAME_WITH_VERSION)'
SLIB_INSTALL_LINKS='$(SLIBNAME_WITH_MAJOR)$(SLIBNAME)'

改成

SLIBNAME_WITH_MAJOR='$(SLIBPREF)$(FULLNAME)-$(LIBMAJOR)$(SLIBSUF)'
LIB_INSTALL_EXTRA_CMD='$$(RANLIB)"$(LIBDIR)/$(LIBNAME)"'
SLIB_INSTALL_NAME='$(SLIBNAME_WITH_MAJOR)'
SLIB_INSTALL_LINKS='$(SLIBNAME)'
发布了26 篇原创文章 · 获赞 0 · 访问量 1118

猜你喜欢

转载自blog.csdn.net/Plx0303sunny/article/details/103479177