编译FFmpeg 4.2.3

1. 环境

  • android-ndk-r21
  • ffmpeg-4.2.3
  • openssl-1.1.1g
  • ubuntu18.04

2. 目录结构

# root根目录
- ndk
    - android-ndk-r21
- ffmpeg
    - ffmpeg-4.2.3
        - configure
        - android # 生成文件位置
            - armv7-a
            - armv8-a
    - openssl-1.1.1g
        - android # 生成文件位置
            - arm
            - arm64
            ...
    - build_android_ffmpeg.sh
    - build_android_openssl.sh

3. 编译脚本

  • 修改ffmpeg-4.2.3/configure文件大约 line 3717处(修改编译目标文件名称)
# SLIBNAME_WITH_MAJOR='$(SLIBNAME).$(LIBMAJOR)'
# LIB_INSTALL_EXTRA_CMD='$$(RANLIB) "$(LIBDIR)/$(LIBNAME)"'
# SLIB_INSTALL_NAME='$(SLIBNAME_WITH_VERSION)'
# SLIB_INSTALL_LINKS='$(SLIBNAME_WITH_MAJOR) $(SLIBNAME)'

SLIBNAME_WITH_MAJOR='$(SLIBPREF)$(FULLNAME)-$(LIBMAJOR)$(SLIBSUF)'
LIB_INSTALL_EXTRA_CMD='$$(RANLIB) "$(LIBDIR)/$(LIBNAME)"'
SLIB_INSTALL_NAME='$(SLIBNAME_WITH_MAJOR)'
SLIB_INSTALL_LINKS='$(SLIBNAME)'

修改后继续编辑configure文件大约 line 6353处(修改检查openssl逻辑,enable_openssl)

check_pkg_config openssl openssl openssl/ssl.h OPENSSL_init_ssl ||
# 添加此处
check_lib openssl openssl/ssl.h OPENSSL_init_ssl -lssl -lcrypto ||
# 添加完成
check_pkg_config openssl openssl openssl/ssl.h SSL_library_init ||

  • 编译openssl脚本
#!/bin/bash

#
# Author:Jerry
#

cd openssl-1.1.1g

export ANDROID_NDK_ROOT=/root/ndk/android-ndk-r21
export PATH=$ANDROID_NDK_ROOT/toolchains/llvm/prebuilt/linux-x86_64/bin:$PATH
export ANDROID_NDK_HOME=$ANDROID_NDK_ROOT

./config

function build
{

echo "begin build openssl for $PLATFORM_ABI "

make clean


./Configure $PLATFORM_ABI -D__ANDROID_API__=$PLATFORM_API no-threads no-shared no-ssl3 no-comp no-hw no-engine --prefix=$PREFIX

make
make install

rm -r $PREFIX/bin
rm -r $PREFIX/share
rm -r $PREFIX/ssl
rm -r $PREFIX/lib/engines-1.1

echo "build openssl for $PLATFORM_ABI completed "

}

#arm-v7a
PLATFORM_ABI=android-arm
PLATFORM_API=19
PREFIX=`pwd`/android/arm
PATH=$ANDROID_NDK_ROOT/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin:$PATH
build

#arm64
PLATFORM_ABI=android-arm64
PLATFORM_API=21
PREFIX=`pwd`/android/arm64
PATH=$ANDROID_NDK_ROOT/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64/bin:$PATH
build


#x86
PLATFORM_ABI=android-x86
PLATFORM_API=19
PREFIX=`pwd`/android/x86
PATH=$ANDROID_NDK_ROOT/toolchains/x86-4.9/prebuilt/linux-x86_64/bin:$PATH
build


#x86_64
PLATFORM_ABI=android-x86_64
PLATFORM_API=21
PREFIX=`pwd`/android/x86_64
PATH=$ANDROID_NDK_ROOT/toolchains/x86_64-4.9/prebuilt/linux-x86_64/bin:$PATH
build

  • 编译ffmpeg脚本
#! /bin/bash

NDK=/root/ndk/android-ndk-r21
SSL=/root/ffmpeg/openssl-1.1.1g/android
TOOLCHAIN=$NDK/toolchains/llvm/prebuilt/linux-x86_64
cd ffmpeg-4.2.3

