您现在的位置是:首页 >技术交流 >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 yams
brew 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 != "" ]]; 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
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
# avresample
#CONFIGURE_FLAGS="$CONFIGURE_FLAGS --enable-avresample"
ARCHS="arm64 armv7 x86_64 i386"
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
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 != "" ]]; 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
CONFIGURE_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" ]
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"
ARCHS="arm64 armv7 x86_64 i386"
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 -j5 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
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