目前网络上的FFMPEG构建的帖子十分的多,集成到iOS项目中的例子也比比皆是,但是,一方面网络上FFMPEG的教程大多数都是基本库的构建,FFMPEG库可以使用很多的第三方库,这方面相关的内容几乎没有;另一方面,iOS使用FFMPEG大多数还是通过命令行使用,在FFMPEG的交流群里,大多还是纯FFMPEG开发,我们的问题too LOW,没人管。。。
FFMPEG安装和无组件构建。
网上相关的例子太多了,我就不一一列举了,有需要的同学直接搜索就好了,我这里主要说下arm64框架下的有组件的静态库的构建。
freetype 、Libpng构建
这里做了一个加文字水印的功能,网上相关命令有很多,只有很少才告诉了你需要用freetype,但是freetype怎么打包却没人说。
水印命令:
ffmpeg -i input.MP4 -s "540x720" -vf "drawtext=fontfile=FZHTJW.TTF:text=技术是第一生产力:fontsize=28:fontcolor=white@0.5:shadowy=2[text];[in][text]overlay=main_w-overlay_w-10:main_h-overlay_h-10[out]" -y output.mp4
//使用字体库FZHTJW.TTF添加水印,文字为“技术是第一生产力”,位置是左下角
水印功能使用drawtext 滤镜,drawtext需要Freetype支持,freetype打包的时候,需要libpng库的支持
libpng
下载压缩包地址:libpng 地址
libpng 构建相对简单,在libpng-1.6.34/scripts 目录下存在一大批的makeFile开头的文件。我们可以找到makefile.darwin
文件,修改该文件,并复制到根目录,命名为Makefile
,makefile中改动如下,多余代码已注释一部分,各位根据情况各自去替换。
# where "make install" puts libpng.a, libpng16.dylib, png.h, pngconf.h,
# and pnglibconf.h
# 设置库的最终输出路径,这里我使用了绝对路径,各位自己改吧
prefix=/Users/kyang/Downloads/libpng-1.6.34/atmp/arm64
# Where the zlib library and include files are located
#这里设置依赖库的路径和头文件路径,个人使用的XCode的SDK版本是13.2,各位根据自己的情况,修改对应的SDK版本。
ZLIBLIB=/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.2.sdk/usr/lib
ZLIBINC=/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.2.sdk/usr/include
# 下面这两个是编译模拟器版本的路径,就是x86_64框架
# ZLIBLIB=/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulatorS13.2.sdk/usr/lib
# ZLIBINC=/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator13.2.sdk/usr/include
# Utilities:
# 这里修改了默认的编译器,使用的是真机的编译器。
CC=xcrun -sdk iphoneos clang
# CC=clang
# 这里设置了库的版本是arm64,如果修改其他框架,此处俩设置也改掉
CPPFLAGS=-arch arm64 -I$(ZLIBINC)
# CFLAGS=-W -Wall -O3 -funroll-loops
$(LIBSOMAJ): $(OBJSDLL)
$(CC) -arch arm64 -dynamiclib \
-install_name $(LIBPATH)/$(LIBSOMAJ) \
-current_version 16 -compatibility_version 16 \
-o $(LIBSOMAJ) \
$(OBJSDLL) -L$(ZLIBLIB) -lz
此时在终端执行make install,一切顺利的话,输出:
xcrun -sdk iphoneos clang -arch arm64 -dynamiclib \
-install_name /Users/kyang/Downloads/libpng-1.6.34/atmp/arm64/lib/libpng16.16.dylib \
-current_version 16 -compatibility_version 16 \
-o libpng16.16.dylib \
png.pic.o pngset.pic.o pngget.pic.o pngrutil.pic.o pngtrans.pic.o pngwutil.pic.o pngread.pic.o pngrio.pic.o pngwio.pic.o pngwrite.pic.o pngrtran.pic.o pngwtran.pic.o pngmem.pic.o pngerror.pic.o pngpread.pic.o -L/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.2.sdk/usr/lib -lz
cat scripts/libpng.pc.in | sed -e s!@prefix@!/Users/kyang/Downloads/libpng-1.6.34/atmp/arm64! \
-e s!@exec_prefix@!/Users/kyang/Downloads/libpng-1.6.34/atmp/arm64! \
-e s!@libdir@!/Users/kyang/Downloads/libpng-1.6.34/atmp/arm64/lib! \
-e s!@includedir@!/Users/kyang/Downloads/libpng-1.6.34/atmp/arm64/include! \
-e s!-lpng16!-lpng16\ -lz! > libpng.pc
cp libpng16.16.dylib /Users/kyang/Downloads/libpng-1.6.34/atmp/arm64/lib
chmod 755 /Users/kyang/Downloads/libpng-1.6.34/atmp/arm64/lib/libpng16.16.dylib
(cd /Users/kyang/Downloads/libpng-1.6.34/atmp/arm64/lib; \
ln -sf libpng16.16..dylib libpng16.dylib; \
ln -sf libpng16.dylib libpng.dylib)
cp libpng.pc /Users/kyang/Downloads/libpng-1.6.34/atmp/arm64/lib/pkgconfig/libpng16.pc
chmod 644 /Users/kyang/Downloads/libpng-1.6.34/atmp/arm64/lib/pkgconfig/libpng16.pc
(cd /Users/kyang/Downloads/libpng-1.6.34/atmp/arm64/lib/pkgconfig; ln -sf libpng16.pc libpng.pc)
cp libpng.3 /Users/kyang/Downloads/libpng-1.6.34/atmp/arm64/man/man3
cp libpngpf.3 /Users/kyang/Downloads/libpng-1.6.34/atmp/arm64/man/man3
cp png.5 /Users/kyang/Downloads/libpng-1.6.34/atmp/arm64/man/man5
( cat scripts/libpng-config-head.in; \
echo prefix=\"/Users/kyang/Downloads/libpng-1.6.34/atmp/arm64\"; \
echo I_opts=\"-I/Users/kyang/Downloads/libpng-1.6.34/atmp/arm64/include/libpng16\"; \
echo L_opts=\"-L/Users/kyang/Downloads/libpng-1.6.34/atmp/arm64/lib\"; \
echo libs=\"-lpng16 -lz\"; \
cat scripts/libpng-config-body.in ) > libpng-config
chmod +x libpng-config
cp libpng-config /Users/kyang/Downloads/libpng-1.6.34/atmp/arm64/bin/libpng16-config
chmod 755 /Users/kyang/Downloads/libpng-1.6.34/atmp/arm64/bin/libpng16-config
(cd /Users/kyang/Downloads/libpng-1.6.34/atmp/arm64/bin; ln -sf libpng16-config libpng-config)
image.png
输出库支持的框架信息:
localhost:lib kyang$ lipo libpng16.a -info
Non-fat file: libpng16.a is architecture: arm64
freetype
freetype 编译的shell如下:
export SDKPATH="/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.2.sdk"
export PNGLIB="/Users/kyang/Downloads/libpng-1.6.34/atmp/arm64/lib"
export PNGINCLUDE="/Users/kyang/Downloads/libpng-1.6.34/atmp/arm64/include"
export CC=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc
export CFLAGS="-arch arm64 -I${PNGINCLUDE} -pipe -std=c99 -Wno-invalid-offsetof -Wno-trigraphs -fpascal-strings -O2 -Wreturn-type -Wunused-variable -fmessage-length=0 -fvisibility=hidden -miphoneos-version-min=8.0 -I/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.2.sdk/usr/include -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.2.sdk"
export AR=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ar
export LDFLAGS="-arch arm64 -L${PNGLIB} -isysroot $SDKPATH -miphoneos-version-min=8.0"
./configure --prefix=/Users/kyang/Downloads/freetype-2.10.2/atmp --host=arm-apple-darwin --without-harfbuzz
unset SDKPATH PNGLIB PNGINCLUDE CC CFLAGS AR LDFLAGS
上面shell中设置了libpng的路径-L${PNGLIB} 和 -I${PNGINCLUDE}
,当我们不设置该路径,去make,会提示如下错误
ld: warning: ignoring file /usr/local/Cellar/libpng/1.6.37/lib/libpng16.dylib, building for iOS-arm64 but attempting to link with file built for macOS-x86_64
Undefined symbols for architecture arm64:
"_png_create_info_struct", referenced from:
_Load_SBit_Png in sfnt.o
"_png_create_read_struct", referenced from:
_Load_SBit_Png in sfnt.o
"_png_destroy_read_struct", referenced from:
_Load_SBit_Png in sfnt.o
"_png_error", referenced from:
_read_data_from_FT_Stream in sfnt.o
.
.
.
"_png_set_read_fn", referenced from:
_Load_SBit_Png in sfnt.o
"_png_set_read_user_transform_fn", referenced from:
_Load_SBit_Png in sfnt.o
"_png_set_strip_16", referenced from:
_Load_SBit_Png in sfnt.o
"_png_set_tRNS_to_alpha", referenced from:
_Load_SBit_Png in sfnt.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [/Users/kyang/Downloads/freetype-2.10.2/objs/libfreetype.la] Error 1
设置libpng路径之后成功make成功,输出如下:
。
。
。
mv builds/unix/freetype-config.tmp builds/unix/freetype-config
rm -f builds/unix/freetype2.pc builds/unix/freetype2.pc.tmp
sed -e 's|%REQUIRES_PRIVATE%|zlib, libpng|' \
-e 's|%LIBS_PRIVATE%|-lbz2|' \
-e 's|%build_libtool_libs%||' \
-e 's|%exec_prefix%|/Users/kyang/Downloads/freetype-2.10.2/atmp|' \
-e 's|%ft_version%|23.2.17|' \
-e 's|%includedir%|/Users/kyang/Downloads/freetype-2.10.2/atmp/include|' \
-e 's|%libdir%|/Users/kyang/Downloads/freetype-2.10.2/atmp/lib|' \
-e 's|%prefix%|/Users/kyang/Downloads/freetype-2.10.2/atmp|' \
/Users/kyang/Downloads/freetype-2.10.2/builds/unix/freetype2.in \
> builds/unix/freetype2.pc.tmp
chmod a-w builds/unix/freetype2.pc.tmp
mv builds/unix/freetype2.pc.tmp builds/unix/freetype2.pc
构建成功之后,库存放路径如下图所示:
image.png
FFMPEG
FFMPEG 构建使用build-ffmpeg.sh,
1.在CONFIGURE_FLAGS中设置开启libfreetype
2.设置编译环境是arm64
3.修改CFLAGS LDFLAGS设置freetype的路径
CONFIGURE_FLAGS="--enable-cross-compile --disable-debug --disable-programs \
--disable-doc --enable-pic\
--enable-libfreetype\
"
# 设置仅编译arm64
ARCHS="arm64"
# 大概在108行左右,已识别出是arm64环境,
PLATFORM="iPhoneOS"
CFLAGS="$CFLAGS -mios-version-min=$DEPLOYMENT_TARGET"
# -fembed-bitcode"
if [ "$ARCH" = "arm64" ]
then
EXPORT="GASPP_FIX_XCODE5=1"
echo "设置arm64下的 CFLAGS,设置freetype依赖的路径和zlib的依赖路径"
CFLAGS="$CFLAGS -I/Users/kyang/local/arm64/freetype/include/"
LDFLAGS="$LDFLAGS -L/Users/kyang/local/arm64/freetype/lib/ -lfreetype"
执行./build-ffmpeg.sh
,结果如下
集成FFMpeg到项目中
这部分有很多文章可看,需要注意的是,如果我们使用了单独编译的第三方库,则需要把这些库都导入到项目中进行使用,包括各自的静态库和头文件
网友评论