> 文章列表 > FFmpeg 编译静态库

FFmpeg 编译静态库

FFmpeg 编译静态库

1. 使用工具

  1.1 FFmpeg 官网:

  1.2 FFmpeg macOS 官方安装教程:

  1.3 Homebreaw 安装网站:

2. Homebreaw 介绍

  2.1 简称 brew,在 Mac 平台终端上管理软件包,安装,更新,卸载等软件

  2.2 安装 brew,终端执行指令(内部安装): 

  // ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)”// /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"

  2.3 出现安装或者更新问题:

    错误:Error: Cannot download non-corrupt https://formulae.brew.sh/api/formula.json!

    解决网址: brew upgrade error:

  2.4 常用命令

    搜索软件 brew search  软件名;安装软件 brew install 软件名;卸载软件 brew remove  软件名;

  2.5 安装 FFmpeg(可选项,使用到 ffmpeg 命令行操作,必须安装)

    安装完 Homebrew,然后在终端执行指令:

// brew install ffmpeg --with-libvpx --with-libvorbis --with-ffplay
brew install ffmpeg

    执行结束,在终端中输入 ffmpeg,验证是否安装成功 

3. 编译静态库

  3.1 下载 gas-preprocessor 库(自动编译脚本的时候用到),方法一: 可以手动下载操作,方法二: 命令下载步骤,依次执行下面命令:

// https://github.com/bigsen/gas-preprocessor.git
sudo git clone https://github.com/libav/gas-preprocessor.git /usr/local/bin/gas
sudo cp /usr/local/bin/gas/gas-preprocessor.pl /usr/local/bin/gas-preprocessor.pl
sudo chmod 777 /usr/local/bin/gas-preprocessor.pl
sudo rm -rf /usr/local/bin/gas/

  3.2 安装 yams (系统现已找不到 yams 库进行安装,自带有 yaws,可进行安装) 

    yasm 是汇编编译器,ffmpeg 中提高效率用到了汇编指令,所以编译时需要安装,安装执行下面命令:

 // 报错则执行 sudo brew install yamsbrew install yams

4. 配置编译 FFmpeg 脚本

  手动编译 FFmpeg 脚本,有些复杂,这里选择的是 FFmpeg-iOS-build-script,github 一个开源的脚本,个人认为简单而实用,执行脚本会自动从 github 下载 ffmpeg 源码,并开始编译成 iOS 支持各种架构库。

  4.1 脚本下载地址,也可手动点击下载

  git clone https://github.com/kewlbear/FFmpeg-iOS-build-script.git

  4.2 下载完成后,可自行修改脚本文件,自带脚本:

