音视频开发音视频从入门到放弃

MAC编译ffmpeg+x264+mp3lame(IOS和and

2019-08-07  本文已影响0人  仙人掌__

前言

FFmpeg是一个很强大的垮平台的音视频处理库,它可用于Mac/windows/ios/android/linux等各个平台,FFmpeg库自身native代码实现了很多功能,也引入了其它的很多外部库,比如x264、fdk_aac、lamemp3等等,然后对这些外部库做封装,对外则提供了统一的接口。如下提供了尽量简单的方法编译用于这些平台的ffmpeg库(引入常用的x264、fdk_aac、lamemp3外部库)。

编译基础知识

编译一般分为本地编译和交叉编译,本地编译是相对于交叉编译而言的。本地编译一般指在PC上编译用于当前PC平台系统使用的程序或者库,交叉编译一般指在本地PC平台上用指定的交叉编译器编译用于其它嵌入式平台用的程序或者库,如在Mac平台上编译iOS平台用的FFmpeg库。每一个嵌入式平台都会提供PC平台的交叉编译工具链,他们都会有如下几个工具:

CC:C语言编译器
CXX:C++编译器
AS:汇编语言编译器
AR:打包器,将.o文件打包(CC/CXX/AS编译器生成的为.o文件)
LD:连接器,将库文件和.o文件连接成可执行程序(如.out文件)
NM:查看静态库文件中的符号表
GDB:调试工具
STRIP:通过优化减小可执行文件或者库文件体积
Objdump:查看静态库或者动态库的方法签名

如:

gcc -c main.cpp -I ./include
ar libmedia.a media.o
gcc -o main main.o -L media

make编译工具

通常我们通过命令编译的步骤如下:

// 生成中间文件
gcc -c  main.c
// 生成可执行文件(可执行程序 main;生成库的步骤也一样)
gcc -o main main.o

那如果工程有成千上万个文件,那么我们这样的命令就需要写成千上万变,显然是非常不方便的。make是一个非常强大的工具,它可以极大简化如上的编译命令,生成想要的库或者程序。基本如下:

执行过程如下:
当调用./configure .....命令时将会配置make的相关参数
当调用make ..... 或者make install 命令时,将会调用make工具进行编译连接并生成可执行程序或者库文件

编译mac平台下使用的ffmpeg库

1、homebrew(推荐)
2、make工具手动编译

IOS和android平台交叉编译工具

每个操作系统(MAC osx/windows/linux)都有提供ios和android平台的交叉编译工具链,这里以mac 系统为例,ios和android的交叉编译工具如下:
1、ios交叉编译工具路径
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
在这个路径下就有上面说的那些 cc,ar等工具,这里的cc实际上是指向了clang编译器
ios编译中bitcode
开启之后app性能会增加,关闭则有可能降低性能
xcrun -sdk iphoneos clang -arch arm64 意思就是调用clang编译器编译64位的源代码

2、android交叉编译工具路径
<安卓sdk/ndk-bundle路径>/toolchains/[aarch64-linux-android-4.9(编译64位arm,具体版本号根据安装的ndk版本确定)][arm-linux-androideabi-4.9(编译32位)]/prebuilt/darwin-x86_64/bin
在这个路径下就有上面那些cc,ar等工具

编译环境

开始编译

这里的脚本是基于ijkplayer脚本经过改写而成,默认会将x264库,fdkaac库,lame库全部编译到ffmpeg中,ijkplayer是一个很优秀的国内开源库,github地址:ijkplayer开源地址

编译脚本地址:脚本地址

git clone https://github.com/nldzsz/ffmpeg-build-scripts.git

一、 编译 ffmpeg for iOS

cd ffmpeg-build-scripts
./compile-ffmpeg-ios.sh all

执行此脚本会先检查系统中是否安装 brew yasm gas-preprocessor.pl工具,如果没有则会自动安装;
接着会自动下载ffmpeg/x264/mp3lame/fdk-aac源码;如果不想下载x264/mp3lame/fdk-aac三个库,只需要将脚本./compile-init.sh中如下对应代码注释即可:

# 拉取 x264源码
echo "== pull x264 base =="
sh $TOOLS/pull-repo-base.sh $X264_UPSTREAM $X264_LOCAL_REPO

# 拉取 fdkaac源码
echo "== pull fdkaac base =="
sh $TOOLS/curl-repo-base.sh $FDKAAC_UPSTREAM $FDKAAC_LOCAL_REPO $FDKAAC_VERSION

# 拉取 mp3lame源码
echo "== pull mp3lame base =="
sh $TOOLS/curl-repo-base.sh $MP3LAME_UPSTREAM $MP3LAME_LOCAL_REPO $MP3LAME_VERSION

# 拉取 ffmpeg源码
echo "== pull ffmpeg base =="
sh $TOOLS/pull-repo-base.sh $FFMPEG_UPSTREAM $FFMPEG_LOCAL_REPO

默认会编译 arm64 x86_64两个平台的静态库

编译后的结果如下:
[图片上传中...(image.png-b02ffa-1593251833927-0)]

这里开启了x264/mp3lame/fdk-aac三个外部库,libxrzffmpeg.a是将lib中所有的库合并成一个库(推荐使用),如果不做任何裁剪,可以看到默认的.a库还是挺大的,以上是编译的默认配置的库

// 开启rtsp解析协议
export COMMON_FF_CFG_FLAGS="$COMMON_FF_CFG_FLAGS --enable-protocol=rtp"

export COMMON_FF_CFG_FLAGS="$COMMON_FF_CFG_FLAGS --enable-demuxer=rtsp"

在ios项目中使用ffmpeg的.a库
1、直接将universal/lib目录拖进工程(或者将lib目录中的库替换成libxrzffmpeg.a一个库)
2、配置头文件和库文件搜索路径,如下


image.png

3、添加外部库

libiconv.2.4.0.tbd
libz.1.2.5.tbd

如下:


image.png

二、编译ffmpeg for android

cd ffmpeg-build-scripts
./compile-ffmpeg-android.sh reset
./compile-ffmpeg-android.sh all

点击"仍然允许"即可。从android studio下载的则不会(来自Google的说法,未尝试过)

最后编译结果如下:


image.png

导入android工程,通过cmake方式导入


image.png

CMakeLists.txt文件导入ffmpeg动态库的关键代码如下:
设置ffmpeg头文件路径

set(SRC_DIR ${PROJECT_SOURCE_DIR}/src/main/cpp)
set(FFMPEG_INC ${SRC_DIR}/ffmpeg)
include_directories(${FFMPEG_INC})

设置ffmpeg.so动态库文件路径

# import ffmpeg library
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -L${CMAKE_SOURCE_DIR}/src/main/jniLibs/${CMAKE_ANDROID_ARCH_ABI}")
# ding yi jiang gai ku da bao jin app de lei xing
add_library(adMedia SHARED
        ${src_cpp}
        ${ffmpeg_demo}
        ${ffmpeg_common}
        ${com_cpp}
        ${opensles_cpp}
        )
target_link_libraries(adMedia android log
        OpenSLES
        mediandk
        avformat avcodec avfilter avutil swresample swscale
        ${log-lib}
    )

tips:编译动态库时没有加入fdk_aac库,因为还没找到去掉libfdk-aac.so.2.0.0后缀的办法,如果没有去掉后缀,引入安卓时会提示找不到"fdk-aac.so.2"的错误

找到脚本compile-ffmpeg-android.sh,做如下修改
export FF_COMPILE_SHARED=FALSE
export FF_COMPILE_STATIC=TRUE
然后执行
./compile-ffmpeg-android.sh all

最终生成的静态库如下:

静态ffmpeg库的引入方式
1、将相关库拖入对应目录
2、配置CMakeLists.txt
设置ffmpeg头文件路径和动态库一样

set(SRC_DIR {PROJECT_SOURCE_DIR}/src/main/cpp) set(FFMPEG_INC{SRC_DIR}/ffmpeg)
include_directories(${FFMPEG_INC})
设置ffmpeg 相关静态库文件路径

# import ffmpeg library
# import ffmpeg library
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -L${CMAKE_SOURCE_DIR}/src/main/jniLibs/${CMAKE_ANDROID_ARCH_ABI} -lm -lz")
# ding yi jiang gai ku da bao jin app de lei xing
add_library(adMedia SHARED
        ${src_cpp}
        ${ffmpeg_demo}
        ${ffmpeg_common}
        ${com_cpp}
        ${opensles_cpp}
        )
target_link_libraries(adMedia android log
        OpenSLES
        mediandk
        # for dynamic
#        avformat avcodec avfilter avutil swresample swscale
        # for static
        libavformat.a libavcodec.a libavfilter.a libavutil.a libswresample.a libswscale.a libfdk-aac.a libx264.a libmp3lame.a
        ${log-lib}
    )
brew install ffmpeg

会自动安装最新的ffmpeg,如果没有安装homebrew,则使用如下命令安装

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

homebrew卸载ffmpeg

brew uninstall ffmpeg
cd ffmpeg-build-scripts
./compile-ffmpeg-mac.sh all

会生成ffmpeg.a库,如下:


image.png 3.jpg

注意第二张图,需要添加如下额外的库,不然会出现错误。

FFmpeg许可协议

在使用开源代码时要清除开源代码所使用的的许可协议,因为可能涉及到一定的法律风险。常见的开源许可协议:

ffmpeg关于协议的configure参数为

Licensing options:
  --enable-gpl             allow use of GPL code, the resulting libs
                           and binaries will be under GPL [no]
  --enable-version3        upgrade (L)GPL to version 3 [no]
  --enable-nonfree         allow use of nonfree code, the resulting libs
                           and binaries will be unredistributable [no]

1、ffmpeg协议默认为LGPL v2.1+
2、如果ffmpeg引入了x264库或者使用GPL的模块,那么ffmpeg将变成GPL许可协议,需要开启--enable-gpl
3、ffmpeg使用了gmp、libaribb24等基于LGPL v3的模块,则需要开启--enable-version3
4、如果ffmpeg引入了fdk-aac等和GPL不兼容的外部库,则需要开启--enable-nonfree,编译后ffmpeg变成unredistributable

具体参考官网
许可协议文档1
许可协议介绍

遇到问题

ld: warning: could not create compact unwind for _ff_init_vlc_sparse: stack subq instruction is too different from dwarf stack size
ld: warning: could not create compact unwind for _ff_init_2d_vlc_rl: stack subq instruction is too different from dwarf stack size
ld: warning: could not create compact unwind for _ff_rl_init_vlc: stack subq instruction is too different from dwarf stack size
Undefined symbols for architecture x86_64:
  "_x264_encoder_close", referenced from:
      _X264_close in libavcodec.a(libx264.o)
  "_x264_picture_init", referenced from:
      _X264_frame in libavcodec.a(libx264.o)
  "_x264_encoder_reconfig", referenced from:
      _X264_frame in libavcodec.a(libx264.o)
  "_x264_encoder_open_157", referenced from:
      _X264_init in libavcodec.a(libx264.o)
  "_x264_encoder_encode", referenced from:
      _X264_frame in libavcodec.a(libx264.o)
  "_x264_param_default", referenced from:
      _X264_init in libavcodec.a(libx264.o)
  "_x264_param_apply_fastfirstpass", referenced from:
      _X264_init in libavcodec.a(libx264.o)
  "_x264_levels", referenced from:
      _X264_init in libavcodec.a(libx264.o)
  "_x264_param_default_preset", referenced from:
      _X264_init in libavcodec.a(libx264.o)
  "_x264_param_apply_profile", referenced from:
      _X264_init in libavcodec.a(libx264.o)
  "_x264_param_parse", referenced from:
      _X264_init in libavcodec.a(libx264.o)
  "_x264_encoder_delayed_frames", referenced from:
      _X264_frame in libavcodec.a(libx264.o)
  "_x264_encoder_headers", referenced from:
      _X264_init in libavcodec.a(libx264.o)
  "_x264_encoder_maximum_delayed_frames", referenced from:
      _X264_init in libavcodec.a(libx264.o)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (us

编译的libx264.a也要一并添加进去(如果是用的libxrzffmpeg.a则不需要加)

2、ffmpeg 4.0编译fdk-aac时遇到的错误
libavcodec/libfdk-aacenc.c:292:34: error: ‘AACENC_InfoStruct {aka struct <anonymous>}’ has no member named ‘encoderDelay’ #93
换成4.2,解决了此问题

3、xcode环境不全时编译ffmpeg 提示 No working C compiler found.
执行如下命令:

xcode-select --install # Install Command Line Tools if you haven't already.
sudo xcode-select --switch /Library/Developer/CommandLineTools # Enable command line tools
# Change the path if you installed Xcode somewhere else.
sudo xcode-select -s /Applications/Xcode.app/Contents/Developer

4、编译x264时,如果提示Found no assembler Minimum version is nasm-2.13
说明系统的nasm版本过低,重新安装即可

brew reinstall nasm

5、ffmpeg引用错误
必须要添加如下对应的库,否则可能会出现对应的错误,如下:
libiconv.2.4.0.tbd,libz.1.2.5.tbd

Undefined symbols for architecture xxx
"deflateInit2", referenced from:
_encode_frame in libavcodec.a(pngenc.o)
"_crc32", referenced from:
_encode_frame in libavcodec.a(pngenc.o)
"_deflateReset", referenced from:
_encode_frame in libavcodec.a(lclenc.o)
_encode_frame in libavcodec.a(zmbvenc.o)
"_compress", referenced from:
.......

Security.framework

Undefined symbols for architecture x86_64:
  "_SSLClose", referenced from:
      _tls_open in libavformat.a(tls_securetransport.o)
      _tls_close in libavformat.a(tls_securetransport.o)
  "_SSLCopyPeerTrust", referenced from:
......

VideoDecodeAcceleration.framework

Undefined symbols for architecture x86_64:
  "_VDADecoderCreate", referenced from:
      _ff_vda_create_decoder in libavcodec.a(vda_h264.o)
      _ff_vda_default_init in libavcodec.a(vda_h264.o)
  "_VDADecoderDecode", referenced from:
.....

AudioToolbox.framework

Undefined symbols for architecture x86_64:
  "_AudioConverterDispose", referenced from:
      _ffat_close_decoder in libavcodec.a(audiotoolboxdec.o)
      _ffat_close_encoder in libavcodec.a(audiotoolboxenc.o)
  "_AudioConverterFillComplexBuffer", referenced from:
....

CoreMedia.framework

Undefined symbols for architecture x86_64:
  "_CMBlockBufferCopyDataBytes", referenced from:
      _vtenc_frame in libavcodec.a(videotoolboxenc.o)
  "_CMBlockBufferCreateWithMemoryBlock", referenced from:
      _videotoolbox_common_end_frame in libavcodec.a(videotoolbox.o)
  "_CMSampleBufferCreate", referenced from:
.....

CoreFoundation.framework

Undefined symbols for architecture x86_64:
  "_CFArrayCreateMutableCopy", referenced from:
      _tls_open in libavformat.a(tls_securetransport.o)
  "_CFArrayGetCount", referenced from:
      _vtenc_frame in libavcodec.a(videotoolboxenc.o)
......

CoreVideo.framework

Undefined symbols for architecture x86_64:
  "_CVBufferSetAttachments", referenced from:
      _vtenc_send_frame in libavcodec.a(videotoolboxenc.o)
  "_CVPixelBufferCreateWithPlanarBytes", referenced from:
      _vtenc_send_frame in libavcodec.a(videotoolboxenc.o)
....

VideoToolbox.framework

Undefined symbols for architecture x86_64:
  "_VTCompressionSessionCompleteFrames", referenced from:
      _vtenc_init in libavcodec.a(videotoolboxenc.o)
      _vtenc_frame in libavcodec.a(videotoolboxenc.o)
  "_VTCompressionSessionCreate", referenced from:
.....

libz.tbd

Undefined symbols for architecture x86_64:
  "_compress", referenced from:
      _encode_strip in libavcodec.a(tiffenc.o)
  "_compress2", referenced from:
      _flashsv2_encode_frame in libavcodec.a(flashsv2enc.o)
      _flashsv_encode_frame in libavcodec.a(flashsvenc.o)
  "_deflate", referenced from:
....

liblzma.tbd

Undefined symbols for architecture x86_64:
  "_lzma_code", referenced from:
      _decode_frame in libavcodec.a(tiff.o)
  "_lzma_end", referenced from:
      _decode_frame in libavcodec.a(tiff.o)
....

libbz2.tbd

Undefined symbols for architecture x86_64:
  "_BZ2_bzDecompress", referenced from:
      _matroska_decode_buffer in libavformat.a(matroskadec.o)
  "_BZ2_bzDecompressEnd", referenced from:
....

libiconv.tbd

Undefined symbols for architecture x86_64:
  "_iconv", referenced from:
      _avcodec_decode_subtitle2 in libavcodec.a(utils.o)
  "_iconv_close", referenced from:
      _avcodec_open2 in libavcodec.a(utils.o)
....

libz.tbd

Undefined symbols for architecture x86_64:
  "_compress", referenced from:
      _encode_strip in libavcodec.a(tiffenc.o)
  "_compress2", referenced from:
      _flashsv2_encode_frame in libavcodec.a(flashsv2enc.o)
      _flashsv_encode_frame in libavcodec.a(flashsvenc.o)
  "_deflate", referenced from:
.....

6、"stack_not_16_byte_aligned_error"错误
此错误为mac osX 10.15.4 (19E266)和Version 11.4 (11E146)编译环境下新产生问题。在编译CFlags=添加 "-fno-stack-check" 即可,如下:

# fixbug:mac osX 10.15.4 (19E266)和Version 11.4 (11E146)编译时产生错误"stack_not_16_byte_aligned_error"
# 添加 "-fno-stack-check" 即可
FF_EXTRA_CFLAGS="-fno-stack-check"

源代码地址

编译脚本地址:
https://github.com/nldzsz/ffmpeg-build-scripts.git
ffmpeg地址:
https://github.com/FFmpeg/FFmpeg.git
http://ffmpeg.org/releases/ffmpeg-4.2.tar.bz2
x264地址:
https://git.videolan.org/git/x264.git
http://download.videolan.org/x264/snapshots/x264-snapshot-20190809-2245-stable.tar.bz2(备注:该地址每天都会按照日期20190809增加一份拷贝,其它不变)
lame地址:
https://sourceforge.net/projects/lame/files/lame/3.100/lame-3.100.tar.gz
https://jaist.dl.sourceforge.net/project/lame/lame/3.100/lame-3.100.tar.gz
fdk-aac地址:
https://github.com/mstorsjo/fdk-aac
https://sourceforge.net/p/opencore-amr/fdk-aac/ci/v2.0.0/tarball
https://jaist.dl.sourceforge.net/project/opencore-amr/fdk-aac/fdk-aac-2.0.0.tar.gz

参考文章

1、[FFmpeg零基础(2)-FFmpeg的编译]https://juejin.im/post/5af3bef5f265da0b9d783ae3
2、xrun命令介绍
3、https://github.com/kewlbear
4、ijkplayer
5、gas-preprocessor
6、https://github.com/kewlbear/FFmpeg-iOS-build-script

上一篇下一篇

猜你喜欢

热点阅读