Android 编译ffmpeg库

Android 编译ffmpeg库

  1. 下载ffmpeg源码
    git clone https://git.ffmpeg.org/ffmpeg.git 或者 git clone https://github.com/FFmpeg/FFmpeg.git。

  2. 下载NDK版本, 目前我使用的是NDK21, 下载NDK和查看NDK目录文件的方法:
    (1) 进入AndroidStudio查看, 勾选Show Package Details ,可以查看当前已经下载的NDK版本, 如果需要下载其他版本可以点击下载. 存放目录在自己配置的Android/Sdk目录下面.
    (2) 在Android开发者文档可以下载到ndk源码,ndk下载链接。 根据实际需求选择对应ndk版本,建议下载ndk-r20b稳定版。(这个是github上建议下载)

  3. 创建编译脚本build_ffmpeg.sh

#!/bin/bash
make clean
set -e
archbit=64

if [ $archbit -eq 64 ];then
echo "build for 64bit"
ARCH=aarch64
CPU=armv8-a
API=21
PLATFORM=aarch64
ANDROID=android
CFLAGS=""
LDFLAGS=""

else
echo "build for 32bit"
ARCH=arm
CPU=armv7-a
API=16
PLATFORM=armv7a
ANDROID=androideabi
CFLAGS="-mfloat-abi=softfp -march=$CPU"
LDFLAGS="-Wl,--fix-cortex-a8"
fi

export NDK=/home/xzx/Android/Sdk/ndk/21.0.6113669    #这里是自己的NDK路径
export TOOLCHAIN=$NDK/toolchains/llvm/prebuilt/linux-x86_64/bin  #注意这里是linux
export SYSROOT=$NDK/toolchains/llvm/prebuilt/linux-x86_64/sysroot
export CROSS_PREFIX=$TOOLCHAIN/$ARCH-linux-$ANDROID-
export CC=$TOOLCHAIN/$PLATFORM-linux-$ANDROID$API-clang
export CXX=$TOOLCHAIN/$PLATFORM-linux-$ANDROID$API-clang++
export PREFIX=../ffmpeg-android/$CPU

function build_android {
    ./configure \
    --prefix=$PREFIX \
    --cross-prefix=$CROSS_PREFIX \
    --target-os=android \  #注意这里是android
    --arch=$ARCH \
    --cpu=$CPU \
    --cc=$CC \
    --cxx=$CXX \
    --nm=$TOOLCHAIN/$ARCH-linux-$ANDROID-nm \
    --strip=$TOOLCHAIN/$ARCH-linux-$ANDROID-strip \
    --enable-cross-compile \
    --sysroot=$SYSROOT \
    --extra-cflags="$CFLAGS" \
    --extra-ldflags="$LDFLAGS" \
    --extra-ldexeflags=-pie \
    --enable-runtime-cpudetect \
    --disable-static \
    --enable-shared \
    --enable-logging \
    --disable-ffprobe \
    --enable-ffplay \
    --disable-ffmpeg \
    --enable-debug \
    --disable-doc \
    --enable-avfilter \
    --enable-decoders \
    #--enable-version3 \
    #--enable-rkmpp \
    $ADDITIONAL_CONFIGURE_FLAG

    make
    make install
}
build_android

修改文件的权限chmod 777 build_ffmpeg.sh
执行编译脚本./build_ffmpeg.sh

  1. 编译完成生成ffmpeg-android文件, 我编译的arm64版本, 其他版本自己修改脚本编译.
xzx@xzx-Lenovo:~/disk2/project/ffmpeg/ffmpeg-android$ ls
armv8-a

生成的对应模块的so库, 还存在对应的头文件.

xzx@xzx-Lenovo:~/disk2/project/ffmpeg/ffmpeg-android/armv8-a/lib$ ls
libavcodec.so  libavdevice.so  libavfilter.so  libavformat.so  libavutil.so  libswresample.so  libswscale.so  pkgconfig
  1. 选择编译不同版本的ffmpeg, 请使用git checkout release/4.2不同的分支进行却换源码.
xzx@xzx-Lenovo:~/disk2/project/ffmpeg/ffmpeg$ git branch -a
  master
  release/4.0
* release/4.2
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
  remotes/origin/oldabi
  remotes/origin/release/0.10
  remotes/origin/release/0.11
  remotes/origin/release/0.5
  remotes/origin/release/0.6
  remotes/origin/release/0.7
  remotes/origin/release/0.8
  remotes/origin/release/0.9
  remotes/origin/release/1.0
  remotes/origin/release/1.1
  remotes/origin/release/1.2
  remotes/origin/release/2.0
  remotes/origin/release/2.1
  remotes/origin/release/2.2
  remotes/origin/release/2.3
  remotes/origin/release/2.4
  remotes/origin/release/2.5
  remotes/origin/release/2.6
  remotes/origin/release/2.7
  remotes/origin/release/2.8
  remotes/origin/release/3.0
  remotes/origin/release/3.1
  remotes/origin/release/3.2
  remotes/origin/release/3.3
  remotes/origin/release/3.4
  remotes/origin/release/4.0
  remotes/origin/release/4.1
  remotes/origin/release/4.2
  remotes/origin/release/4.3
  remotes/origin/release/4.4

  1. 编译出来的库下载链接: https://download.csdn.net/download/nihenmeila/35382760

  2. 参考编译链接:

FFmpeg官方文档

查看FFmpeg编译流程

查看FFmpeg常用命令行

https://github.com/xufuji456/FFmpegAndroid/blob/master/doc/FFmpeg_compile_shell.md

你可能感兴趣的:(Android编译,android,ffmpeg)