#!/bin/sh# directories
FF_VERSION="4.3.1"
#FF_VERSION="snapshot-git"
if [[ $FFMPEG_VERSION != "" ]]; thenFF_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-iosCONFIGURE_FLAGS="--enable-cross-compile --disable-debug --disable-programs \\--disable-doc --enable-pic"if [ "$X264" ]
thenCONFIGURE_FLAGS="$CONFIGURE_FLAGS --enable-gpl --enable-libx264"
fiif [ "$FDK_AAC" ]
thenCONFIGURE_FLAGS="$CONFIGURE_FLAGS --enable-libfdk-aac --enable-nonfree"
fi# avresample
#CONFIGURE_FLAGS="$CONFIGURE_FLAGS --enable-avresample"ARCHS="arm64 armv7 x86_64 i386"COMPILE="y"
LIPO="y"DEPLOYMENT_TARGET="8.0"if [ "$*" ]
thenif [ "$*" = "lipo" ]then# skip compileCOMPILE=elseARCHS="$*"if [ $# -eq 1 ]then# skip lipoLIPO=fifi
fiif [ "$COMPILE" ]
thenif [ ! `which yasm` ]thenecho 'Yasm not found'if [ ! `which brew` ]thenecho 'Homebrew not found. Trying to install...'ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" \\|| exit 1fiecho 'Trying to install Yasm...'brew install yasm || exit 1fiif [ ! `which gas-preprocessor.pl` ]thenecho '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 1fiif [ ! -r $SOURCE ]thenecho 'FFmpeg source not found. Trying to download...'curl http://www.ffmpeg.org/releases/$SOURCE.tar.bz2 | tar xj \\|| exit 1fiCWD=`pwd`for ARCH in $ARCHSdoecho "building $ARCH..."mkdir -p "$SCRATCH/$ARCH"cd "$SCRATCH/$ARCH"CFLAGS="-arch $ARCH"if [ "$ARCH" = "i386" -o "$ARCH" = "x86_64" ]thenPLATFORM="iPhoneSimulator"CFLAGS="$CFLAGS -mios-simulator-version-min=$DEPLOYMENT_TARGET"elsePLATFORM="iPhoneOS"CFLAGS="$CFLAGS -mios-version-min=$DEPLOYMENT_TARGET -fembed-bitcode"if [ "$ARCH" = "arm64" ]thenEXPORT="GASPP_FIX_XCODE5=1"fifiXCRUN_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" ]thenAS="gas-preprocessor.pl -arch aarch64 -- $CC"elseAS="gas-preprocessor.pl -- $CC"fiCXXFLAGS="$CFLAGS"LDFLAGS="$CFLAGS"if [ "$X264" ]thenCFLAGS="$CFLAGS -I$X264/include"LDFLAGS="$LDFLAGS -L$X264/lib"fiif [ "$FDK_AAC" ]thenCFLAGS="$CFLAGS -I$FDK_AAC/include"LDFLAGS="$LDFLAGS -L$FDK_AAC/lib"fiTMPDIR=${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 1make -j3 install $EXPORT || exit 1cd $CWDdone
fiif [ "$LIPO" ]
thenecho "building fat binaries..."mkdir -p $FAT/libset - $ARCHSCWD=`pwd`cd $THIN/$1/libfor LIB in *.adocd $CWDecho lipo -create `find $THIN -name $LIB` -output $FAT/lib/$LIB 1>&2lipo -create `find $THIN -name $LIB` -output $FAT/lib/$LIB || exit 1donecd $CWDcp -rf $THIN/$1/include $FAT
fiecho Done

  4.3 脚本可根据需求修改的地方,如下图:

  4.4 编译裁剪库(可选项) 

    FFmpeg 库很庞大,不进行裁剪,编译出来的库也会很大,根据自身的需求,裁剪部分库中的内容,更改内容是在脚本文件中 CONFIGURE_FLAGS 对应的选项,进行更改之后的脚本: build-ffmpeg-simple.sh

#!/bin/sh# directories
FF_VERSION="4.3.1"
#FF_VERSION="snapshot-git"
if [[ $FFMPEG_VERSION != "" ]]; thenFF_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-iosCONFIGURE_FLAGS="--enable-cross-compile \\--disable-debug \\--disable-programs \\--disable-doc \\--enable-pic \\--enable-small \\--enable-gpl \\--enable-network \\--enable-version3 \\--disable-ffplay \\--disable-ffprobe \\--disable-avdevice \\--disable-outdevs \\--disable-ffmpeg \\--disable-postproc \\--disable-symver \\--disable-stripping \\--disable-iconv \\--enable-asm \\--enable-neon \\--enable-zlib \\--disable-encoders \\--enable-encoder=h264_videotoolbox \\--enable-encoder=hevc_videotoolbox \\--enable-encoder=aac \\--disable-muxers \\--enable-muxer=h264 \\--enable-muxer=hevc \\--enable-muxer=rtsp \\--enable-muxer=dts \\--enable-muxer=adts \\--enable-muxer=mp3 \\--disable-decoders \\--enable-decoder=h264 \\--enable-decoder=hevc \\--enable-decoder=aac \\--enable-decoder=mp3 \\--disable-demuxers \\--enable-demuxer=h264 \\--enable-demuxer=hevc \\--enable-demuxer=rtsp \\--enable-demuxer=sdp \\--enable-demuxer=aac \\--enable-demuxer=mp3 \\--disable-parsers \\--enable-parser=aac \\--enable-parser=h264 \\--enable-parser=hevc \\--disable-protocols \\--enable-protocol=tcp \\--enable-protocol=file \\--enable-protocol=hls \\--enable-protocol=concat \\--enable-protocol=unix \\--disable-filters \\--enable-filter=aresample \\--enable-filter=asetpts \\--enable-filter=setpts \\--enable-filter=scale \\--enable-filter=concat \\--enable-filter=atempo \\--enable-filter=movie \\--enable-filter=overlay \\--enable-filter=rotate \\--enable-filter=format \\--enable-filter=transpose \\--enable-filter=hflip"if [ "$X264" ]
thenCONFIGURE_FLAGS="$CONFIGURE_FLAGS --enable-gpl --enable-libx264"
fiif [ "$FDK_AAC" ]
thenCONFIGURE_FLAGS="$CONFIGURE_FLAGS --enable-libfdk-aac --enable-nonfree"
fi# avresample
#CONFIGURE_FLAGS="$CONFIGURE_FLAGS --enable-avresample"ARCHS="arm64 armv7 x86_64 i386"COMPILE="y"
LIPO="y"DEPLOYMENT_TARGET="8.0"if [ "$*" ]
thenif [ "$*" = "lipo" ]then# skip compileCOMPILE=elseARCHS="$*"if [ $# -eq 1 ]then# skip lipoLIPO=fifi
fiif [ "$COMPILE" ]
thenif [ ! `which yasm` ]thenecho 'Yasm not found'if [ ! `which brew` ]thenecho 'Homebrew not found. Trying to install...'ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" \\|| exit 1fiecho 'Trying to install Yasm...'brew install yasm || exit 1fiif [ ! `which gas-preprocessor.pl` ]thenecho '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 1fiif [ ! -r $SOURCE ]thenecho 'FFmpeg source not found. Trying to download...'curl http://www.ffmpeg.org/releases/$SOURCE.tar.bz2 | tar xj \\|| exit 1fiCWD=`pwd`for ARCH in $ARCHSdoecho "building $ARCH..."mkdir -p "$SCRATCH/$ARCH"cd "$SCRATCH/$ARCH"CFLAGS="-arch $ARCH"if [ "$ARCH" = "i386" -o "$ARCH" = "x86_64" ]thenPLATFORM="iPhoneSimulator"CFLAGS="$CFLAGS -mios-simulator-version-min=$DEPLOYMENT_TARGET"elsePLATFORM="iPhoneOS"CFLAGS="$CFLAGS -mios-version-min=$DEPLOYMENT_TARGET -fembed-bitcode"if [ "$ARCH" = "arm64" ]thenEXPORT="GASPP_FIX_XCODE5=1"fifiXCRUN_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" ]thenAS="gas-preprocessor.pl -arch aarch64 -- $CC"elseAS="gas-preprocessor.pl -- $CC"fiCXXFLAGS="$CFLAGS"LDFLAGS="$CFLAGS"if [ "$X264" ]thenCFLAGS="$CFLAGS -I$X264/include"LDFLAGS="$LDFLAGS -L$X264/lib"fiif [ "$FDK_AAC" ]thenCFLAGS="$CFLAGS -I$FDK_AAC/include"LDFLAGS="$LDFLAGS -L$FDK_AAC/lib"fiTMPDIR=${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 1make -j5 install $EXPORT || exit 1cd $CWDdone
fiif [ "$LIPO" ]
thenecho "building fat binaries..."mkdir -p $FAT/libset - $ARCHSCWD=`pwd`cd $THIN/$1/libfor LIB in *.adocd $CWDecho lipo -create `find $THIN -name $LIB` -output $FAT/lib/$LIB 1>&2lipo -create `find $THIN -name $LIB` -output $FAT/lib/$LIB || exit 1donecd $CWDcp -rf $THIN/$1/include $FAT
fiecho Done

  4.5 根据架构进行编译,编译脚本(编译的裁剪版) 

    1)如果出现权限问题,执行命令

