ffmpeg iOS平台编译

一、编译过程

1.下载源码
2.预先编译作为插件的库fdk-aac,x264等。
3.安装yasm
4.安装编译工具
5.编写脚本
6.执行脚本


ffmpeg-ios流程.png

二、脚本注解(包含fdk,x264库的集成,如果不包含编译,需要移除configure相关编译参数)

#!/bin/sh

# directories
#赋值源码版本
SOURCE="ffmpeg-4.0.1"
#FAT文件夹名称
FAT="FFmpeg-iOS"
#文件夹名称
SCRATCH="scratch"
# must be an absolute path
#THIN路径
THIN=`pwd`/"thin"
#架构类型数组
ARCHS="arm64 armv7"

# absolute path to x264 library
X264=`pwd`/x264-ios

# absolute path to fdk-aac library
FDK_AAC=`pwd`/fdk-aac-ios

#编译选项
CONFIGURE_FLAGS="--enable-gpl --disable-shared --disable-stripping --disable-ffmpeg --disable-ffplay  --disable-ffprobe --disable-avdevice --disable-indevs --disable-filters --disable-devices --disable-parsers --disable-postproc --disable-debug --disable-asm --disable-yasm --disable-doc --disable-bsfs --disable-muxers --disable-demuxers --disable-ffplay --disable-ffprobe  --disable-indevs --disable-outdevs --enable-cross-compile --enable-filter=aresample --enable-bsf=aac_adtstoasc --enable-small --enable-dct --enable-dwt --enable-lsp --enable-mdct --enable-rdft --enable-fft --enable-static --enable-version3 --enable-nonfree --disable-encoders --enable-encoder=pcm_s16le --enable-encoder=aac --enable-encoder=libx264 --enable-encoder=mp2 --disable-decoders --enable-decoder=aac --enable-decoder=mp3 --enable-decoder=h264 --enable-decoder=pcm_s16le --disable-parsers --enable-parser=aac --enable-parser=mpeg4video --enable-parser=mpegvideo --enable-parser=mpegaudio --enable-parser=aac --disable-muxers --enable-muxer=flv --enable-muxer=mp4 --enable-muxer=wav --enable-muxer=adts --disable-demuxers --enable-demuxer=flv --enable-demuxer=mpegvideo --enable-demuxer=mpegtsraw --enable-demuxer=mpegts --enable-demuxer=mpegps --enable-demuxer=h264 --enable-demuxer=y4m --enable-demuxer=wav --enable-demuxer=aac --enable-demuxer=hls --enable-demuxer=mov --enable-demuxer=m4v --disable-protocols --enable-protocol=rtmp --enable-protocol=http --enable-protocol=file --enable-libx264 --enable-libfdk-aac --enable-libfdk_aac --enable-encoder=libfdk_aac"

COMPILE="y"
LIPO="y"

#支持的iOS最低版本
DEPLOYMENT_TARGET="8.0"

#赋值部分结束

