前言:要使用ffmpeg就是用它的各种库,这里用动态so库,使用centos7编译。
1.租一台云服务器,部署centos7
2.wget xxxxffmpeg安装包地址 //这里我使用3.3的
3.tar -xzf xxx安装包 //解压
4.mkdir ndkbuild 并进入此目录 /
5.wget 下载ndk包 //这里选择14b的
6.unzip 安装包
7.cd 退到根目录后:vim ~/.bashrc 进入后改为:
export NDKROOT=/root/ndkbuild/android-ndk-r14b//配置你的安装路径
export PATH=$NDKROOT:$PATH//配置路径
保存退出:wq!
8 :source ~/.bashrc //执行后修改环境变量
9.进入ffmpeg包目录,新建vim android_build.sh,并写入
#!/bin/bashecho "进入FFmpeg编译脚本"export NDK=/root/ndkbuild/android-ndk-r14b
export SYSROOT=$NDK/platforms/android-21/arch-arm/
export TOOLCHAIN=$NDK/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64
PREFIX=android-buildCOMMON_OPTIONS="\--prefix=android/ \--target-os=android \--enable-shared \--enable-runtime-cpudetect \--enable-small \--disable-static \--disable-debug \--disable-ffmpeg \--disable-ffplay \--disable-ffprobe \--disable-ffserver \--disable-doc \--disable-symver \--disable-asm \--disable-stripping \--disable-armv5te \"function build_android {echo "开始编译FFmpeg..."# armeabiecho "开始编译FFmpeg(armeabi)"./configure \--libdir=${PREFIX}/libs/armeabi \--incdir=${PREFIX}/include/armeabi \--pkgconfigdir=${PREFIX}/pkgconfig/armeabi \--arch=arm \--cpu=armv6 \--cross-prefix=$TOOLCHAIN/bin/arm-linux-androideabi- \--sysroot=$SYSROOT \--extra-ldexeflags=-pie \${COMMON_OPTIONS}make cleanmake -j8 && make installecho "结束编译FFmpeg(armeabi)"# armeabi-v7aecho "开始编译FFmpeg(armeabi-v7a)"./configure \--libdir=${PREFIX}/libs/armeabi-v7a \--incdir=${PREFIX}/include/armeabi-v7a \--pkgconfigdir=${PREFIX}/pkgconfig/armeabi-v7a \--arch=arm \--cpu=armv7-a \--cross-prefix=$TOOLCHAIN/bin/arm-linux-androideabi- \--sysroot=$SYSROOT \--extra-cflags="-march=armv7-a -mfloat-abi=softfp -mfpu=neon" \--extra-ldexeflags=-pie \${COMMON_OPTIONS}make cleanmake -j8 && make installecho "结束编译FFmpeg(armeabi-v7a)"# arm64-v8aecho "开始编译FFmpeg(arm64-v8a)"./configure \--libdir=${PREFIX}/libs/arm64-v8a \--incdir=${PREFIX}/include/arm64-v8a \--pkgconfigdir=${PREFIX}/pkgconfig/arm64-v8a \--arch=aarch64 \--cpu=armv8-a \--cross-prefix=$TOOLCHAIN/bin/arm-linux-androideabi- \--sysroot=$SYSROOT \--extra-ldexeflags=-pie \${COMMON_OPTIONS} make cleanmake -j8 && make installecho "结束编译FFmpeg(arm64-v8a)"# x86_64echo "开始编译FFmpeg(x86_64)"./configure \--libdir=${PREFIX}/libs/x86_64 \--incdir=${PREFIX}/include/x86_64 \--pkgconfigdir=${PREFIX}/pkgconfig/x86_64 \--arch=x86_64 \--cpu=x86_64 \--cross-prefix=$TOOLCHAIN/bin/arm-linux-androideabi- \--sysroot=$SYSROOT \--extra-ldexeflags=-pie \${COMMON_OPTIONS}make cleanmake -j8 && make installecho "结束编译FFmpeg(x86_64)"echo "编译结束"};
build_android
10.给android_build.sh修改权限,添加执行权限
chmod +x android_build.sh
11.修改ffmpeg包目录下的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)'
最后在ffmpeg包目录下执行sh文件
./android_build.sh
编译后ffmpeg包下会多一个android-build目录,里面就是include目录下得到各种架构的头文件和libs目录下的各种架构的so动态库
二:将ffmpeg动态库移植到安卓工程中
1.新建一个ndk项目
2.将CmakeLists.txt文件移动到和gradle文件同目录下(即app目录下)
3.将CmakeLists .txt文件修改为:
cmake_minimum_required(VERSION 3.4.1)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).src/main/cpp/native-lib.cpp)find_library( log-liblog )include_directories(libs/include)
set(DIR ../../../../libs)
add_library(avcodecSHAREDIMPORTED)
set_target_properties(avcodecPROPERTIES IMPORTED_LOCATION${DIR}/armeabi-v7a/libavcodec.so)add_library(avdeviceSHAREDIMPORTED)
set_target_properties(avdevicePROPERTIES IMPORTED_LOCATION${DIR}/armeabi-v7a/libavdevice.so)
add_library(avformatSHAREDIMPORTED)
set_target_properties(avformatPROPERTIES IMPORTED_LOCATION${DIR}/armeabi-v7a/libavformat.so)
add_library(avutilSHAREDIMPORTED)
set_target_properties(avutilPROPERTIES IMPORTED_LOCATION${DIR}/armeabi-v7a/libavutil.so)
add_library(swresampleSHAREDIMPORTED)
set_target_properties(swresamplePROPERTIES IMPORTED_LOCATION${DIR}/armeabi-v7a/libswresample.so)
add_library(swscaleSHAREDIMPORTED)
set_target_properties(swscalePROPERTIES IMPORTED_LOCATION${DIR}/armeabi-v7a/libswscale.so)
add_library(avfilterSHAREDIMPORTED)
set_target_properties(avfilterPROPERTIES IMPORTED_LOCATION${DIR}/armeabi-v7a/libavfilter.so)
target_link_libraries( native-libavfilteravcodecavdeviceavformatavutilswresampleswscale${log-lib}android)
4.修改gradle文件,:
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"sourceSets {main {jniLibs.srcDirs = ['libs']jni.srcDirs = []}}externalNativeBuild {cmake {cppFlags ""abiFilters 'armeabi-v7a'}}ndk {abiFilters 'armeabi-v7a'}
5.在libs文件下新建armeabi-v7a目录,将编译后的armeabi-v7a目录下的so库复制到armeabi-v7a目录下
6.在libs文件下新建include目录,将编译后include\armeabi-v7a目录的文件全部复制到include目录下
整体项目结构如图:
到此就可以了,后面就是写代码实现视频播放了,发现播放好慢,后面看看别人写的模仿学习吧,毕竟刚学,哈哈。
参考1
参考2