macOS下从零开始编译ffmpeg
2019-06-22 本文已影响0人
stephen__liu
1. FFMpeg 编译选项:
./configure --as=yasm --enable-nonfree --enable-shared --disable-static --enable-ffplay --enable-gpl --enable-pthreads --enable-postproc --enable-libmp3lame --enable-libtheora --enable-libvorbis --enable-libx264 --enable-libxvid --enable-libspeex --enable-bzlib --enable-zlib --enable-libsnappy --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-version3 --enable-libfdk-aac --enable-libvpx --enable-encoder=pcm_mulaw --enable-encoder=flac --enable-libgsm --enable-libopus --enable-fontconfig --enable-libfreetype --enable-libass --enable-libbluray --enable-filters --extra-cflags=-mmacosx-version-min=10.9 --extra-ldflags=-mmacosx-version-min=10.9
注意编译选项:
--extra-cflags=-mmacosx-version-min=10.9 --extra-ldflags=-mmacosx-version-min=10.9
在 macOS 上编译需要注意这个选项,指定编译库兼容的最低系统版本,编译其他依赖库(动态库)的时候也需要指定,因为编译出来的动态库如果需要打包到app并发布到AppStore, 必须指定这个,如果不指定,无法上传到Appstore。报如下错误:
Couldn't find platform family in Info.plist CFBundleSupportedPlatforms or Mach-O LC_VERSION_MIN for libass.9.dylib
如果成功编译,使用 otool -l *.dylib 命令查看 load command 8 :
Load command 8
cmd LC_VERSION_MIN_MACOSX
cmdsize 16
version 10.9
sdk 10.14
如果没有成功指定最低兼容版本就是下面这样:
Load command 8
cmd LC_BUILD_VERSION
cmdsize 32
platform macos
sdk 10.14
minos 10.14
ntools 1
tool ld
version 409.12
这点在macOS上编译很重要,尤其在产品需要发布AppStore的情况下。
2.依赖库编译安装
这里其实也可以直接使用brew安装依赖库:
brew install opencore-amr
brew install lame
brew install speex
brew install theora
就像前面所说需要指定最低兼容版本号,所以所有依赖库都需要下载源码一个一个编译:
yasm
libsnappy
libogg
libtheora
libvorbis
opencore-amr
speex
x264
fdk-aac
fontconfig
freetype
fribidi
gsm
gsm源码编译不需要configure,项目下自带 Makefile 文件,直接执行 make 就行,但是如果想要编译出动态库.dylib文件,需要手动修改 Makefile 文件:
line 143 add :
LIBGSMSO= $(LIB)/libgsm.1.0.13.dylib
line 302 add :
$(LIBGSMSO): $(LIB) $(GSM_OBJECTS)
$(LD) "-mmacosx-version-min=10.9" -o $(LIBGSMSO) -dynamiclib -Wl,-compatibility_version,1,-current_version,1.0.13,-install_name,$(LIBGSMSO) $(GSM_OBJECTS) -lc
ln -fs libgsm.1.0.13.dylib lib/libgsm.1.dylib
ln -fs libgsm.1.0.13.dylib lib/libgsm.dylib
lame
./configure CFLAGS="-mmacosx-version-min=10.9"
3.100版本源码下载下来有个编译问题,用如下方式解决:
Remove the line containing 'lame_init_old' from the file 'include/libmp3lame.sym'
libass
libbluray
SDL2
opus
xvidcore
关于依赖库编译
代码下载下来后开始逐个编译安装,基本都遵从 ./configure / make / make install 这个过程:
还是需要注意之前说到过的制定最低兼容系统版本号问题:
by configuring the autotools utility with
./configure CFLAGS="-mmacosx-version-min=10.9" LDFLAGS="-mmacosx-version-min=10.9"
and by configuring the other cmake utility with
-D CMAKE_OSX_DEPLOYMENT_TARGET=10.9
3. 关于动态库打包和安装
编译出来的动态库默认加载路径是 /usr/local/lib, 如果希望把库打包到app里并发布到appstore给其他人使用,这个加载路径就必须改,使用 install_name_tool 工具可以达到目的:
#!/bin/sh
# change libavdevide.dylib
install_name_tool -change /usr/local/lib/libavfilter.7.dylib @loader_path/libavfilter.7.dylib libavdevice.58.dylib
install_name_tool -change /usr/local/lib/libavformat.58.dylib @loader_path/libavformat.58.dylib libavdevice.58.dylib
install_name_tool -change /usr/local/lib/libavcodec.58.dylib @loader_path/libavcodec.58.dylib libavdevice.58.dylib
install_name_tool -change /usr/local/lib/libavutil.56.dylib @loader_path/libavutil.56.dylib libavdevice.58.dylib
install_name_tool -change /usr/local/lib/libpostproc.55.dylib @loader_path/libpostproc.55.dylib libavdevice.58.dylib
install_name_tool -change /usr/local/lib/libswresample.3.dylib @loader_path/libswresample.3.dylib libavdevice.58.dylib
install_name_tool -change /usr/local/lib/libswscale.5.dylib @loader_path/libswscale.5.dylib libavdevice.58.dylib
install_name_tool -change /usr/local/lib/libfreetype.6.dylib @loader_path/libfreetype.6.dylib libavdevice.58.dylib
install_name_tool -change /usr/local/lib/libfontconfig.1.dylib @loader_path/libfontconfig.1.dylib libavdevice.58.dylib
install_name_tool -change /usr/local/opt/sdl2/lib/libSDL2-2.0.0.dylib @loader_path/libSDL2.dylib libavdevice.58.dylib
install_name_tool -change /usr/local/lib/libvorbisenc.2.dylib @loader_path/libvorbisenc.2.dylib libavdevice.58.dylib
install_name_tool -change /usr/local/lib/libvorbis.0.dylib @loader_path/libvorbis.0.dylib libavdevice.58.dylib
install_name_tool -change /usr/local/lib/libx264.157.dylib @loader_path/libx264.157.dylib libavdevice.58.dylib
install_name_tool -change /usr/local/lib/libmp3lame.0.dylib @loader_path/libmp3lame.0.dylib libavdevice.58.dylib
install_name_tool -change /usr/local/lib/libfdk-aac.2.dylib @loader_path/libfdk-aac.2.dylib libavdevice.58.dylib
install_name_tool -change /usr/local/lib/libspeex.1.dylib @loader_path/libspeex.1.dylib libavdevice.58.dylib
install_name_tool -change /usr/local/lib/libopus.0.dylib @loader_path/libopus.0.dylib libavdevice.58.dylib
install_name_tool -change /usr/local/opt/libgsm/lib/libgsm.1.0.13.dylib @loader_path/libgsm.1.0.13.dylib libavdevice.58.dylib
install_name_tool -change /usr/local/lib/libbluray.2.dylib @loader_path/libbluray.2.dylib libavdevice.58.dylib
install_name_tool -change /usr/local/lib/libass.9.dylib @loader_path/libass.9.dylib libavdevice.58.dylib
install_name_tool -change /usr/local/lib/libogg.0.dylib @loader_path/libogg.0.dylib libavdevice.58.dylib
install_name_tool -change /usr/local/lib/libtheoraenc.1.dylib @loader_path/libtheoraenc.1.dylib libavdevice.58.dylib
install_name_tool -change /usr/local/lib/libtheoradec.1.dylib @loader_path/libtheoradec.1.dylib libavdevice.58.dylib
install_name_tool -change /usr/local/lib/libopencore-amrwb.0.dylib @loader_path/libopencore-amrwb.0.dylib libavdevice.58.dylib
install_name_tool -change /usr/local/lib/libopencore-amrnb.0.dylib @loader_path/libopencore-amrnb.0.dylib libavdevice.58.dylib
install_name_tool -change @rpath/libsnappy.1.dylib @loader_path/libsnappy.1.dylib libavdevice.58.dylib
# change libavfilter.dylib
install_name_tool -change /usr/local/lib/libavformat.58.dylib @loader_path/libavformat.58.dylib libavfilter.7.dylib
install_name_tool -change /usr/local/lib/libavcodec.58.dylib @loader_path/libavcodec.58.dylib libavfilter.7.dylib
install_name_tool -change /usr/local/lib/libavutil.56.dylib @loader_path/libavutil.56.dylib libavfilter.7.dylib
install_name_tool -change /usr/local/lib/libpostproc.55.dylib @loader_path/libpostproc.55.dylib libavfilter.7.dylib
install_name_tool -change /usr/local/lib/libswresample.3.dylib @loader_path/libswresample.3.dylib libavfilter.7.dylib
install_name_tool -change /usr/local/lib/libswscale.5.dylib @loader_path/libswscale.5.dylib libavfilter.7.dylib
install_name_tool -change /usr/local/lib/libfreetype.6.dylib @loader_path/libfreetype.6.dylib libavfilter.7.dylib
install_name_tool -change /usr/local/lib/libfontconfig.1.dylib @loader_path/libfontconfig.1.dylib libavfilter.7.dylib
install_name_tool -change /usr/local/opt/sdl2/lib/libSDL2-2.0.0.dylib @loader_path/libSDL2.dylib libavfilter.7.dylib
install_name_tool -change /usr/local/lib/libvorbisenc.2.dylib @loader_path/libvorbisenc.2.dylib libavfilter.7.dylib
install_name_tool -change /usr/local/lib/libvorbis.0.dylib @loader_path/libvorbis.0.dylib libavfilter.7.dylib
install_name_tool -change /usr/local/lib/libx264.157.dylib @loader_path/libx264.157.dylib libavfilter.7.dylib
install_name_tool -change /usr/local/lib/libmp3lame.0.dylib @loader_path/libmp3lame.0.dylib libavfilter.7.dylib
install_name_tool -change /usr/local/lib/libfdk-aac.2.dylib @loader_path/libfdk-aac.2.dylib libavfilter.7.dylib
install_name_tool -change /usr/local/lib/libspeex.1.dylib @loader_path/libspeex.1.dylib libavfilter.7.dylib
install_name_tool -change /usr/local/lib/libopus.0.dylib @loader_path/libopus.0.dylib libavfilter.7.dylib
install_name_tool -change /usr/local/opt/libgsm/lib/libgsm.1.0.13.dylib @loader_path/libgsm.1.0.13.dylib libavfilter.7.dylib
install_name_tool -change /usr/local/lib/libbluray.2.dylib @loader_path/libbluray.2.dylib libavfilter.7.dylib
install_name_tool -change /usr/local/lib/libass.9.dylib @loader_path/libass.9.dylib libavfilter.7.dylib
install_name_tool -change /usr/local/lib/libogg.0.dylib @loader_path/libogg.0.dylib libavfilter.7.dylib
install_name_tool -change /usr/local/lib/libtheoraenc.1.dylib @loader_path/libtheoraenc.1.dylib libavfilter.7.dylib
install_name_tool -change /usr/local/lib/libtheoradec.1.dylib @loader_path/libtheoradec.1.dylib libavfilter.7.dylib
install_name_tool -change /usr/local/lib/libopencore-amrwb.0.dylib @loader_path/libopencore-amrwb.0.dylib libavfilter.7.dylib
install_name_tool -change /usr/local/lib/libopencore-amrnb.0.dylib @loader_path/libopencore-amrnb.0.dylib libavfilter.7.dylib
install_name_tool -change @rpath/libsnappy.1.dylib @loader_path/libsnappy.1.dylib libavfilter.7.dylib
# change libavcodec.dylib
install_name_tool -change /usr/local/lib/libavfilter.7.dylib @loader_path/libavfilter.7.dylib libavcodec.58.dylib
install_name_tool -change /usr/local/lib/libavformat.58.dylib @loader_path/libavformat.58.dylib libavcodec.58.dylib
install_name_tool -change /usr/local/lib/libavutil.56.dylib @loader_path/libavutil.56.dylib libavcodec.58.dylib
install_name_tool -change @rpath/libsnappy.1.dylib @loader_path/libsnappy.1.dylib libavcodec.58.dylib
install_name_tool -change /usr/local/lib/libswresample.3.dylib @loader_path/libswresample.3.dylib libavcodec.58.dylib
install_name_tool -change /usr/local/lib/libvorbisenc.2.dylib @loader_path/libvorbisenc.2.dylib libavcodec.58.dylib
install_name_tool -change /usr/local/lib/libvorbis.0.dylib @loader_path/libvorbis.0.dylib libavcodec.58.dylib
install_name_tool -change /usr/local/lib/libx264.157.dylib @loader_path/libx264.157.dylib libavcodec.58.dylib
install_name_tool -change /usr/local/lib/libmp3lame.0.dylib @loader_path/libmp3lame.0.dylib libavcodec.58.dylib
install_name_tool -change /usr/local/lib/libfdk-aac.2.dylib @loader_path/libfdk-aac.2.dylib libavcodec.58.dylib
install_name_tool -change /usr/local/lib/libspeex.1.dylib @loader_path/libspeex.1.dylib libavcodec.58.dylib
install_name_tool -change /usr/local/lib/libopus.0.dylib @loader_path/libopus.0.dylib libavcodec.58.dylib
install_name_tool -change /usr/local/opt/libgsm/lib/libgsm.1.0.13.dylib @loader_path/libgsm.1.0.13.dylib libavcodec.58.dylib
install_name_tool -change /usr/local/lib/libbluray.2.dylib @loader_path/libbluray.2.dylib libavcodec.58.dylib
install_name_tool -change /usr/local/lib/libass.9.dylib @loader_path/libass.9.dylib libavcodec.58.dylib
install_name_tool -change /usr/local/lib/libogg.0.dylib @loader_path/libogg.0.dylib libavcodec.58.dylib
install_name_tool -change /usr/local/lib/libtheoraenc.1.dylib @loader_path/libtheoraenc.1.dylib libavcodec.58.dylib
install_name_tool -change /usr/local/lib/libtheoradec.1.dylib @loader_path/libtheoradec.1.dylib libavcodec.58.dylib
install_name_tool -change /usr/local/lib/libopencore-amrwb.0.dylib @loader_path/libopencore-amrwb.0.dylib libavcodec.58.dylib
install_name_tool -change /usr/local/lib/libopencore-amrnb.0.dylib @loader_path/libopencore-amrnb.0.dylib libavcodec.58.dylib
# change libavformat.58.dylib
install_name_tool -change /usr/local/lib/libavformat.58.dylib @loader_path/libavformat.58.dylib libavformat.58.dylib
install_name_tool -change /usr/local/lib/libavcodec.58.dylib @loader_path/libavcodec.58.dylib libavformat.58.dylib
install_name_tool -change /usr/local/lib/libavutil.56.dylib @loader_path/libavutil.56.dylib libavformat.58.dylib
install_name_tool -change /usr/local/lib/libpostproc.55.dylib @loader_path/libpostproc.55.dylib libavformat.58.dylib
install_name_tool -change /usr/local/lib/libswresample.3.dylib @loader_path/libswresample.3.dylib libavformat.58.dylib
install_name_tool -change /usr/local/lib/libvorbisenc.2.dylib @loader_path/libvorbisenc.2.dylib libavformat.58.dylib
install_name_tool -change /usr/local/lib/libvorbis.0.dylib @loader_path/libvorbis.0.dylib libavformat.58.dylib
install_name_tool -change /usr/local/lib/libx264.157.dylib @loader_path/libx264.157.dylib libavformat.58.dylib
install_name_tool -change /usr/local/lib/libmp3lame.0.dylib @loader_path/libmp3lame.0.dylib libavformat.58.dylib
install_name_tool -change /usr/local/lib/libfdk-aac.2.dylib @loader_path/libfdk-aac.2.dylib libavformat.58.dylib
install_name_tool -change /usr/local/lib/libspeex.1.dylib @loader_path/libspeex.1.dylib libavformat.58.dylib
install_name_tool -change /usr/local/lib/libopus.0.dylib @loader_path/libopus.0.dylib libavformat.58.dylib
install_name_tool -change /usr/local/opt/libgsm/lib/libgsm.1.0.13.dylib @loader_path/libgsm.1.0.13.dylib libavformat.58.dylib
install_name_tool -change /usr/local/lib/libbluray.2.dylib @loader_path/libbluray.2.dylib libavformat.58.dylib
install_name_tool -change /usr/local/lib/libass.9.dylib @loader_path/libass.9.dylib libavformat.58.dylib
install_name_tool -change /usr/local/lib/libogg.0.dylib @loader_path/libogg.0.dylib libavformat.58.dylib
install_name_tool -change /usr/local/lib/libtheoraenc.1.dylib @loader_path/libtheoraenc.1.dylib libavformat.58.dylib
install_name_tool -change /usr/local/lib/libtheoradec.1.dylib @loader_path/libtheoradec.1.dylib libavformat.58.dylib
install_name_tool -change /usr/local/lib/libopencore-amrwb.0.dylib @loader_path/libopencore-amrwb.0.dylib libavformat.58.dylib
install_name_tool -change /usr/local/lib/libopencore-amrnb.0.dylib @loader_path/libopencore-amrnb.0.dylib libavformat.58.dylib
install_name_tool -change @rpath/libsnappy.1.dylib @loader_path/libsnappy.1.dylib libavformat.58.dylib
# change libswscale.dylib
install_name_tool -change /usr/local/lib/libavutil.56.dylib @loader_path/libavutil.56.dylib libswscale.5.dylib
# change libpostproc.dylib
install_name_tool -change /usr/local/lib/libavutil.56.dylib @loader_path/libavutil.56.dylib libpostproc.55.dylib
# change libswresample.dylib
install_name_tool -change /usr/local/lib/libavutil.56.dylib @loader_path/libavutil.56.dylib libswresample.3.dylib
# libfontconfig
install_name_tool -change /usr/local/lib/libfreetype.6.dylib @loader_path/libfreetype.6.dylib libfontconfig.1.dylib
# bluray
install_name_tool -change /usr/local/lib/libfreetype.6.dylib @loader_path/libfreetype.6.dylib libbluray.2.dylib
install_name_tool -change /usr/local/lib/libfontconfig.1.dylib @loader_path/libfontconfig.1.dylib libbluray.2.dylib
# ass
install_name_tool -change /usr/local/lib/libfreetype.6.dylib @loader_path/libfreetype.6.dylib libass.9.dylib
install_name_tool -change /usr/local/lib/libfontconfig.1.dylib @loader_path/libfontconfig.1.dylib libass.9.dylib
install_name_tool -change /usr/local/lib/libfribidi.0.dylib @loader_path/libfribidi.0.dylib libass.9.dylib
# change libvorbisenc.dylib
install_name_tool -change /usr/local/lib/libvorbis.0.dylib @loader_path/libvorbis.0.dylib libvorbisenc.2.dylib
install_name_tool -change /usr/local/lib/libogg.0.dylib @loader_path/libogg.0.dylib libvorbisenc.2.dylib
# change libvorbis.dylib
install_name_tool -change /usr/local/lib/libogg.0.dylib @loader_path/libogg.0.dylib libvorbis.0.dylib
# change libtheoraenc.dylib
install_name_tool -change /usr/local/lib/libogg.0.dylib @loader_path/libogg.0.dylib libtheoraenc.1.dylib
# change id of some dylibs
install_name_tool -id @executable_path/libavdevice.58.dylib libavdevice.58.dylib
install_name_tool -id @executable_path/libavfilter.7.dylib libavfilter.7.dylib
install_name_tool -id @executable_path/libavcodec.58.dylib libavcodec.58.dylib
install_name_tool -id @executable_path/libavutil.56.dylib libavutil.56.dylib
install_name_tool -id @executable_path/libavformat.58.dylib libavformat.58.dylib
install_name_tool -id @executable_path/libswscale.5.dylib libswscale.5.dylib
install_name_tool -id @executable_path/libpostproc.55.dylib libpostproc.55.dylib
install_name_tool -id @executable_path/libswresample.3.dylib libswresample.3.dylib
install_name_tool -id @executable_path/libfdk-aac.2.dylib libfdk-aac.2.dylib
install_name_tool -id @executable_path/libmp3lame.0.dylib libmp3lame.0.dylib
install_name_tool -id @executable_path/libx264.157.dylib libx264.157.dylib
install_name_tool -id @executable_path/libxvidcore.4.dylib libxvidcore.4.dylib
install_name_tool -id @executable_path/libtheoradec.1.dylib libtheoradec.1.dylib
install_name_tool -id @executable_path/libtheoraenc.1.dylib libtheoraenc.1.dylib
install_name_tool -id @executable_path/libopus.0.dylib libopus.0.dylib
install_name_tool -id @executable_path/libspeex.1.dylib libspeex.1.dylib
install_name_tool -id @executable_path/libvorbis.0.dylib libvorbis.0.dylib
install_name_tool -id @executable_path/libvorbisenc.2.dylib libvorbisenc.2.dylib
install_name_tool -id @executable_path/libass.9.dylib libass.9.dylib
install_name_tool -id @executable_path/libsnappy.1.dylib libsnappy.1.dylib
install_name_tool -id @executable_path/libbluray.2.dylib libbluray.2.dylib
install_name_tool -id @executable_path/libfribidi.0.dylib libfribidi.0.dylib
install_name_tool -id @executable_path/libSDL2-2.0.0.dylib libSDL2.dylib
# freetype ogg amr
install_name_tool -id @executable_path/libfreetype.6.dylib libfreetype.6.dylib
install_name_tool -id @executable_path/libfontconfig.1.dylib libfontconfig.1.dylib
install_name_tool -id @executable_path/libgsm.1.0.13.dylib libgsm.1.0.13.dylib
install_name_tool -id @executable_path/libogg.0.dylib libogg.0.dylib
install_name_tool -id @executable_path/libopencore-amrnb.0.dylib libopencore-amrnb.0.dylib
install_name_tool -id @executable_path/libopencore-amrwb.0.dylib libopencore-amrwb.0.dylib
以上是针对所有动态库的修改,可以保存到shell文件,一键执行完成。