本文主要是介绍NDK r21编译FFmpeg 4.2.2(x86、x86_64、armv7、armv8),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
文章目录
- 1.编译FFmpeg
- 2.使用FFmpeg的so库
1.编译FFmpeg
准备Ununtu、ndk r21(linux)、FFmpeg。
准备编译脚本,这里有两个,其中一个是专门针对armv7的。
armv7
#!/bin/bashAPI=21
#armv7-a
ARCH=armv7 PREFIX=./SO/$ARCHTOOLCHAIN=/home/qwe/android-ndk-r21/toolchains/llvm/prebuilt/linux-x86_64build()
{
./configure \
--prefix=$PREFIX \
--disable-static \
--enable-shared \
--enable-small \
--enable-gpl \
--disable-doc \
--disable-programs \
--disable-avdevice \
--enable-cross-compile \
--target-os=android \
--arch=$ARCH \
--cc=$TOOLCHAIN/bin/armv7a-linux-androideabi$API-clang \
--cross-prefix=$TOOLCHAIN/bin/arm-linux-androideabi- make clean
make -j4
make install}build
arm64 x86 x86_64
#!/bin/bashAPI=21
#arm64 x86 x86_64 对应 aarch64 i686 x86_64
ARCH=arm64
ARCH2=aarch64 PREFIX=./SO/$ARCHTOOLCHAIN=/home/qwe/android-ndk-r21/toolchains/llvm/prebuilt/linux-x86_64build()
{
./configure \
--prefix=$PREFIX \
--disable-static \
--enable-shared \
--enable-small \
--disable-doc \
--disable-programs \
--disable-avdevice \
--enable-cross-compile \
--target-os=android \
--arch=$ARCH \
--cc=$TOOLCHAIN/bin/$ARCH2-linux-android$API-clang \
--cross-prefix=$TOOLCHAIN/bin/$ARCH2-linux-android- make clean
make -j4
make install}build
在FFmpeg的根目录运行脚本。最初运行会提示缺少软件,按提示进行安装就行。
qwe@ubuntu:~/FFmpeg-n4.2.2$ sudo su
[sudo] qwe 的密码:
root@ubuntu:/home/qwe/FFmpeg-n4.2.2# ./build3.sh
2.使用FFmpeg的so库
android studio创建项目
将so文件复制到如下目录。include是FFmpeg的.h文件,x86、x86_64、armv7、armv8生成的.h一样的。
编辑build.gradle(:app)
android {.....sourceSets {main {//jniLibsjniLibs.srcDirs = ['src/main/libs']}}
}
编辑cmakeList.txt
cmake_minimum_required(VERSION 3.4.1)#设置两个目录,${ANDROID_ABI}会根据设备变化
set(DIR ${PROJECT_SOURCE_DIR}/../libs/${ANDROID_ABI})
set(DIR2 ${PROJECT_SOURCE_DIR}/../libs)
#指定h文件的目录,编译过程需要找.h文件
include_directories(${DIR2}/include)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).native-lib.cpp)#导入FFmpeg的库
add_library(libavcodec SHARED IMPORTED)
#so文件的位置
set_target_properties(libavcodecPROPERTIES IMPORTED_LOCATION${DIR}/libavcodec.so)add_library(libavfilter SHARED IMPORTED)
set_target_properties(libavfilterPROPERTIES IMPORTED_LOCATION${DIR}/libavfilter.so)add_library(libavformat SHARED IMPORTED)
set_target_properties(libavformatPROPERTIES IMPORTED_LOCATION${DIR}/libavformat.so)add_library(libavutil SHARED IMPORTED)
set_target_properties(libavutilPROPERTIES IMPORTED_LOCATION${DIR}/libavutil.so)add_library(libswresample SHARED IMPORTED)
set_target_properties(libswresamplePROPERTIES IMPORTED_LOCATION${DIR}/libswresample.so)add_library(libswscale SHARED IMPORTED)
set_target_properties(libswscalePROPERTIES IMPORTED_LOCATION${DIR}/libswscale.so)find_library( # Sets the name of the path variable.log-liblog)#连接库
target_link_libraries( # Specifies the target library.native-lib# Links the target library to the log library# included in the NDK.${log-lib}libavfilterlibavformatlibswresamplelibswscalelibavutillibavcodec)
更改native-lib.cpp中的stringFromJNI方法
#include <jni.h>
#include <string>extern "C"{
#include "libavcodec/avcodec.h"
}extern "C" JNIEXPORT jstring JNICALL
Java_cn_study_testffmpeg_MainActivity_stringFromJNI(JNIEnv *env,jobject /* this */) {std::string hello = avcodec_configuration();return env->NewStringUTF(hello.c_str());
}
运行效果
这篇关于NDK r21编译FFmpeg 4.2.2(x86、x86_64、armv7、armv8)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!