使用AVFoundation录制视频的输出

时间:2014-02-12 15:46:16

标签: ios iphone objective-c video avfoundation

我正在使用AVFoundation录制视频。我正在使用下面的代码。

  -(IBAction)record:(id)sender
    {


     AVCaptureSession *session = [[AVCaptureSession alloc] init];
     [session setSessionPreset:AVCaptureSessionPresetHigh];
      AVCaptureDevice *inputDevice = [AVCaptureDevice    defaultDeviceWithMediaType:AVMediaTypeVideo];

      NSError *error;

     AVCaptureDeviceInput *deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:inputDevice error:&error];
     if ( [session canAddInput:deviceInput] )
     [session addInput:deviceInput];
     AVCaptureVideoPreviewLayer *previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
    [previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];

    CALayer *rootLayer = [[self view] layer];
    [rootLayer setMasksToBounds:YES];
    [previewLayer setFrame:CGRectMake(-70, 0, rootLayer.bounds.size.height, rootLayer.bounds.size.height)];
    [rootLayer insertSublayer:previewLayer atIndex:0];

    [session startRunning];

 }

我可以在我的预览层上看到视频,我该如何将其发送到NSData,以便我可以在录制直播时即时将其发送到服务器。

1 个答案:

答案 0 :(得分:0)

您需要创建一个AVCaptureVideoDataOutput并将其连接到您的会话,然后实施

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

在此处sampleBuffer保存框架的数据

同样,我建议您查看RosyWriter示例应用程序,因为它有一个以像素为单位获取帧数据的示例