前言
编译FFmpeg的Android版本,并整合到Android Studio。
系统:macOS Sierra 10.12。
NDK版本:r12b。
FFmpeg版本:3.1.3
编译FFmpeg的步骤
-
下载 NDK 。
-
下载 ffmpeg源码 ,解压到NDK目录下的sources目录下。如图
WechatIMG9.jpeg
- 修改ffmpeg-3.1.3目录下的configure文件 定位到3309行:如图:
WechatIMG11.jpeg
将
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)'
目的是将生成.so名称规范化,用以满足android build。
-
在当前目录下新建一个build_android.sh文件,如图:
WechatIMG13.jpeg
内容为:
#!/bin/bash
NDK=/Users/maimingliang/Documents/NDK/android-ndk-r12b
SYSROOT=$NDK/platforms/android-15/arch-arm/
TOOLCHAIN=$NDK/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64
function build_one
{
./configure /
--prefix=$PREFIX /
--enable-shared /
--disable-static /
--disable-doc /
--disable-ffmpeg /
--disable-ffplay /
--disable-ffprobe /
--disable-ffserver /
--disable-avdevice /
--disable-doc /
--disable-symver /
--cross-prefix=$TOOLCHAIN/bin/arm-linux-androideabi- /
--target-os=linux /
--arch=arm /
--enable-cross-compile /
--sysroot=$SYSROOT /
--extra-cflags="-Os -fpic $ADDI_CFLAGS" /
--extra-ldflags="$ADDI_LDFLAGS" /
$ADDITIONAL_CONFIGURE_FLAG
make clean
make
make install
}
CPU=arm
PREFIX=$(pwd)/android/$CPU
ADDI_CFLAGS="-marm"
build_one
-
打开终端并且把路径切换到build_android 所在的目录 ,执行命令
./build_android.sh
完成编译以后,进入生成的android文件夹,在arm文件夹里面新建一个Android.mk文件,内容为:
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE:= libavcodec
LOCAL_SRC_FILES:= lib/libavcodec-57.so
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE:= libavformat
LOCAL_SRC_FILES:= lib/libavformat-57.so
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE:= libswscale
LOCAL_SRC_FILES:= lib/libswscale-4.so
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE:= libavutil
LOCAL_SRC_FILES:= lib/libavutil-55.so
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE:= libavfilter
LOCAL_SRC_FILES:= lib/libavfilter-6.so
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE:= libswresample
LOCAL_SRC_FILES:= lib/libswresample-2.so
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
include $(PREBUILT_SHARED_LIBRARY)
至此FFmpeg的编译完成,下面整合到Android Studio 中:
配置Android Studio
- 在local.properties加入ndk的路径,我这里是:
ndk.dir=/Users/maimingliang/Documents/NDK/android-ndk-r12b
也可以在project structure 中添加。如图所示:
WechatIMG14.jpeg
-
在build.gradle 文件中添加图中标红处的内容。
WechatIMG21.jpeg
-
配置javah,ndk-build ,ndk-build clean 命令
WechatIMG15.jpeg
添加javah命令:
![Uploading WechatIMG17_232544.jpeg . . .]
program:$JDKPath$/bin/javah
paramers:-classpath $Classpath$ -v -jni $FileClass$
working diretory : $ModuleFileDir$/src/main/jni
添加 ndk-build
IMG_4113.JPG
program:/Users/maimingliang/Documents/NDK/android-ndk-r12b/build/ndk-build //这里要修改你的ndk路径 的ndk-build
paramers:
working diretory : $ModuleFileDir$/src/main/jni
添加 ndk-build clean
IMG_4114.JPG
program:/Users/maimingliang/Documents/NDK/android-ndk-r12b/build/ndk-build //这里要修改你的ndk路径的 ndk-build
paramers:clean
working diretory : $ProjectFileDir$/app/src/main
- 编写 C文件
- 编写Android.mk文件
- 编写Application.mk文件。
- 运行 javah,ndk-build ndk-build clean 命令
IMG_4115.JPG
IMG_4116.JPG
来自:http://www.jianshu.com/p/42c82169bb7d