【问题标题】:How to get real time video stream from iphone camera and send it to server?如何从 iPhone 摄像头获取实时视频流并将其发送到服务器?
【发布时间】:2012-08-27 21:00:24
【问题描述】:

我正在使用AVCaptureSession 捕获视频并从 iPhone 相机获取实时帧,但是如何将其发送到具有帧和声音多路复用的服务器以及如何使用 ffmpeg 完成此任务,如果有人有任何教程关于 ffmpeg 或任何示例,请在此处分享。

【问题讨论】:

  • 我假设您已经在录制 MOV/MP4“块”文件。您可以使用我写的旧项目ffstream(由某人分叉)来流式传输这些内容。请记住,您必须保持单调递增的时间。换句话说,每个 MOV/MP4 的开始时间必须相同。这样所有 1+N 文件开头都会有“空白”空间,ffmpeg 中 mov.c 生成的时间戳都是正确的。您可以使用 RTSP/RTP over UDP/TCP 或使用 librtmp 插件为 ffmpeg 进行流式传输。
  • @SteveMcFarlin 你能帮我如何将流从本地发送到服务器,这意味着如何将流作为块发送。

标签: iphone objective-c ffmpeg


【解决方案1】:

我这样做的方式是实现一个 AVCaptureSession,它有一个委托和一个在每一帧上运行的回调。该回调通过网络将每个帧发送到服务器,服务器有一个自定义设置来接收它。

流程如下:

http://developer.apple.com/library/ios/#documentation/AudioVideo/Conceptual/AVFoundationPG/Articles/03_MediaCapture.html#//apple_ref/doc/uid/TP40010188-CH5-SW2

这里有一些代码:

// make input device

NSError *deviceError;

AVCaptureDevice *cameraDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

AVCaptureDeviceInput *inputDevice = [AVCaptureDeviceInput deviceInputWithDevice:cameraDevice error:&deviceError];

// make output device

AVCaptureVideoDataOutput *outputDevice = [[AVCaptureVideoDataOutput alloc] init];

[outputDevice setSampleBufferDelegate:self queue:dispatch_get_main_queue()];

// initialize capture session

AVCaptureSession *captureSession = [[[AVCaptureSession alloc] init] autorelease];

[captureSession addInput:inputDevice];

[captureSession addOutput:outputDevice];

// make preview layer and add so that camera's view is displayed on screen

AVCaptureVideoPreviewLayer *previewLayer = [AVCaptureVideoPreviewLayer    layerWithSession:captureSession];
previewLayer.frame = view.bounds;
[view.layer addSublayer:previewLayer];

// go!

[captureSession startRunning];

那么输出设备的委托(这里是self)必须实现回调:

-(void) captureOutput:(AVCaptureOutput*)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection*)connection

{
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer( sampleBuffer );

CGSize imageSize = CVImageBufferGetEncodedSize( imageBuffer );

// also in the 'mediaSpecific' dict of the sampleBuffer

   NSLog( @"frame captured at %.fx%.f", imageSize.width, imageSize.height );

    }

发送原始帧或单个图像对您来说永远不够好(因为数据量和帧数)。您也不能通过电话合理地提供任何服务(WWAN 网络有各种防火墙)。您需要对视频进行编码,并将其流式传输到服务器,最有可能通过标准流格式(RTSP、RTMP)。 iPhone >= 3GS 上有一个 H.264 编码器芯片。问题是它不是面向流的。也就是说,它输出最后解析视频所需的元数据。这给您留下了一些选择。

1) 获取原始数据并在手机上使用 FFmpeg 进行编码(将消耗大量 CPU 和电池)。

2) 为 H.264/AAC 输出编写自己的解析器(非常难)。

3) 以块的形式记录和处理(将增加等于块长度的延迟,并在您开始和停止会话时在每个块之间减少大约 1/4 秒的视频)。

【讨论】:

  • ,到目前为止我已经完成了,感谢您的回复,我的问题是之后我如何将音频和视频帧从 iphone 发送到服务器。
  • 我知道这是旧的,但我被困在这个主题的服务器端。您是如何配置服务器来处理图像帧流的?
  • developer.apple.com 找不到页面。
【解决方案2】:

故事有长有短。

这是简短的: 去看看https://github.com/OpenWatch/H264-RTSP-Server-iOS

这是一个起点。

你可以得到它,看看他是如何提取框架的。这是一个小而简单的项目。

然后您可以查看具有特定功能“encodedFrame”的kickflip,它被回调一次,并且编码帧从此时到达您可以用它做您想做的事情,通过websocket发送。有一堆非常硬的代码可用于读取 mpeg 原子

【讨论】:

    【解决方案3】:

    Look here , and here

    尝试使用 AV Foundation 框架捕获视频。使用 HTTP 流将其上传到您的服务器。

    还可以查看下面的另一个堆栈溢出帖子的堆栈

    (The post below was found at this link here)

    你很可能已经知道....

     1) How to get compressed frames and audio from iPhone's camera?
    

    你不能这样做。 AVFoundation API 阻止了这种情况 每个角度。我什至尝试过命名管道和其他一些鬼鬼祟祟的 unix foo。 没有这样的运气。您别无选择,只能将其写入文件。在你的 链接的帖子用户建议设置回调以传递编码 帧。据我所知,这对于 H.264 流是不可能的。 捕获委托将提供以特定像素编码的图像 格式。是 Movie Writers 和 AVAssetWriter 做的 编码。

     2) Encoding uncompressed frames with ffmpeg's API is fast enough for
     real-time streaming?
    

    是的。但是,您必须使用 libx264 才能进入 GPL 领域。这与应用商店不完全兼容。

    我建议使用 AVFoundation 和 AVAssetWriter 来提高效率 原因。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-28
      • 2013-02-25
      • 1970-01-01
      • 2016-10-23
      • 2014-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多