OSX下NDK编译ffmpg和libx264

近期在研究Android视频开发相关的内容,先从ffmpg开发入手。这里描述下如何在mac osx 下使用NDK来编译libx264和ffmpg的过程。

1.版本

  1. ndk:23版本,23版本ndk变化是去掉了platforms文件夹

    下载地址: google官方 https://developer.android.com/ndk
    
  2. ffmpeg: 4.4

    从github获取
    git@github.com:FFmpeg/FFmpeg.git
    git checkout release/4.4
    
  3. libx264: 用于支持ffmpeg编码h264。

    git clone https://code.videolan.org/videolan/x264.git
    git checkout stable
    

2. NDK 编译libx264

首先需要修改configure脚本,由于原有交叉编译选项在ndk 23下有缺陷,需要手工修复

原来的代码:去configure下找 首先由于NDK 23版本是llvm clange的编译器,于是修改

    CC="${CC-${cross_prefix}gcc}"
    ## 修改为:
    CC="${CC-${cross_prefix}clang}"
    
    STRIP="${STRIP-${cross_prefix}strip}"
    ## 增加cross_prefix_tool变量,并修改为llvm:
    STRIP="${STRIP-${cross_prefix-tool}llvm-strip}"
    
    STRINGS="${STRINGS-${cross_prefix}strings}"
    ## 增加cross_prefix_tool变量,并修改为llvm:
    STRINGS="${STRINGS-${cross_prefix_tool}llvm-strings}"
    INSTALL="${INSTALL-install}"
    
    
    PKGCONFIG="${PKGCONFIG-${cross_prefix}pkg-config}
    ## 增加cross_prefix_tool变量,并修改为llvm:
    PKGCONFIG="${PKGCONFIG-${cross_prefix-tool}llvm-pkg-config}
    
    
    # ar and ranlib doesn't load the LTO plugin by default, prefer the gcc-prefixed wrappers which does.
if ${cross_prefix}gcc-ar --version >/dev/null 2>&1; then
    AR="${AR-${cross_prefix}gcc-ar}"
else
    AR="${AR-${cross_prefix}ar}"
    # 支持llvm
    AR="${AR-${cross_prefix-tool}llvm-ar}"
fi
if ${cross_prefix}gcc-ranlib --version >/dev/null 2>&1; then
    RANLIB="${RANLIB-${cross_prefix}gcc-ranlib}"
else
    RANLIB="${RANLIB-${cross_prefix}lranlib}"
    # 支持llvm
    AR="${AR-${cross_prefix-tool}llvm-ranlib}"
fi

引入新的选项    
cross_prefix_tool=""

--cross-prefix-tool=*)
        cross_prefix_tool="$optarg"
        ;;
        

编写如下build脚本:

    #!/bin/bash

export NDK_HOME=/Volumes/data/Android/NDK

function build() {
   ./configure --prefix=$PREFIX \
    --cross-prefix=$CROSS_COMPILE \
    --sysroot=$SYSROOT \
    --enable-static \
    --enable-shared \
    --enable-pic \
    --disable-asm \
    --cross-prefix-tool=/Volumes/data/Android/NDK/toolchains/llvm/prebuilt/darwin-x86_64/bin/ \
    --host=$host
    make clean
    make -j8
    make install
}


#x86
ARCH=x86_64
CPU=x86_64
PREFIX=$(pwd)/android/$ARCH
TOOLCHAIN=$NDK_HOME/toolchains/llvm/prebuilt/darwin-x86_64/bin
CROSS_COMPILE=/Volumes/data/Android/NDK/toolchains/llvm/prebuilt/darwin-x86_64/bin/x86_64-linux-android31-
host="x86_64-linux"
build

#AARCH
ARCH=armv7a
CPU=armv7a
PREFIX=$(pwd)/android/$ARCH
TOOLCHAIN=$NDK_HOME/toolchains/llvm/prebuilt/darwin-x86_64/bin
CROSS_COMPILE=/Volumes/data/Android/NDK/toolchains/llvm/prebuilt/darwin-x86_64/bin/armv7a-linux-androideabi31-
host="arm-linux"
build


