Ron*_*Ron 30 iphone ffmpeg objective-c
我正在使用AVCaptureSession捕捉视频并从iPhone相机获取实时帧但是如何通过多路复用框架和声音将其发送到服务器以及如何使用ffmpeg来完成此任务,如果任何人有关于ffmpeg的任何教程或任何示例请在这里分享.
小智 24
我这样做的方法是实现一个AVCaptureSession,它有一个带有回调的委托,该回调在每一帧上运行.该回调通过网络将每个帧发送到服务器,该服务器具有接收它的自定义设置.
这是流程:
这里是一些代码:
// 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];
Run Code Online (Sandbox Code Playgroud)
然后输出设备的委托(这里,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 );
}
Run Code Online (Sandbox Code Playgroud)
发送原始帧或单个图像对您来说永远不会很好(因为数据量和帧数).您也无法通过电话合理地提供任何服务(WWAN网络具有各种防火墙).您需要对视频进行编码,然后将其流式传输到服务器,最有可能采用标准流式传输格式(RTSP,RTMP).iPhone上有一个H.264编码器芯片> = 3GS.问题是它不是面向流的.也就是说,它输出最后解析视频所需的元数据.这为您提供了一些选择.
1)获取原始数据并使用FFmpeg在手机上进行编码(将使用大量的CPU和电池).
2)为H.264/AAC输出编写自己的解析器(非常难).
3)以块的形式记录和处理(将增加等于块长度的延迟,并在启动和停止会话时在每个块之间减少大约1/4秒的视频).