我使用AVCaptureSession来捕获视频,并从iPhone摄像头获取实时帧,但我如何才能将其发送到多路复用的帧和声音的服务器,以及如何使用ffmpeg来完成这项任务,如果有人有任何关于ffmpeg的教程或任何示例,请在这里分享。
发布于 2012-09-13 19:28:15
我这样做的方式是实现一个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秒的视频丢失)。
发布于 2012-09-17 07:48:38
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应用编程接口已经从各个角度阻止了这一点。我甚至尝试了命名管道和其他一些偷偷摸摸的unix foo。没有这样的运气。您别无选择,只能将其写入文件。在你的链接帖子中,用户建议设置回调来传递编码帧。据我所知,这对于H.264流是不可能的。捕获代理将提供以特定像素格式编码的图像。编码是由电影编剧和AVAssetWriter完成的。
2) Encoding uncompressed frames with ffmpeg's API is fast enough for
real-time streaming?:是的,是的。然而,您将不得不使用libx264,这将使您进入通用公共许可证的领域。这与应用商店并不完全兼容。
出于效率的原因,我建议使用AVFoundation和AVAssetWriter。
发布于 2015-11-01 17:35:31
有一个长篇和一个短篇的故事。
这是一个简短的例子:去看看https://github.com/OpenWatch/H264-RTSP-Server-iOS
这是一个起点。
你可以得到它,看看他是如何提取帧的。这是一个小而简单的项目。
然后你可以看看kickflip,它有一个特定的函数"encodedFrame“,它被调用一次,编码的帧从这一点到达,你可以用它做你想做的事情,通过websocket发送。有一堆非常难的代码可以读取mpeg原子。
https://stackoverflow.com/questions/12242513
复制相似问题