目的:實現(xiàn)iOS的rtsp轉(zhuǎn)mp4
方案:
ijkplayer:其實還是FFmpeg。- FFmpeg:之前做過安卓的rtsp轉(zhuǎn)mp4,比較熟悉,所以用這個方案。
環(huán)境:
- macOS 10.13.6
- yasm 1.3.0
- Xcode 10.1
- FFmpeg 4.1
- x264: x264-snapshot-20190325-2245-stable
- Target Arch: armv7 arm64
1. FFmpeg
參考向晨宇的文章:
iOS編解碼入門篇(FFMPEG環(huán)境搭建) | 向晨宇的技術(shù)博客
http://www.iosxxx.com/blog/2017-07-17-iOS%E7%BC%96%E8%A7%A3%E7%A0%81%E5%85%A5%E9%97%A8%E7%AF%87-FFMPEG%E7%8E%AF%E5%A2%83%E6%90%AD%E5%BB%BA.html
這里大概寫一下關鍵步驟。
下載ffmpeg編譯腳本:
kewlbear/FFmpeg-iOS-build-script: Shell scripts to build FFmpeg for iOS and tvOS
https://github.com/kewlbear/FFmpeg-iOS-build-script
執(zhí)行編譯腳本哈蝇,自動按順序下載安裝homebrew、yasm、gas-preprocessor味赃、ffmpeg源碼
$ ./build-ffmpeg.sh armv7 arm64
合并a文件
$ ./build-ffmpeg.sh lipo
合并后的a文件在FFmpeg-iOS/lib目錄下,頭文件在FFmpeg-iOS/include目錄虐拓。
2. x264
下載x264源碼
Index of /pub/videolan/x264/snapshots/
http://download.videolan.org/pub/videolan/x264/snapshots/
下載x264編譯腳本
kewlbear/x264-ios: Script to build x264 for iOS apps
https://github.com/kewlbear/x264-ios
執(zhí)行編譯腳本
$ ./build-x264.sh armv7 arm64
合并
$ ./build-x264.sh lipo
合并后的a文件和頭文件在x264-iOS目錄心俗。
3. FFmpeg+x264
將x264-iOS放到ffmpeg編譯腳本的目錄,改名為fat-x264蓉驹,反注釋build-ffmpeg.sh這行
X264=`pwd`/fat-x264
再編一次ffmpeg城榛。
4. 新建Xcode工程FFmpeg4iOS
復制FFmpeg-iOS和x264-iOS目錄到FFmpeg4iOS/FFmpeg4iOS下。
復制scratch對應架構(gòu)的config.h到FFmpeg4iOS/FFmpeg4iOS/FFmpeg-iOS/include/config下态兴。
參考ijkplayer用一個config.h管理幾個架構(gòu)狠持,新建文件FFmpeg4iOS/FFmpeg4iOS/FFmpeg-iOS/include/config/config.h
#if defined(__aarch64__)
# include "arm64/config.h"
#elif defined(__x86_64__)
# include "x86_64/config.h"
#elif defined(__arm__)
# if defined(__ARM_ARCH_7S__)
# include "armv7s/config.h"
# elif defined(__ARM_ARCH)
# if __ARM_ARCH == 7
# include "armv7/config.h"
# else
# error Unsupport ARM architecture
# endif
# else
# error Unsupport ARM architecture
# endif
#elif defined(__i386__)
# include "i386/config.h"
#else
# error Unsupport architecture
#endif
設置Header Search Paths:
$(PROJECT_DIR)/FFmpeg4iOS/FFmpeg-iOS/include
$(PROJECT_DIR)/FFmpeg4iOS/x264-iOS/include
Library Search Paths:
$(PROJECT_DIR)/FFmpeg4iOS/FFmpeg-iOS/lib
$(PROJECT_DIR)/FFmpeg4iOS/x264-iOS/lib
項目配置linked frameworks and libraries加上VideoToolbox.framework libz.tbd libbz2.tbd libiconv.tbd CoreMedia.framework AudioToolBox.framework。
補全缺少的十幾個頭文件(瞻润?)
添加測試代碼
av_register_all();
NSLog(@"version=%d\n", avcodec_version());
編譯運行喘垂,打印log
version=3810148
集成ffmpeg成功。
5. RTSP轉(zhuǎn)MP4
參考劍西的文章绍撞,雖然是安卓端正勒,但方法都是通的。
編譯Android下可執(zhí)行命令的FFmpeg - 劍西 - CSDN博客
https://blog.csdn.net/mabeijianxi/article/details/72904694
復制fftools到工程的FFmpeg-iOS目錄傻铣,不要ffplay章贞、ffprobe、ffmpeg_qsv非洲、Makefile鸭限。
修改ffmpeg.c和ffmpeg.h的main函數(shù),修改后是這樣
int jrRun(int argc, char **argv)
修改cmdutils.c
int exit_program(int ret)
{
if (program_exit)
program_exit(ret);
// exit(ret);
return ret;
}
修改cmdutils.h
int exit_program(int ret);
在ffmpeg.c两踏,jrRun的return前加上
nb_filtergraphs = 0;
progress_avio = NULL;
input_streams = NULL;
nb_input_streams = 0;
input_files = NULL;
nb_input_files = 0;
output_streams = NULL;
nb_output_streams = 0;
output_files = NULL;
nb_output_files = 0;
ffmpeg_opt.c
的opt_init_hw_device
函數(shù)最后面加上return 0;
打開文件共享
在 Info.plist 文件中添加 UIFileSharingEnabled 這個Key, 并設置該值為 YES 即可
把testfile.mpg復制到手機败京。
測試代碼
//獲取文件位置
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
// NSString *vidPath = @"rtsp://184.72.239.149/vod/mp4://BigBuckBunny_175k.mov";
NSString *vidPath = [NSString stringWithFormat:@"%@/testfile.mpg", docPath];
NSString *outPath = [NSString stringWithFormat:@"%@/h264.mp4", docPath];
NSString *cmd=[NSString stringWithFormat:@"ffmpeg -y -i %@ -c:v libx264 %@", vidPath, outPath];
NSArray *array = [cmd componentsSeparatedByString:@" "];
NSLog(@"array:%@",array);
int argc = [array count];
char *argv[argc];
for (int i = 0; i < argc; i++) {
argv[i] = [[array objectAtIndex:i] UTF8String];
}
jrRun(argc, argv);
測試h264編碼成功。音頻自動轉(zhuǎn)碼成aac(LC)缆瓣,我沒有集成fdk-aac喧枷,為什么可以轉(zhuǎn)aac?
測試RTSP轉(zhuǎn)MP4成功
(完)
附錄
- 新建項目簽名時弓坞,卡在signing隧甚,菊花轉(zhuǎn)。渡冻。要打開雙重驗證蘋果賬號戚扳。
- 編譯報錯
$ ./build-x264.sh
building arm64...
No working C compiler found.
$ ./build-ffmpeg.sh
FFmpeg source not found. Trying to download...
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 10.1M 100 10.1M 0 0 876k 0 0:00:11 0:00:11 --:--:-- 933k
building arm64...
xcrun -sdk iphoneos clang is unable to create an executable file.
C compiler test failed.
If you think configure made a mistake, make sure you are using the latest
version from Git. If the latest version fails, report the problem to the
ffmpeg-user@ffmpeg.org mailing list or IRC #ffmpeg on irc.freenode.net.
Include the log file "ffbuild/config.log" produced by configure as this will help
solve the problem.
看config.log
WARNING: pkg-config not found, library detection may fail.
mktemp -u XXXXXX
lGTJ1g
test_ld cc
test_cc
BEGIN /var/folders/n4/3_0qsdm172v_6vr7gdnxz3zc0000gn/T/ffconf.9TYNjHRZ/test.c
1 int main(void){ return 0; }
END /var/folders/n4/3_0qsdm172v_6vr7gdnxz3zc0000gn/T/ffconf.9TYNjHRZ/test.c
xcrun -sdk iphoneos clang -arch arm64 -mios-version-min=8.0 -fembed-bitcode -c -o /var/folders/n4/3_0qsdm172v_6vr7gdnxz3zc0000gn/T/ffconf.9TYNjHRZ/test.o /var/folders/n4/3_0qsdm172v_6vr7gdnxz3zc0000gn/T/ffconf.9TYNjHRZ/test.c
xcrun: error: SDK "iphoneos" cannot be located
xcrun -sdk iphoneos clang -arch arm64 -mios-version-min=8.0 -fembed-bitcode -o /var/folders/n4/3_0qsdm172v_6vr7gdnxz3zc0000gn/T/ffconf.9TYNjHRZ/test /var/folders/n4/3_0qsdm172v_6vr7gdnxz3zc0000gn/T/ffconf.9TYNjHRZ/test.o
xcrun: error: SDK "iphoneos" cannot be located
ld: warning: ignoring file /usr/lib/libSystem.dylib, missing required architecture arm64 in file /usr/lib/libSystem.dylib (2 slices)
ld: dynamic main executables must link with libSystem.dylib for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
C compiler test failed.
解:
參考這里解決
C compiler test failed. · Issue #130 · kewlbear/FFmpeg-iOS-build-script
https://github.com/kewlbear/FFmpeg-iOS-build-script/issues/130
關鍵是這句
$ sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer/
- 編譯報錯:
Undefined symbols for architecture arm64:
"_VTDecompressionSessionDecodeFrame", referenced from:
_videotoolbox_common_end_frame in libavcodec.a(videotoolbox.o)
...
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
解:
項目配置linked frameworks and libraries加上VideoToolbox.framework libz.tbd libbz2.tbd libiconv.tbd CoreMedia.framework AudioToolBox.framework
- 編譯報錯
The file “FFmpeg4iOS.app” couldn’t be opened because you don’t have permission to view it.
解:
不知道什么問題。clean一下就好了族吻。
- 編譯報錯
Undefined symbols for architecture arm64:
"_videotoolbox_pixfmt", referenced from:
_options in ffmpeg_opt.o
解:
補上ffmpeg_videotoolbox.c
- 報錯
Undefined symbols for architecture arm64:
"_pp_get_context", referenced from:
_pp_config_props in libavfilter.a(vf_pp.o)
解:添加libpostproc.a
TODO
- [ ] 加上多線程
- [ ] 回調(diào)
- [ ] 封裝framework
其他參考
ffmpeg4.1編譯IOS版本及x264帽借、fdk-aac - 簡書
http://www.reibang.com/p/e4f2eb6f3eb8
FFmpeg源代碼結(jié)構(gòu)圖 - 解碼 - 雷霄驊(leixiaohua1020)的專欄 - CSDN博客
https://blog.csdn.net/leixiaohua1020/article/details/44220151
iOS: FFmpeg的使用一 - 當天真遇到現(xiàn)實 - 博客園
https://www.cnblogs.com/XYQ-208910/p/5651166.html
iOS:集成ijkplayer視頻直播 - 當天真遇到現(xiàn)實 - 博客園
http://www.cnblogs.com/XYQ-208910/p/5856815.html
IOS視頻壓縮 - kouriba - ITeye博客
https://kouriba.iteye.com/blog/1629610
iOS 關于文件操作 NSFileManager - 簡書
http://www.reibang.com/p/086ca6d2c5de
iOS進階——數(shù)據(jù)處理之文件讀寫 - 簡書
http://www.reibang.com/p/fbb997eb032d
6.17 使用MediaPlayer框架播放影片 [iOS開發(fā)-Xcode教程] - 簡書
http://www.reibang.com/p/5464f8016909
通過iTunes共享文件到Document目錄 - 簡書
http://www.reibang.com/p/de59aafe1947