function build_android
{
echo "Compiling FFmpeg for $CPU"
./configure \
    --prefix=$PREFIX \
    --disable-x86asm \
    --disable-asm \
    --disable-hwaccels \
    --disable-gpl \
    --disable-postproc \
    --enable-shared \
    --enable-jni \
    --disable-mediacodec \
    --disable-decoder=h264_mediacodec \
    --disable-static \
    --disable-doc \
    --disable-ffmpeg \
    --disable-ffplay \
    --disable-ffprobe \
    --disable-avdevice \
    --disable-doc \
    --disable-symver \
    --cross-prefix=$CROSS_PREFIX \
    --target-os=android \
    --arch=$ARCH \
    --cpu=$CPU \
    --cc=$CC \
    --cxx=$CXX \
    --enable-cross-compile \
    --sysroot=$SYSROOT \
    --enable-openssl \
    --enable-protocols \
    --extra-cflags="-Os -fpic $OPTIMIZE_CFLAGS $SSL_CFLAGS" \
    --extra-ldflags="$ADDI_LDFLAGS $SSL_LDFLAGS" \
    $ADDITIONAL_CONFIGURE_FLAG
make clean
make
make install
echo "The Compilation of FFmpeg for $CPU is completed"
}

#armv7-a
function build_arm
{
ARCH=arm
CPU=armv7-a
API=19
SSL_CFLAGS=-I$SSL/$ARCH/include
SSL_LDFLAGS=-L$SSL/$ARCH/lib
CC=$TOOLCHAIN/bin/armv7a-linux-androideabi$API-clang
CXX=$TOOLCHAIN/bin/armv7a-linux-androideabi$API-clang++
SYSROOT=$NDK/toolchains/llvm/prebuilt/linux-x86_64/sysroot
CROSS_PREFIX=$TOOLCHAIN/bin/arm-linux-androideabi-
PREFIX=$(pwd)/android/$CPU
OPTIMIZE_CFLAGS="-mfloat-abi=softfp -mfpu=vfp -marm -march=$CPU "
build_android
}

#armv8-a
function build_arm64
{
ARCH=arm64
CPU=armv8-a
API=21
SSL_CFLAGS=-I$SSL/$ARCH/include
SSL_LDFLAGS=-L$SSL/$ARCH/lib
CC=$TOOLCHAIN/bin/aarch64-linux-android$API-clang
CXX=$TOOLCHAIN/bin/aarch64-linux-android$API-clang++
SYSROOT=$NDK/toolchains/llvm/prebuilt/linux-x86_64/sysroot
CROSS_PREFIX=$TOOLCHAIN/bin/aarch64-linux-android-
PREFIX=$(pwd)/android/$CPU
OPTIMIZE_CFLAGS="-march=$CPU"
build_android
}

#x86
function build_x86
{
ARCH=x86
CPU=x86
API=19
SSL_CFLAGS=-I$SSL/$ARCH/include
SSL_LDFLAGS=-L$SSL/$ARCH/lib
CC=$TOOLCHAIN/bin/i686-linux-android$API-clang
CXX=$TOOLCHAIN/bin/i686-linux-android$API-clang++
SYSROOT=$NDK/toolchains/llvm/prebuilt/linux-x86_64/sysroot
CROSS_PREFIX=$TOOLCHAIN/bin/i686-linux-android-
PREFIX=$(pwd)/android/$CPU
OPTIMIZE_CFLAGS="-march=i686 -mtune=intel -mssse3 -mfpmath=sse -m32"
build_android
}

#x86_64
function build_x86_64
{
ARCH=x86_64
CPU=x86-64
API=21
SSL_CFLAGS=-I$SSL/$ARCH/include
SSL_LDFLAGS=-L$SSL/$ARCH/lib
CC=$TOOLCHAIN/bin/x86_64-linux-android$API-clang
CXX=$TOOLCHAIN/bin/x86_64-linux-android$API-clang++
SYSROOT=$NDK/toolchains/llvm/prebuilt/linux-x86_64/sysroot
CROSS_PREFIX=$TOOLCHAIN/bin/x86_64-linux-android-
PREFIX=$(pwd)/android/$CPU
OPTIMIZE_CFLAGS="-march=$CPU -msse4.2 -mpopcnt -m64 -mtune=intel"
build_android
}

function dash
{
echo '------------------------------------------------------------'
echo '------------------------------------------------------------'
echo '------------------------------------------------------------'
}

build_arm

dash
build_arm64

dash
build_x86

dash
build_x86_64

4. 执行编译

# 进入root根目录
cd ffmpeg
./build_android_openssl.sh
./build_android_ffmpeg.sh

# 结束,生成文件相对位置 
cd ffmpeg-4.2.3/android/

你可能感兴趣的:(编译FFmpeg 4.2.3)