Android编译FFmpeg

1、环境配置

  • 1.系统:Linux虚拟机
Distributor ID:	Ubuntu
Description:	Ubuntu 18.04.2 LTS
Release:	18.04
Codename:	bionic

  • 2.FFmpeg 版本4.0.4,点击下载

2、脚本命令

根据自己需求添加编写脚本,这里只是简单的使用

#!/bin/bash
echo "进入编译ffmpeg脚本"
NDK=/home/hou/NDK/android-ndk-r14b
PLATFORM=$NDK/platforms/android-21/arch-arm
TOOLCHAIN=$NDK/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64
CPU=armv7-a
#输出路径
PREFIX=./android/$CPU
function build
{
	echo "start ffmpeg"
	./configure \
    --prefix=$PREFIX \
    --target-os=android \
    --cross-prefix=$TOOLCHAIN/bin/arm-linux-androideabi- \
    --arch=arm \
    --cpu=armv7-a  \
    --sysroot=$PLATFORM \
    --extra-cflags="-I$PLATFORM/usr/include -fPIC -DANDROID -mfpu=neon -mfloat-abi=softfp " \
    --cc=$TOOLCHAIN/bin/arm-linux-androideabi-gcc \
    --nm=$TOOLCHAIN/bin/arm-linux-androideabi-nm \
    --enable-shared \
    --enable-runtime-cpudetect \
    --enable-gpl \
    --enable-small \
    --enable-cross-compile \
    --enable-asm \
    --enable-neon \
    --enable-jni \
    --enable-mediacodec \
    --enable-decoder=h264_mediacodec \
    --enable-hwaccel=h264_mediacodec \
    --disable-debug \
    --disable-static \
    --disable-doc \
    --disable-ffmpeg \
    --disable-ffplay \
    --disable-ffprobe \
    --disable-postproc \
    --disable-avdevice \
    --disable-symver \
    --disable-stripping 
    make -j16
    make install
	echo "end!"
}

build

注意: 给脚本添加权限,我直接给了777

chmod 777 bulid_android.sh
./bulid_android.sh   //运行脚本

编译出结果有对应的 so 和 include 文件夹

3、创建JNI 项目

3.1 项目配置

3.1.1 将2中生成的文件拷贝到项目中

1. 将include复制到项目中
2..so复制到libs

3.1.2 配置项目的build

android {
    compileSdkVersion 28
    defaultConfig {
        ...
        externalNativeBuild {
            cmake {
                cppFlags "-std=c++11"
            }
            ndk{
                abiFilters("armeabi-v7a")
            }
        }
        sourceSets{
            main{
                jniLibs.srcDirs=['libs']
            }
        }
    }
  ...
    externalNativeBuild {
        cmake {
            path "CMakeLists.txt"
        }
    }
}

3.1.3 配置项目的CMakeLists.txt

# 添加头文件路径
include_directories(include)
#设置ffmpeg库所在路径的变量
set(FFMPEG ${CMAKE_CURRENT_SOURCE_DIR}/libs/${ANDROID_ABI})
#设置ffmpeg 导入库
add_library(avcodec SHARED IMPORTED)
#导入库那个库
set_target_properties(avcodec PROPERTIES IMPORTED_LOCATION ${FFMPEG}/libavcodec.so)


target_link_libraries( # Specifies the target library.
                       native-lib
                       android
                       avcodec
                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )

3.2 引用avcodec.h

#include 
#include 
#include 
extern "C"{
    #include 
}

extern "C" JNIEXPORT jstring JNICALL
Java_com_billshuai_testplayer_MainActivity_stringFromJNI(
        JNIEnv* env,
        jobject /* this */) {
    std::string hello = "Hello from C++";
    hello += avcodec_configuration();
    return env->NewStringUTF(hello.c_str());
}

你可能感兴趣的:(FFmpeg)