1.编译ffmpeg库

2019-01-31  本文已影响73人  你weixiao的时候很美
1.下载ffmpeg
#!/bin/bash
source="ffmpeg-4.1"

if [ ! -r $source ]
then
curl http://ffmpeg.org/releases/${source}.tar.bz2 | tar xj || exit 1
fi
2.查看ffmpeg编译配置项

ffmpeg有很多配置项:

help options:帮助选项、
standard options:标准选项、
Licensing options:许可选项、
configuration options:配置选项、
external library support:外部库支持、
toolchain options:工具链选项、
advanced options:高级选项、
developer options:开发者选项、
3.编译ffmpeg

网上有很好的编译脚本:FFmpeg-iOS-build-script

下边分析:
1.定义一些字符串

VERSION="4.1" //版本号

SOURCE="ffmpeg-$VERSION"  // 需要编译的ffmpeg源文件

CACHEDIR="CACHEDIR" // 保存编译生成的.o文件。

STATICDIR=`pwd`/"weixiao-ffmpeg"  //  编译生成的静态库.a的目录

2.添加ffmpeg的配置项

CONFIGURE_FLAGS="--enable-cross-compile --disable-debug \ 
--disable-programs --disable-doc --enable-pic"

if [ "$X264" ]
then
    CONFIGURE_FLAGS="$CONFIGURE_FLAGS --enable-gpl --enable-libx264"
fi

if [ "$FDK_AAC" ]
then
    CONFIGURE_FLAGS="$CONFIGURE_FLAGS --enable-libfdk-aac --enable-nonfree"
fi

FFmpeg一共九个库,常用的(默认)编译的是七个库。 而我们一般用到的是3到4个库,其中最重要的库是用于编解码的库avcodec。

可以根据我们的需要进行编译:

查看这些配置项使用上边第二条讲过的方式,./confifure --help

通过设置CONFIGURE_FLAGS 来配置这些库是否需要,类似下边:
CONFIGURE_FLAGS ="$CONFIGURE_FLAGS --enable-avdevice --enable-avcodec --enable-avformat --disable-postproc" 

3.添加需要编译的cpu平台

ARCHS="arm64 armv7 x86_64 i386"

// 也可以动态的通过入参指定需要的cpu平台
if [ "$*" ]
then
    #存入输入参数,也就说:外部指定需要编译CPU架构类型
    ARCHS ="$*"
fi

4.安装汇编器yasm

if [ ! `which yasm` ]
then
    echo "Yasm not found"
    if [ ! `which brew` ]
    then
        echo "Homebrew not found"
        ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" || exit 1
    fi

    brew install yasm || exit 1
fi

5.gas-preprocessor.pl 脚本文件

