准备
1.下载最新的ndk,ffmpeg包,android studio
一.因为ffmpeg是c,c++的东西所以通过ndk编译成os文件供开发使用
1-> 解压ndk到桌面路径: /Users/用户名/Desktop/android-ndk-r12b(后面idk路径用”&NDK_PATH"表示)
2->将ffmpeg包解压到: &NDK_PATH/sources/ffmpeg-3.1.1
3.->修改ffmpeg-3.1.1目录下的configure文件,目的是将生成的库类文件名字libavcodec.so.56的56版本号去掉(可用文本编辑器打开修改)
找到
修改成
4->编译FFmpeg
·在ffmpeg-3.1.1创建文件build_android.sh
·添加内容
#!/bin/bash
NDK=&NDK_PATH(NDK路径)
SYSROOT=$NDK/platforms/android-21/arch-arm/(删:指定NDK所用的编译平台)
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
·保存后在终端执行
cd /…/…/ffmpeg3.1.3(idk中ffmpeg的文件夹路径)
sudo chmod +x build_android.sh
./build_android.sh
编译需要等待上一段时间,编译成功后会出现下面这样一个目录
其中libavcodec.so、libavfilter.so、libavformat.so、libavutil.so、libswresample.so、libswscale.so都是软链,没有用,可以删掉
·然后在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)
# FFmpeg 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-54.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)
# Program
include $(CLEAR_VARS)
LOCAL_MODULE := herobayboomerangLive
LOCAL_SRC_FILES :=com.herobay.liangchao.herobayboomerang.MainActivity.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)
要注意其中.so前面的数字应该改成你的ffmpeg版本编译出来的数字
到这里就完成了ffmpeg库文件的编译可以再android项目中使用了
二.在android studio中进行使用
1.将下面的内容添加到项目中的jni文件夹中
2.在local.properties文件配置ndk路径
ndk.dir=&NDK_PATH 如:
3.配置app的build.gradle文件
·添加这一段禁止自动ndk-build
sourceSets.main.jni.srcDirs = []
·添加这一段让它知道调用哪个库
ndk {
abiFilter "armeabi"
moduleName "ovsplayer"
ldLibs "log", "z", "m", "jnigraphics", "android"
}
如:
4.在grade.properties文件中添加:android.useDeprecatedNdk=true
5.进入Project中的src/main/jni文件夹,修改其中的Application.mk文件,添加:APP_PLATFORM := android-21(因为该example采用-ljnigraphics默认库,这个库在android-3中没有,如果不声明的话,idk会默认采用android-3进行build)
接下来就可以开始FFmpeg的使用了