【问题标题】:Record video with AVCaptureVideoDataOutput使用 AVCaptureVideoDataOutput 录制视频
【发布时间】:2023-03-16 14:02:01
【问题描述】:

我不确定我应该在方法中添加什么

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

为了将帧写入视频。任何人都可以与我分享他们的这种方法的代码主体,结果是将帧记录到电影中吗?

我以为我的assetWriter 和videoInput 设置正确,但我得到的只是一帧重复使用的电影。

【问题讨论】:

  • 查看 WWDC 2010 中的 AVCamDemo 代码。它有一个示例将向您展示该做什么。
  • 如果您不需要实际检查/修改/显示帧,您可以将 AVCaptureVideoDataOutput 和 AVAssetWriter 替换为更简单的 AVCaptureMovieFileOutput。

标签: ios avfoundation avassetwriter


【解决方案1】:

查看 Apple 示例代码 Rosy Writer。这是您正在寻找的一个很好的例子。

【讨论】:

    【解决方案2】:

    您可以使用此方法成功地同时录制视频和抓帧:

    AVCaptureSession *captureSession = [AVCaptureSession new];
    AVCaptureDevice *captureDevice = [AVCaptureDevice new];
    AVCaptureDeviceInput *deviceInput = [AVCaptureDeviceInput new];
    AVCaptureVideoDataOutput *output = [AVCaptureVideoDataOutput new];
    
    
    NSDictionary *outputSettings = [NSDictionary dictionaryWithObjectsAndKeys:
    [NSNumber numberWithInt:640], AVVideoWidthKey, [NSNumber numberWithInt:480], AVVideoHeightKey, AVVideoCodecH264, AVVideoCodecKey, nil];
    AVAssetWriterInput *assetWriterInput = [AVAssetWriterInput  assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:outputSettings];
    
    
    / AVCaptureVideDataOutput /
    AVAssetWriterInputPixelBufferAdaptor *pixelBufferAdaptor =
               [[AVAssetWriterInputPixelBufferAdaptor alloc] 
                    initWithAssetWriterInput:assetWriterInput 
                    sourcePixelBufferAttributes:
                         [NSDictionary dictionaryWithObjectsAndKeys:
                              [NSNumber numberWithInt:kCVPixelFormatType_32BGRA], 
                               kCVPixelBufferPixelFormatTypeKey,
                         nil]];
    
    
    /* Asset writer with MPEG4 format*/
    AVAssetWriter *assetWriterMyData = [[AVAssetWriter alloc]
                                    initWithURL:URLFromSomwhere
                                    fileType:AVFileTypeMPEG4
                                    error:you need to check error conditions,
                                          this example is too lazy];
    [assetWriterMyData addInput:assetWriterInput];
    assetWriterInput.expectsMediaDataInRealTime = YES;
    
    / Start writing data /
    
    [assetWriterMyData startWriting];
    [assetWriterMyData startSessionAtSourceTime:kCMTimeZero];
    [captureSession startRunning];
    
    
    
    - (void)        captureOutput:(AVCaptureOutput *)captureOutput 
        didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer 
               fromConnection:(AVCaptureConnection *)connection
    {
        CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
    
        // a very dense way to keep track of the time at which this frame
        // occurs relative to the output stream, but it's just an example!
        static int64_t frameNumber = 0;
        if(assetWriterInput.readyForMoreMediaData)
            [pixelBufferAdaptor appendPixelBuffer:imageBuffer
                             withPresentationTime:CMTimeMake(frameNumber, 25)];
        frameNumber++;
    }
    
    /* To stop recording, stop capture session and finish writing data*/
    
    [captureSession stopRunning];
    [assetWriterMyData finishWriting];
    

    【讨论】:

      猜你喜欢
      • 2016-12-22
      • 1970-01-01
      • 2018-01-15
      • 2013-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-11
      • 1970-01-01
      相关资源
      最近更新 更多