参考资料:1.http://www.jb51.net/article/77720.htm
1.首先前往ffmpeg.org网站,进入download目录下,复制git命令。打开terminal,黏贴命令行回车,此时拷贝ffmpeg的源码:
git clone git://source.ffmpeg.org/ffmpeg.git ffmpeg2.将下载下来的ffmpeg目录复制到ndk的source目录下
3.找到目录下的config文件夹:替换下面的代码:
SLIBNAME_WITH_MAJOR='$(SLIBPREF)$(FULLNAME)-$(LIBMAJOR)$(SLIBSUF)'
LIB_INSTALL_EXTRA_CMD='$$(RANLIB)"$(LIBDIR)/$(LIBNAME)"'
SLIB_INSTALL_NAME='$(SLIBNAME_WITH_MAJOR)'
SLIB_INSTALL_LINKS='$(SLIBNAME)'
4.在ffmpeg根目录建立编译脚本文件 build_android.sh
#!/bin/bash
NDK=/Users/whyus/Library/Android/sdk/ndk-bundle
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
5.然后切换到ffmpeg根目录,再chmod a+x build_android.sh 授权,然后 ./build_android.sh 运行脚本,此时开始编译生成so文件,编译完成后在根目录下会生成android目录,包含include和lib.
6.新建立一个测试工程。配置ndk开发,参考上一篇(使用自动编译来配置ndk开发环境),然后增加sourceSets.main.jni.srcDirs = [] 来禁止自动编译,gradle配置如下:
android { compileSdkVersion 23 buildToolsVersion "23.0.2" sourceSets.main.jni.srcDirs = [] defaultConfig { applicationId "cn.eqiyue.myffmpeg" minSdkVersion 16 targetSdkVersion 23 versionCode 1 versionName "1.0" ndk{ moduleName "myffmpeg" abiFilter "armeabi" ldLibs "log", "z", "m", "jnigraphics", "android" } }
7.定义java层调用ffmpeg的类,并申明方法,如下:
/** * Created by kemp on 5/3/16. */ public class FFmpegUtils { public static native String getInfo(); }
8.在activity中使用如下,需要先加载库:
public class MainActivity extends AppCompatActivity { static { System.loadLibrary("myffmpeg"); System.loadLibrary("avutil-55"); System.loadLibrary("avcodec-57"); System.loadLibrary("avformat-57"); System.loadLibrary("swresample-2"); System.loadLibrary("swscale-4"); System.loadLibrary("postproc-53"); System.loadLibrary("avfilter-6"); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView tv = new TextView(this); tv.setText(FFmpegUtils.getInfo()); setContentView(tv); } }9.然后根据上一篇博客一样编译该类。此时同样生成了jni目录和头文件,再把编译好的文件放入该目录.
10.实现c文件代码。
11.在jni目录下建立mk文件:
LOCAL_PATH := $(call my-dir)
# FFmpeg library
include $(CLEAR_VARS)
LOCAL_MODULE := avcodec
LOCAL_SRC_FILES := lib/libavcodec-57.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := avfilter
LOCAL_SRC_FILES := lib/libavfilter-6.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := avformat
LOCAL_SRC_FILES := lib/libavformat-57.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := avutil
LOCAL_SRC_FILES := lib/libavutil-55.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := swresample
LOCAL_SRC_FILES := lib/libswresample-2.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := swscale
LOCAL_SRC_FILES := lib/libswscale-4.so
include $(PREBUILT_SHARED_LIBRARY)
# Program
include $(CLEAR_VARS)
LOCAL_MODULE := myffmpeg
LOCAL_SRC_FILES := ffmpegutils.c
LOCAL_C_INCLUDES += $(LOCAL_PATH)/include
LOCAL_LDLIBS := -llog -lz
LOCAL_SHARED_LIBRARIES := avcodec avdevice avfilter avformat avutil postproc swresample swscale
include $(BUILD_SHARED_LIBRARY)
12.然后自己使用terminal去编译刚刚自己定义的mk文件,生成的文件在build/intermediates/ndk/debug下可以看到。
/Users/whyus/Library/Android/sdk/ndk-bundle/ndk-build NDK_PROJECT_PATH=null APP_BUILD_SCRIPT=/Users/whyus/Downloads/MyFFmpeg2/app/src/main/jni/Android.mk APP_PLATFORM=android-21 NDK_OUT=/Users/whyus/Downloads/MyFFmpeg2/app/build/intermediates/ndk/debug/obj NDK_LIBS_OUT=/Users/whyus/Downloads/MyFFmpeg2/app/build/intermediates/ndk/debug/lib APP_ABI=armeabi
13.运行可以成功看到调用ffmpeg的信息。
错误信息:
1.报找不到so的文件,在mk文件中声明出错
2.打包的时候build/intermediates/ndk/debug下的文件是否存在。