if [ ! `which gas-preprocessor.pl` ]
then
    echo "gas-preprocessor.pl not found"
    (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

6.根据需要的cpu平台循环编译

CWD=`pwd`
for ARCH in $ARCHS
do
       编译代码
done

7.for循环中的代码一,首先设置platform平台(7和下边都是for遵循中执行的代码)

    echo "build $ARCH"
    mkdir -p "$CACHEDIR/$ARCH"
    cd "$CACHEDIR/$ARCH"

    CFLAGS="-arch $ARCH"
    if [ "$ARCH" = "i386" -o "$ARCH" = "x86_64" ]
    then
        PLATFORM="iPhoneSimulator"
        CFLAGS="$CFLAGS -mios-simulator-version-min=$TARGET"
    else
        PLATFORM="iPhoneOS"
        CFLAGS="$CFLAGS -mios-version-min=$TARGET -fembed-bitcode"

        if [ "$ARCH" = "arm64" ]
        then
            EXPORT="GASPP_FIX_XCODE5=1"
        fi
    fi
  1. 是否支持x264 和 acc
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
  1. 设置汇编器
XCRUN_SDK=`echo $PLATFORM | tr '[:upper:]' '[:lower:]'`
CC="xcrun -sdk $XCRUN_SDK clang"

if [ "$ARCH" = "arm64" ]
then
AS="gas-preprocessor.pl -arch aarch64 -- $CC"
else
AS="gas-preprocessor.pl -- $CC"
fi

10.设置参数,并编译

    TMPDIR=${TMPDIR/%\/} $CWD/$SOURCE/configure \
    --target-os=darwin \
    --arch=$ARCH \
    --cc="$CC" \
    --as="$AS" \
    $CONFIGURE_FLAGS \
    --extra-cflags="$CFLAGS" \
    --extra-ldflags="$LDFLAGS" \
    --prefix="$STATICDIR/$ARCH" \
    || exit 1

    make -j3 install $EXPORT || exit 1
    cd $CWD

4.编译ffmpeg遇到的问题汇总:

`pwd` 表示获取当前位置的命令, 'pwd'表示pwd字符串
name="jack"  =号前不能有空格

if [ age == 18 ]   []前后必须有空格
ffmpeg 需要的汇编器是 yasm 而不是ysam。 因为变量名写的时候不报错,所以很难检查出来,要细心些。
xcrun -sdk iphoneos clang is unable to create an executable file.
C compiler test failed.

解决方案是命令行执行如下语句:

sudo xcode-select --switch /Applications/Xcode.app

该问题的链接为:issues119 大意是说因为找不到Xcode的目录。

5.完整脚本如下:
VERSION="4.1"

SOURCE="ffmpeg-$VERSION"

CACHEDIR="CACHEDIR"

STATICDIR=`pwd`/"weixiao-ffmpeg"

CONFIGURE_FLAGS="--enable-cross-compile --disable-debug --disable-programs --disable-doc --enable-pic"

if [ "$X264" ]
then
    CONFIGURE_FLAGS="$CONFIGURE_FLAGS --enable-gpl --enable-libx264"
fi

if [ "$FDK_AAC" ]
then
    CONFIGURE_FLAGS="$CONFIGURE_FLAGS --enable-libfdk-aac --enable-nonfree"
fi

ARCHS="arm64 armv7 x86_64 i386"

TARGET="10.0"

if [ ! `which yasm` ]
then
    echo "Yasm not found"
    if [ ! `which brew` ]
    then
        echo "Homebrew not found"
        ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" || exit 1
    fi

    brew install yasm || exit 1
fi

if [ ! `which gas-preprocessor.pl` ]
then
    echo "gas-preprocessor.pl not found"
    (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

CWD=`pwd`
for ARCH in $ARCHS
do
    echo "build $ARCH"
    mkdir -p "$CACHEDIR/$ARCH"
    cd "$CACHEDIR/$ARCH"

    CFLAGS="-arch $ARCH"
    if [ "$ARCH" = "i386" -o "$ARCH" = "x86_64" ]
    then
        PLATFORM="iPhoneSimulator"
        CFLAGS="$CFLAGS -mios-simulator-version-min=$TARGET"
    else
        PLATFORM="iPhoneOS"
        CFLAGS="$CFLAGS -mios-version-min=$TARGET -fembed-bitcode"

        if [ "$ARCH" = "arm64" ]
        then
            EXPORT="GASPP_FIX_XCODE5=1"
        fi
    fi

    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

    XCRUN_SDK=`echo $PLATFORM | tr '[:upper:]' '[:lower:]'`
    CC="xcrun -sdk $XCRUN_SDK clang"

    if [ "$ARCH" = "arm64" ]
    then
    AS="gas-preprocessor.pl -arch aarch64 -- $CC"
    else
    AS="gas-preprocessor.pl -- $CC"
    fi

    TMPDIR=${TMPDIR/%\/} $CWD/$SOURCE/configure \
    --target-os=darwin \
    --arch=$ARCH \
    --cc="$CC" \
    --as="$AS" \
    $CONFIGURE_FLAGS \
    --extra-cflags="$CFLAGS" \
    --extra-ldflags="$LDFLAGS" \
    --prefix="$STATICDIR/$ARCH" \
    || exit 1

    make -j3 install $EXPORT || exit 1
    cd $CWD
done
上一篇下一篇

猜你喜欢

热点阅读