iOS FFMPeg4.4.1 编译

160 阅读1分钟

音视频项目大多都使用FFMPeg,编译更新FFMPeg也是日常工作的一部分,这里记录一下:
脚本文件:build-ffmpeg.sh

FF_VERSION="4.4.1"

#FF_VERSION="snapshot-git"
if [[ $FFMPEG_VERSION != "" ]]; then
  FF_VERSION=$FFMPEG_VERSION
fi
SOURCE="ffmpeg-$FF_VERSION"
FAT="FFmpeg-iOS"

SCRATCH="scratch"
# must be an absolute path
THIN=`pwd`/"thin"
# absolute path to x264 library
#X264=`pwd`/fat-x264

#FDK_AAC=`pwd`/../fdk-aac-build-script-for-iOS/fdk-aac-ios

# --disable-libopus --disable-libilbc --disable-libvorbis

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

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

# avresample

#CONFIGURE_FLAGS="$CONFIGURE_FLAGS --enable-avresample"
#x86_64 i386
ARCHS="arm64 armv7"

COMPILE="y"
LIPO="y"

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
    if [ ! `which yasm` ]
    then
        echo 'Yasm not found'
        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
        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...'
curl http://www.ffmpeg.org/releases/$SOURCE.tar.bz2 | tar xj \
|| exit 1
        fi

        CWD=`pwd`
        for ARCH in $ARCHS
        do
            echo "building $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 -fembed-bitcode"
                if [ "$ARCH" = "arm64" ]
                then
                    EXPORT="GASPP_FIX_XCODE5=1"
                fi
            fi

            XCRUN_SDK=`echo $PLATFORM | tr '[:upper:]' '[:lower:]'`
            CC="xcrun -sdk $XCRUN_SDK clang"
            # force "configure" to use "gas-preprocessor.pl" (FFmpeg 3.3)
            if [ "$ARCH" = "arm64" ]
            then
                AS="gas-preprocessor.pl -arch aarch64 -- $CC"
            else
                AS="gas-preprocessor.pl -- $CC"

            fi

            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
            TMPDIR=${TMPDIR/%\/} $CWD/$SOURCE/configure \
                --target-os=darwin \
                --arch=$ARCH \
                --cc="$CC" \
                --as="$AS" \
                $CONFIGURE_FLAGS \
                --extra-cflags="$CFLAGS" \
                --extra-ldflags="$LDFLAGS" \
                --prefix="$THIN/$ARCH" \
            || 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
        lipo -create `find $THIN -name $LIB` -output $FAT/lib/$LIB || exit 1
    done

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

echo Done

先说一下:

  1. 如果需要使用到H264,可以将脚本中的H264解开,支持一起编译。
  2. 在上面编译中是禁止使用”--disable-audiotoolbox“iOS原生的AudioToolBox的,如果解开的话会编译不过,具体原因未找到,如有知道,还请指教。

在使用FFMPeg进行编码的时候,部分视频解码后播放可能会出现灰屏的情况,下面是处理灰屏的一种方式

1、找到libavcodec库中hevcdec.c文件

找到文件中 static int hls_slice_header(HEVCContext *s)函数

找到下面这个判断语句,并直接将return 注释掉即可

if (s->ref && sh->first_slice_in_pic_flag){
    av_log(s->avctx, AV_LOG_ERROR, "");
    //return 1;
}