if [ "$*" ]
then
    if [ "$*" = "lipo" ]
    then
        # skip compile
        COMPILE=
    else
        ARCHS="$*"
        if [ $# -eq 1 ]
        then
            # skip lipo
            LIPO=
        fi
    fi
fi

if [ "$COMPILE" ]
then
#是否安装yasm,x86汇编反汇编器 支持16位、32位、64位
    if [ ! `which yasm` ]
    then
        echo 'Yasm not found'
        #是否安装brew软件包管理工具
        if [ ! `which brew` ]
        then
            echo 'Homebrew not found. Trying to install...'
                        ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" \
                || exit 1
        fi
        echo 'Trying to install Yasm...'
        brew install yasm || exit 1
    fi
    #是否已经安装ffmpeg编译工具
    if [ ! `which gas-preprocessor.pl` ]
    then
        echo 'gas-preprocessor.pl not found. Trying to install...'
        (curl -L https://github.com/libav/gas-preprocessor/raw/master/gas-preprocessor.pl \
            -o /usr/local/bin/gas-preprocessor.pl \
            && chmod +x /usr/local/bin/gas-preprocessor.pl) \
            || exit 1
    fi

    if [ ! -r $SOURCE ]
    then
        echo 'FFmpeg source not found. Trying to download...'
        #尝试下载对于的ffmpeg源码并解压,如果失败就退出
        curl http://www.ffmpeg.org/releases/$SOURCE.tar.bz2 | tar xj \
            || exit 1
    fi

#赋值当前路径
    CWD=`pwd`
    #遍历需要编译的架构
    for ARCH in $ARCHS
    do
        echo "building $ARCH..."
        #创建/scratch/$ARCH目录
        mkdir -p "$SCRATCH/$ARCH"
        #进入目录
        cd "$SCRATCH/$ARCH"

        CFLAGS="-arch $ARCH"
        if [ "$ARCH" = "i386" -o "$ARCH" = "x86_64" ]
        then
        #如果是模拟器
            PLATFORM="iPhoneSimulator"
            #编译参数 最低系统版本
            CFLAGS="$CFLAGS -mios-simulator-version-min=$DEPLOYMENT_TARGET"
        else
            #如果是真机
            PLATFORM="iPhoneOS"
            CFLAGS="$CFLAGS -mios-version-min=$DEPLOYMENT_TARGET"
            if [ "$ARCH" = "arm64" ]
            then
                EXPORT="GASPP_FIX_XCODE5=1"
            fi
        fi

        #PLATFORM变量转换为小写并赋值给XCRUN_SDK
        XCRUN_SDK=`echo $PLATFORM | tr '[:upper:]' '[:lower:]'`
        CC="xcrun -sdk $XCRUN_SDK clang"
        CXXFLAGS="$CFLAGS"
        LDFLAGS="$CFLAGS"
        if [ "$X264" ]
        then
        #指定头文件路径
            CFLAGS="$CFLAGS -I$X264/include"
            #指定库路径
            LDFLAGS="$LDFLAGS -L$X264/lib"
        fi
        if [ "$FDK_AAC" ]
        then
            CFLAGS="$CFLAGS -I$FDK_AAC/include"
            LDFLAGS="$LDFLAGS -L$FDK_AAC/lib"
        fi

        #修改configure编译参数
        TMPDIR=${TMPDIR/%\/} $CWD/$SOURCE/configure \
            --target-os=darwin \
            --arch=$ARCH \
            --cc="$CC" \
            $CONFIGURE_FLAGS \
            --extra-cflags="$CFLAGS" \
            --extra-ldflags="$LDFLAGS" \
            --prefix="$THIN/$ARCH" \
        || exit 1
        #清除之前编译的可执行文件及配置文件。 
        echo 'build-ffmpeg:make clean'
        make clean
        echo 'build-ffmpeg:make'
        #make || exit 1
        echo 'build-ffmpeg:make install'
        #make install $EXPORT || exit 1
        make -j3 install $EXPORT || exit 1
        cd $CWD
    done
fi


if [ "$LIPO" ]
then
    echo "building fat binaries..."
    mkdir -p $FAT/lib
    set - $ARCHS
    CWD=`pwd`
    cd $THIN/$1/lib
    for LIB in *.a
    do
        cd $CWD
        echo lipo -create `find $THIN -name $LIB` -output $FAT/lib/$LIB 1>&2
        #合并.a库
        lipo -create `find $THIN -name $LIB` -output $FAT/lib/$LIB || exit 1
    done

    cd $CWD
    cp -rf $THIN/$1/include $FAT
fi

echo Done

三、相关命令

命令/参数 说明
which 查看可执行文件的位置
curl 下载相关资源
tar xj 解压相关资源

make|对源代码包进行编译|
make install |安装编译后的代码包|
configure|生成makefile文件,并可在后面指定相关编译选项|

configure 编译选项,可用 ./configure --help查看

参数 说明 [默认]
--target-os compiler targets OS []
--prefix=PREFIX install in PREFIX [/usr/local]
--cc=CC use C compiler CC [gcc]
--extra-cflags=ECFLAGS 指定头文件路径 add ECFLAGS to CFLAGS []
--extra-ldflags=ELDFLAGS 指定库文件路径 add ELDFLAGS to LDFLAGS []

参考:
configure/make/make install的作用
https://www.linuxidc.com/Linux/2011-02/32211.htm
CFLAGS LDFLAGS详解
https://blog.csdn.net/xinyuan510214/article/details/50457433
https://blog.csdn.net/shenhuxi_yu/article/details/79788760
https://www.jianshu.com/p/e590f041c5f6

你可能感兴趣的:(ffmpeg iOS平台编译)