Linux中NDK编译FFmpeg,在AndroidStudio导入

本文介绍了如何在Linux环境下下载FFmpeg的历史版本,并详细阐述了编译过程,包括创建目录、配置NDK路径、关闭ASM、执行shell脚本等步骤。最终,将编译好的FFmpeg导出到Android项目中,供Android Studio使用,并展示了CMakeLists.txt配置和native-lib.cpp的示例代码。
摘要由CSDN通过智能技术生成

下载FFmpeg

a) FFmpeg历史版本:

    http://www.ffmpeg.org/releases/

b) 下载及解压

[root@VM-32-17-centos ~]# mkdir my-ffmpeg

[root@VM-32-17-centos ~]# cd my-ffmpeg/

[root@VM-32-17-centos my-ffmpeg]# wget wget http://www.ffmpeg.org/releases/ffmpeg-4.0.2.tar.bz2

[root@VM-32-17-centos my-ffmpeg]# tar -xjf ffmpeg-4.0.2.tar.bz2 

[root@VM-32-17-centos my-ffmpeg]# ls -lh
total 10M
drwx------ 18 mysql mysql 4.0K Jul 18  2018 ffmpeg-4.0.2
-rw-r--r--  1 root  root   10M Jul 18  2018 ffmpeg-4.0.2.tar.bz2

[root@VM-32-17-centos my-ffmpeg]# cd ffmpeg-4.0.2/