#AARCH
ARCH=arm64
CPU=arm64
PREFIX=$(pwd)/android/$ARCH
TOOLCHAIN=$NDK_HOME/toolchains/llvm/prebuilt/darwin-x86_64/bin
CROSS_COMPILE=/Volumes/data/Android/NDK/toolchains/llvm/prebuilt/darwin-x86_64/bin/aarch64-linux-android31-
host="arm64-linux"
build

运行该脚本完成libx264的编译

3. 编译ffmpeg

编写如下脚本:

#!/bin/bash
export NDK_HOME=/Volumes/data/Android/NDK
export PLATFORM_VERSION=android-10
export X264_LOCATION=/Users/lokie/Downloads/x264/android
function build
{
    echo "start build ffmpeg for $ARCH"
        make clean
    ./configure --target-os=android \
    --prefix=$PREFIX --arch=$ARCH \
    --disable-doc \
    --enable-shared \
    --disable-static \
    --disable-yasm \
    --disable-asm \
    --disable-symver \
    --enable-gpl \
    --disable-ffmpeg \
    --disable-ffplay \
    --disable-ffprobe \
    --cross-prefix=$CROSS_COMPILE \
    --enable-cross-compile \
    --sysroot=$SYSROOT \
    --enable-small \
        --nm=$NM \
        --strip=$STRIP \
        --enable-libx264 \
        --extra-cflags="-I$InclueX264" \
        --extra-ldflags="-L$LinkX264" \
    make -j8
    make install
    echo "build ffmpeg for $ARCH finished"
}


#x86
ARCH=x86_64
CPU=x86_64
PREFIX=$(pwd)/android/$ARCH
TOOLCHAIN=$NDK_HOME/toolchains/llvm/prebuilt/darwin-x86_64/bin
CROSS_COMPILE=$TOOLCHAIN/x86_64-linux-android31-
STRIP=$TOOLCHAIN/llvm-strip
NM=$TOOLCHAIN/llvm-nm
ADDI_CFLAGS="-marm"
InclueX264=$X264_LOCATION/$ARCH/include
LinkX264=$X264_LOCATION/$ARCH/lib
SYSROOT=$NDK_HOME/toolchains/llvm/prebuilt/darwin-x86_64/sysroot
build

#arm64
ARCH=arm64
CPU=arm64
PREFIX=$(pwd)/android/$ARCH
TOOLCHAIN=$NDK_HOME/toolchains/llvm/prebuilt/darwin-x86_64/bin
CROSS_COMPILE=/Volumes/data/Android/NDK/toolchains/llvm/prebuilt/darwin-x86_64/bin/aarch64-linux-android31-
ADDI_CFLAGS="-marm"
SYSROOT=$NDK_HOME/toolchains/llvm/prebuilt/darwin-x86_64/sysroot
STRIP=$TOOLCHAIN/llvm-strip
NM=$TOOLCHAIN/llvm-nm
InclueX264=$X264_LOCATION/$ARCH/include
LinkX264=$X264_LOCATION/$ARCH/lib
build

#arm
ARCH=armv7a
CPU=armv7a
PREFIX=$(pwd)/android/$ARCH
TOOLCHAIN=$NDK_HOME/toolchains/llvm/prebuilt/darwin-x86_64/bin
CROSS_COMPILE=/Volumes/data/Android/NDK/toolchains/llvm/prebuilt/darwin-x86_64/bin/armv7a-linux-androideabi31-
ADDI_CFLAGS="-marm"
SYSROOT=$NDK_HOME/toolchains/llvm/prebuilt/darwin-x86_64/sysroot
STRIP=$TOOLCHAIN/llvm-strip
NM=$TOOLCHAIN/llvm-nm
InclueX264=$X264_LOCATION/$ARCH/include
LinkX264=$X264_LOCATION/$ARCH/lib
build

执行就可以完成编译

Lokie博客
请先登录后发表评论
  • 最新评论
  • 总共0条评论
  • 本博客使用免费开源的 laravel-bjyblog v5.5.1.1 搭建 © 2014-2018 lokie.wang 版权所有 ICP证:沪ICP备18016993号
  • 联系邮箱:kitche1985@hotmail.com