0. 编译环境
- Mac OS X 10.15.1
- Xcode 11.2.1 (11B53)
1. 安装 homebrew, git, yasm工具
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew install git
brew install yasm
2. 从github上下载ijkplayer源码
# 进入到自己保存ijkplayer源代码位置路径
cd /Users/system/Workspace/Github
# 下载ijkplayer源代码
git clone https://github.com/Bilibili/ijkplayer.git ijkplayer-ios
# 进入源码目录
cd ijkplayer-ios
# 切换分支(目前最新版本为k0.8.8,可以在ijkplayer-ios根目录下找到NEWS.md文件可以查看最新版本号)
git checkout -B latest k0.8.8
3. 配置编解码器格式
默认为最少支持, 如果足够你使用, 可跳过:
- module-default.sh 更多的编解码器/格式
- module-lite-hevc.sh 较少的编解码器/格式(包括hevc)
- module-lite.sh 较少的编解码器/格式(默认情况)
# 进入 config目录
cd config
# 删除当前的 module.sh 文件
rm module.sh
# 可根据需要替换为`module-default.sh`, `module-lite-hevc.sh`, `module-lite.sh`
# 创建软链接 module.sh 指向 module-lite-hevc.sh
ln -s module-lite.sh module.sh
cd ..
cd ios
# 清空历史编译记录(如果有编译过)
sh compile-ffmpeg.sh clean
4. 获取 ffmpeg 并初始化
此过程需要从网上拉取ffmpeg,需要花些时间
cd ..
./init-ios.sh
5. 获取 openssl 并初始化(增加HTTPS支持)
编译完openssl后会生成支持 https 的静态文件 libcrypto.a 和 libssl.a,如不需要支持HTTPS可跳过i此步骤:
./init-ios-openssl.sh
cd ios
# 在模块文件中添加一行配置 以启用 openssl 组件
# 模块文件:
# module-default.sh 更多的编解码器/格式
# module-lite-hevc.sh 较少的编解码器/格式(包括hevc)
# module-lite.sh 较少的编解码器/格式(默认情况)
echo 'export COMMON_FF_CFG_FLAGS="$COMMON_FF_CFG_FLAGS --enable-openssl"' >> ../config/module.sh
./compile-ffmpeg.sh clean
6. 编译
# 如果下一步提示错误`xcrun: error: SDK "iphoneos" cannot be located`, 请执行`sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer/`, 再重新执行下一步
# 编译openssl, 如不需要https可跳过
./compile-openssl.sh all
# 编译ffmpeg
./compile-ffmpeg.sh all
如果编译ffmpeg提示如下错误:
./libavutil/arm/asm.S:50:9: error: unknown directive
.arch armv7-a
^
make: *** [libavcodec/arm/aacpsdsp_neon.o] Error 1
最新的 Xcode 环境中iOS SDK最低支持从iOS 8.0开始了,弱化了对 32 位iOS系统的支持,解决方法:
在 compile-ffmpeg.sh
中删除 armv7
, 修改如:
FF_ALL_ARCHS_IOS8_SDK="arm64 i386 x86_64"
再重新执行出现错误的命令: ./compile-ffmpeg.sh all
7. 打开 IJKMediaDemo 项目
open IJKMediaDemo/IJKMediaDemo.xcodeproj
也可以手动用 Xcode 打开 ios 目录下的 IJKMediaDemo 项目.
command + B
Build一下发现报出以下错误:
Ld /Users/system/Library/Developer/Xcode/DerivedData/IJKMediaDemo-bnncwcpyzeijjachazvkcxsjbdll/Build/Products/Debug-iphoneos/IJKMediaDemo.app/IJKMediaDemo normal arm64 (in target 'IJKMediaDemo' from project 'IJKMediaDemo')
cd /Users/system/Workspace/Github/ijkplayer-ios/ios/IJKMediaDemo
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -target arm64-apple-ios8.0 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.2.sdk -L/Users/system/Library/Developer/Xcode/DerivedData/IJKMediaDemo-bnncwcpyzeijjachazvkcxsjbdll/Build/Products/Debug-iphoneos -F/Users/system/Library/Developer/Xcode/DerivedData/IJKMediaDemo-bnncwcpyzeijjachazvkcxsjbdll/Build/Products/Debug-iphoneos -filelist /Users/system/Library/Developer/Xcode/DerivedData/IJKMediaDemo-bnncwcpyzeijjachazvkcxsjbdll/Build/Intermediates.noindex/IJKMediaDemo.build/Debug-iphoneos/IJKMediaDemo.build/Objects-normal/arm64/IJKMediaDemo.LinkFileList -dead_strip -Xlinker -object_path_lto -Xlinker /Users/system/Library/Developer/Xcode/DerivedData/IJKMediaDemo-bnncwcpyzeijjachazvkcxsjbdll/Build/Intermediates.noindex/IJKMediaDemo.build/Debug-iphoneos/IJKMediaDemo.build/Objects-normal/arm64/IJKMediaDemo_lto.o -Xlinker -export_dynamic -Xlinker -no_deduplicate -fembed-bitcode-marker -fobjc-arc -fobjc-link-runtime -lstdc++ -lbz2 -lz -framework AudioToolbox -framework AVFoundation -framework CoreGraphics -framework CoreMedia -framework CoreVideo /Users/system/Library/Developer/Xcode/DerivedData/IJKMediaDemo-bnncwcpyzeijjachazvkcxsjbdll/Build/Products/Debug-iphoneos/IJKMediaFramework.framework/IJKMediaFramework -framework MediaPlayer -framework MobileCoreServices -framework OpenGLES -framework QuartzCore -framework UIKit -framework VideoToolbox -Xlinker -dependency_info -Xlinker /Users/system/Library/Developer/Xcode/DerivedData/IJKMediaDemo-bnncwcpyzeijjachazvkcxsjbdll/Build/Intermediates.noindex/IJKMediaDemo.build/Debug-iphoneos/IJKMediaDemo.build/Objects-normal/arm64/IJKMediaDemo_dependency_info.dat -o /Users/system/Library/Developer/Xcode/DerivedData/IJKMediaDemo-bnncwcpyzeijjachazvkcxsjbdll/Build/Products/Debug-iphoneos/IJKMediaDemo.app/IJKMediaDemo
Undefined symbols for architecture arm64:
"_ERR_error_string", referenced from:
_OUTLINED_FUNCTION_2 in IJKMediaFramework(tls_openssl.o)
"_SSL_shutdown", referenced from:
_tls_close in IJKMediaFramework(tls_openssl.o)
"_SSL_write", referenced from:
_tls_write in IJKMediaFramework(tls_openssl.o)
"_CRYPTO_get_locking_callback", referenced from:
_ff_openssl_init in IJKMediaFramework(tls_openssl.o)
_ff_openssl_deinit in IJKMediaFramework(tls_openssl.o)
"_SSL_read", referenced from:
_tls_read in IJKMediaFramework(tls_openssl.o)
"_SSL_CTX_free", referenced from:
_tls_close in IJKMediaFramework(tls_openssl.o)
"_SSL_CTX_use_certificate_chain_file", referenced from:
_tls_open in IJKMediaFramework(tls_openssl.o)
"_SSL_CTX_ctrl", referenced from:
_tls_open in IJKMediaFramework(tls_openssl.o)
"_SSL_CTX_use_PrivateKey_file", referenced from:
_tls_open in IJKMediaFramework(tls_openssl.o)
"_BIO_clear_flags", referenced from:
_url_bio_ctrl in IJKMediaFramework(tls_openssl.o)
_OUTLINED_FUNCTION_5 in IJKMediaFramework(tls_openssl.o)
"_SSLv23_server_method", referenced from:
_tls_open in IJKMediaFramework(tls_openssl.o)
"_SSL_CTX_set_verify", referenced from:
_tls_open in IJKMediaFramework(tls_openssl.o)
"_SSL_new", referenced from:
_tls_open in IJKMediaFramework(tls_openssl.o)
"_SSL_accept", referenced from:
_tls_open in IJKMediaFramework(tls_openssl.o)
"_SSL_set_bio", referenced from:
_tls_open in IJKMediaFramework(tls_openssl.o)
"_SSLv23_client_method", referenced from:
_tls_open in IJKMediaFramework(tls_openssl.o)
"_SSL_library_init", referenced from:
_ff_openssl_init in IJKMediaFramework(tls_openssl.o)
"_CRYPTO_num_locks", referenced from:
_ff_openssl_init in IJKMediaFramework(tls_openssl.o)
_ff_openssl_deinit in IJKMediaFramework(tls_openssl.o)
"_ERR_get_error", referenced from:
_tls_open in IJKMediaFramework(tls_openssl.o)
_print_tls_error in IJKMediaFramework(tls_openssl.o)
"_SSL_CTX_load_verify_locations", referenced from:
_tls_open in IJKMediaFramework(tls_openssl.o)
"_BIO_new", referenced from:
_tls_open in IJKMediaFramework(tls_openssl.o)
"_SSL_connect", referenced from:
_tls_open in IJKMediaFramework(tls_openssl.o)
"_SSL_free", referenced from:
_tls_close in IJKMediaFramework(tls_openssl.o)
"_SSL_CTX_new", referenced from:
_tls_open in IJKMediaFramework(tls_openssl.o)
"_SSL_load_error_strings", referenced from:
_ff_openssl_init in IJKMediaFramework(tls_openssl.o)
"_SSL_ctrl", referenced from:
_tls_open in IJKMediaFramework(tls_openssl.o)
"_CRYPTO_set_locking_callback", referenced from:
_ff_openssl_init in IJKMediaFramework(tls_openssl.o)
_ff_openssl_deinit in IJKMediaFramework(tls_openssl.o)
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Showing Recent Messages:
Undefined symbol: _ERR_error_string
Undefined symbol: _SSL_shutdown
Undefined symbol: _SSL_write
Undefined symbol: _CRYPTO_get_locking_callback
Undefined symbol: _SSL_read
Undefined symbol: _SSL_CTX_free
Undefined symbol: _SSL_CTX_use_certificate_chain_file
...
出错原因是因为既然添加了https的支持,需要把依赖的openssl库给关联进来。解决办法:
IJKMediaDemo->IJKMediaPlayer.xcodeproj->Classes->IJKFFMoviePlayerController->ffmpeg->lib
按照上面的可以看到该文件夹,然后鼠标右击delete
-> Remove References
.
接下来,重新把ffmpeg编译的库静态.a文件和include中的头文件拖进来,找到ijkplayer-ios存放的目录,我这里是:
/Users/system/Workspace/Github/ijkplayer-ios
然后进入到ijkplayer-ios
下的ios/build/universal
,可以看到include
和lib
两个文件夹,然后把他们拖到IJKMediaDemo刚才的位置中:
IJKMediaDemo->IJKMediaPlayer.xcodeproj->Classes->IJKFFMoviePlayerController->ffmpeg
Destination: Copy items if needed
Added folders: Create groups
Add to targets: IJKMediaFramework
8. 测试地址
http://static.tripbe.com/videofiles/20121214/9533522808.f4v.mp4
http://img.ksbbs.com/asset/Mon_1703/05cacb4e02f9d9e.mp4
https://media.w3.org/2010/05/sintel/trailer.mp4
https://www.sample-videos.com/video123/mp4/720/big_buck_bunny_720p_20mb.mp4
完结