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/bash

API=21
#armv7-a
ARCH=armv7 

PREFIX=./SO/$ARCH

TOOLCHAIN=/home/qwe/android-ndk-r21/toolchains/llvm/prebuilt/linux-x86_64

build()
{
     
./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/bash

API=21
#arm64  x86 x86_64 对应 aarch64  i686  x86_64 
ARCH=arm64
ARCH2=aarch64    

PREFIX=./SO/$ARCH

TOOLCHAIN=/home/qwe/android-ndk-r21/toolchains/llvm/prebuilt/linux-x86_64

build()
{
     
./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 

NDK r21编译FFmpeg 4.2.2(x86、x86_64、armv7、armv8)_第1张图片

2.使用FFmpeg的so库

android studio创建项目
NDK r21编译FFmpeg 4.2.2(x86、x86_64、armv7、armv8)_第2张图片
将so文件复制到如下目录。include是FFmpeg的.h文件,x86、x86_64、armv7、armv8生成的.h一样的。
NDK r21编译FFmpeg 4.2.2(x86、x86_64、armv7、armv8)_第3张图片
编辑build.gradle(:app)

android {
     
    .....
    sourceSets {
     
        main {
     //jniLibs
            jniLibs.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(libavcodec
        PROPERTIES IMPORTED_LOCATION
        ${
     DIR}/libavcodec.so)


add_library(libavfilter SHARED IMPORTED)
set_target_properties(libavfilter
        PROPERTIES IMPORTED_LOCATION
        ${
     DIR}/libavfilter.so)

add_library(libavformat SHARED IMPORTED)
set_target_properties(libavformat
        PROPERTIES IMPORTED_LOCATION
        ${
     DIR}/libavformat.so)

add_library(libavutil SHARED IMPORTED)
set_target_properties(libavutil
        PROPERTIES IMPORTED_LOCATION
        ${
     DIR}/libavutil.so)

add_library(libswresample SHARED IMPORTED)
set_target_properties(libswresample
        PROPERTIES IMPORTED_LOCATION
        ${
     DIR}/libswresample.so)

add_library(libswscale SHARED IMPORTED)
set_target_properties(libswscale
        PROPERTIES IMPORTED_LOCATION
        ${
     DIR}/libswscale.so)

find_library( # Sets the name of the path variable.
        log-lib

        log)

#连接库
target_link_libraries( # Specifies the target library.
        native-lib

        # Links the target library to the log library
        # included in the NDK.
        ${
     log-lib}
        libavfilter
        libavformat
        libswresample
        libswscale
        libavutil
        libavcodec)

更改native-lib.cpp中的stringFromJNI方法

#include 
#include 

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)_第4张图片

你可能感兴趣的:(NDK,ffmpeg,android,studio,android,ndk)