chmod 777 ./build-ffmpeg-simple.sh

    2)编译所有版本,执行脚本指令 

./build-ffmpeg-simple.sh

    编译所有版本,操作如下图: 

    编译所有版本,scratch 文件夹,如下图: 

    编译所有版本,thin 文件夹中头文件,如下图: 

    编译所有版本,thin 文件夹中静态库文件,如下图: 

    编译所有版本,FFmpeg-iOS 文件夹,如下图: 

    3)编译 arm64 架构版本,执行脚本指令 

./build-ffmpeg-simple.sh arm64

    4)编译 armv7 x86_64 i386 架构版本,执行脚本指令

./build-ffmpeg-simple.sh armv7 x86_64 i386

    5)lipo 合成库,指令

./build-ffmpeg-simple.sh lipo

    lipo 合成库,如把 arm64,x86_64 合并, 操作如图:

    lipo 合成库,如把 arm64,x86_64 合并, 合并到文件夹如图: 

5. 查看 .a 文件支持的内核架构指令: 

lipo -info FFmpeg-iOS/lib/libavcodec.a

  查询的结果:

lihanyang@MacBook-Pro FFmpeg % lipo -info FFmpeg-iOS/lib/libavcodec.a
Architectures in the fat file: FFmpeg-iOS/lib/libavcodec.a are: x86_64 arm64