当前位置:网站首页>NDK R21 compiles ffmpeg 4.2.2 (x86, x86_64, armv7, armv8)

NDK R21 compiles ffmpeg 4.2.2 (x86, x86_64, armv7, armv8)

2022-06-11 05:34:00 Come and go

1. compile FFmpeg

Get ready Ununtu、ndk r21(linux)、FFmpeg.

Prepare to compile the script , There are two , One of them is dedicated to armv7 Of .

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  Corresponding  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

stay FFmpeg Run the script from the root directory of . The initial run will prompt the lack of software , Just follow the prompts to install .

[email protected]:~/FFmpeg-n4.2.2$ sudo su
[sudo] qwe  Password : 
[email protected]:/home/qwe/FFmpeg-n4.2.2# ./build3.sh 

 Insert picture description here

2. Use FFmpeg Of so library

android studio Create project
 Insert picture description here
take so Copy the file to the following directory .include yes FFmpeg Of .h file ,x86、x86_64、armv7、armv8 Generated .h Same .
 Insert picture description here
edit build.gradle(:app)

android {
    
    .....
    sourceSets {
    
        main {
    //jniLibs
            jniLibs.srcDirs = ['src/main/libs']
        }
    }
}

edit cmakeList.txt

cmake_minimum_required(VERSION 3.4.1)

# Set up two directories ,${
    ANDROID_ABI} It will change according to the equipment 
set(DIR ${
    PROJECT_SOURCE_DIR}/../libs/${
    ANDROID_ABI})
set(DIR2 ${
    PROJECT_SOURCE_DIR}/../libs)
# Appoint h File directory , The compilation process needs to find .h file 
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)

# Import FFmpeg The library of 
add_library(libavcodec SHARED IMPORTED)
#so The location of the file 
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)

# Connection Library 
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)

change native-lib.cpp Medium stringFromJNI Method

#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());
}

Running effect
 Insert picture description here

原网站

版权声明
本文为[Come and go]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203020538239282.html