iOS之RTSP转MP4

2019-03-29  本文已影响0人  奥巴荣

本文链接:https://www.jianshu.com/p/fe74a8579c34

源码地址:https://gitee.com/obarong/FFmpeg4iOS

目的:实现iOS的rtsp转mp4

方案:

  1. ijkplayer:其实还是FFmpeg。
  2. FFmpeg:之前做过安卓的rtsp转mp4,比较熟悉,所以用这个方案。

环境:

1. FFmpeg

参考向晨宇的文章:

iOS编解码入门篇(FFMPEG环境搭建) | 向晨宇的技术博客
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

执行编译脚本,自动按顺序下载安装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

执行编译脚本

$ ./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对应架构的config.h到FFmpeg4iOS/FFmpeg4iOS/FFmpeg-iOS/include/config下。

参考ijkplayer用一个config.h管理几个架构,新建文件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转MP4

参考剑西的文章,虽然是安卓端,但方法都是通的。

编译Android下可执行命令的FFmpeg - 剑西 - CSDN博客
https://blog.csdn.net/mabeijianxi/article/details/72904694

复制fftools到工程的FFmpeg-iOS目录,不要ffplay、ffprobe、ffmpeg_qsv、Makefile。


目录结构

修改ffmpeg.c和ffmpeg.h的main函数,修改后是这样

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.copt_init_hw_device函数最后面加上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编码成功。音频自动转码成aac(LC),我没有集成fdk-aac,为什么可以转aac?

h264

测试RTSP转MP4成功


rtsp2mp4

(完)

附录

$ ./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

其他参考

ffmpeg4.1编译IOS版本及x264、fdk-aac - 简书
https://www.jianshu.com/p/e4f2eb6f3eb8

FFmpeg源代码结构图 - 解码 - 雷霄骅(leixiaohua1020)的专栏 - CSDN博客
https://blog.csdn.net/leixiaohua1020/article/details/44220151

iOS: FFmpeg的使用一 - 当天真遇到现实 - 博客园
https://www.cnblogs.com/XYQ-208910/p/5651166.html

iOS:集成ijkplayer视频直播 - 当天真遇到现实 - 博客园
http://www.cnblogs.com/XYQ-208910/p/5856815.html

IOS视频压缩 - kouriba - ITeye博客
https://kouriba.iteye.com/blog/1629610

iOS 关于文件操作 NSFileManager - 简书
https://www.jianshu.com/p/086ca6d2c5de

iOS进阶——数据处理之文件读写 - 简书
https://www.jianshu.com/p/fbb997eb032d

6.17 使用MediaPlayer框架播放影片 [iOS开发-Xcode教程] - 简书
https://www.jianshu.com/p/5464f8016909

通过iTunes共享文件到Document目录 - 简书
https://www.jianshu.com/p/de59aafe1947

上一篇下一篇

猜你喜欢

热点阅读