[root@VM-32-17-centos ffmpeg-4.0.2]# tree -L 1
.
|-- Changelog
|-- compat
|-- configure
|-- CONTRIBUTING.md
|-- COPYING.GPLv2
|-- COPYING.GPLv3
|-- COPYING.LGPLv2.1
|-- COPYING.LGPLv3
|-- CREDITS
|-- doc
|-- ffbuild
|-- fftools
|-- INSTALL.md
|-- libavcodec   用于各种类型声音/图象编解码
|-- libavdevice
|-- libavfilter
|-- libavformat  用于各种音视频封装格式的生成和解析,包括获取解码所需信息以生成解码上下文结构和读取音视频帧等功能
|-- libavresample
|-- libavutil    公共的工具函数
|-- libpostproc  用于后期效果处理
|-- libswresample
|-- libswscale   用于视频场景比例缩放、色彩映射转换
|-- LICENSE.md
|-- MAINTAINERS
|-- Makefile
|-- presets
|-- README.md
|-- RELEASE
|-- RELEASE_NOTES
|-- tests
|-- tools
`-- VERSION

查看ffmpeg帮助文档

[root@VM-32-17-centos ffmpeg-4.0.2]# ./configure --help
Usage: configure [options]
Options: [defaults in brackets after descriptions]

Help options:
  --help                   print this message
  --quiet                  Suppress showing informative output
  --list-decoders          show all available decoders	显示所有可用的解码器
  --list-encoders          show all available encoders	显示所有可用的编码器
  --list-hwaccels          show all available hardware accelerators 显示所有可用的硬件加速器

Standard options:
  --prefix=PREFIX          install in PREFIX [/usr/local] 	安装到对应的目录

Configuration options:
  --disable-static         do not build static libraries [no]	生成静态库
  --enable-shared          build shared libraries [no]			不生成动态库
  --enable-small           optimize for size instead of speed	优化库的大小

Program options:
  --disable-programs       do not build command line programs   不编译ffmpeg程序(命令行工具)
  --disable-ffmpeg         disable ffmpeg build		用于格式转换、解码、编码等
  --disable-ffplay         disable ffplay build		播放器
  --disable-ffprobe        disable ffprobe build	分析媒体文件信息

Component options:
  --disable-avdevice       disable libavdevice build	操作摄像头、视频输出等,默认开启的
  --disable-avcodec        disable libavcodec build		音视频编解码,默认开启的
  --disable-avformat       disable libavformat build	音视频封装格式的生成与解析,默认开启的
  --disable-swresample     disable libswresample build	音频重采样
  --disable-swscale        disable libswscale build		对视频放大/小等
  --disable-postproc       disable libpostproc build	音视频后期处理
  --disable-avfilter       disable libavfilter build	给视频加字幕、水印等
  
Individual component options:
  --disable-encoders       disable all encoders  禁用所有编码器
  --disable-muxers         disable all muxers    禁用所有多路复用器,不需要生成mp4这样的文件
  --disable-filters        disable all filters   禁用所有滤镜

Toolchain options:
  --arch=ARCH              select architecture []               选择架构
  --cross-prefix=PREFIX    use PREFIX for compilation tools []  使用PREFIX内容进行编译
  --enable-cross-compile   assume a cross-compiler is used      开启交叉编译(ffmpeg是跨平台的)
  --sysroot=PATH           root of cross-build tree             寻找库文件头文件
  --target-os=OS           compiler targets OS []               编译指定系统
  --extra-cflags=ECFLAGS   add ECFLAGS to CFLAGS []             传入gcc参数

Optimization options (experts only):
  --disable-x86asm         disable use of standalone x86 assembly 关闭asm

Linux中编译

1)使用room用户,防止权限问题引起麻烦
2)手动先创建目录

a) 创建指定的目录

[root@VM-32-17-centos ffmpeg-4.0.2]# pwd
/root/my-ffmpeg/ffmpeg-4.0.2

[root@VM-32-17-centos ffmpeg-4.0.2]# mkdir android
[root@VM-32-17-centos ffmpeg-4.0.2]# mkdir android/arm

b) ndk目录

已经下载并解压好ndk在这里

NDK目录为:/root/my-ndk/android-ndk-r17c
gcc目录为:/root/my-ndk/android-ndk-r17c/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64

c) 先关闭asm

[root@VM-32-17-centos ffmpeg-4.0.2]# ./configure --disable-x86asm

d) 再执行shell脚本

#!/bin/bash

# NDK目录
NDK_ROOT=/root/my-ndk/android-ndk-r17c

# gcc目录
TOOLCHAIN=$NDK_ROOT/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64

# 参考项目下 app\.cxx\cmake\debug\xxxxx\build.ninja文件
FLAGS="-isystem $NDK_ROOT/sysroot/usr/include/arm-linux-androideabi -D__ANDROID_API__=21 -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16 -mthumb -Wa,--noexecstack -Wformat -Werror=format-security  -O0 -fPIC"
INCLUDES=" -isystem $NDK_ROOT/sources/android/support/include"

# 定义编译后,所存放的目录
PREFIX=./android/arm

./configure \
--prefix=$PREFIX \
--enable-small \
--disable-programs \
--disable-avdevice \
--disable-encoders \
--disable-muxers \
--disable-filters \
--enable-cross-compile \
--cross-prefix=$TOOLCHAIN/bin/arm-linux-androideabi- \
--disable-shared \
--enable-static \
--sysroot=$NDK_ROOT/platforms/android-21/arch-arm \
--extra-cflags="$FLAGS $INCLUDES" \
--extra-cflags="-isysroot $NDK_ROOT/sysroot/" \
--arch=arm \
--target-os=android

make clean

make install

e) android/arm目录下有如下,则成功

[root@VM-32-17-centos ffmpeg-4.0.2]# cd android/arm/

[root@VM-32-17-centos arm]# ls
include  lib  share

f) 将arm目录下的文件导出到本地,以供AndroidStudio导入

[root@VM-32-17-centos arm]# zip -r myFFmpeg.zip ./*

[root@VM-32-17-centos arm]# ls -l  myFFmpeg.zip 
-rw-r--r-- 1 root root 36027760 Oct 16 13:52 myFFmpeg.zip

AndroidStudio导入

a) 导入头文件、库文件

在这里插入图片描述

b) CMakeLists.txt

cmake_minimum_required(VERSION 3.4.1)

# 导入源文件
file(GLOB allCPP *.c *.h *.cpp)
add_library(
        native-lib
        SHARED
        ${allCPP})

## 导入头文件
include_directories(${CMAKE_SOURCE_DIR}/include)

## 导入静态库
# CMAKE_SOURCE_DIR 等于 CMakeLists.txt所在的地址目录
# 设置内部的方式引入,指定库的目录是 -L  指定具体的库-l
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -L${CMAKE_SOURCE_DIR}/${CMAKE_ANDROID_ARCH_ABI}")

find_library(
        log-lib
        log)

target_link_libraries(
        native-lib
        ${log-lib}
        avformat avcodec avfilter avutil swresample swscale
)
# 忽略顺序的方式,导入
-Wl,--start-group
avcodec avfilter avformat avutil swresample swscale
-Wl,--end-group

c) native-lib.cpp

#include <jni.h>
#include <string>

#include <android/log.h>
#define TAG "AAAAAAAAAAAAA"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, TAG,  __VA_ARGS__);

extern "C" {
    #include <libavutil/avutil.h>
}

extern "C"
JNIEXPORT void JNICALL
Java_com_example_jni_1helloworld_MainActivity_test123(JNIEnv *env, jobject thiz) {
    LOGD("test start =======================");

    LOGD("test: %s\n", av_version_info());

    LOGD("test end =======================");
}

d) app/build.gradle

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值