【问题标题】:CMSampleBufferGetImageBuffer returning nullCMSampleBufferGetImageBuffer 返回 null
【发布时间】:2016-07-26 13:59:03
【问题描述】:

我正在尝试从 CMSampleBufferRef 检索 CVPixelBufferRef,以更改 CVPixelBufferRef 以动态覆盖水印。

我正在使用CMSampleBufferGetImageBuffer(sampleBuffer) 来实现这一目标。我正在打印返回的 CVPixelBufferRef 的结果,但它始终为空。

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

    CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);

    NSLog(@"PixelBuffer %@",pixelBuffer);
...

}

我还有什么遗漏的吗?

【问题讨论】:

    标签: avfoundation cmsamplebufferref


    【解决方案1】:

    经过数小时的调试,结果证明样本可能是视频或音频样本。所以尝试从音频缓冲区中获取 CVPixelBufferRef 会返回 null。

    我在继续之前通过检查样本类型解决了这个问题。因为我对音频样本不感兴趣,所以我只是在它是音频样本时返回。

    - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
    
        CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
    
        CMFormatDescriptionRef formatDesc = CMSampleBufferGetFormatDescription(sampleBuffer);
        CMMediaType mediaType = CMFormatDescriptionGetMediaType(formatDesc);
    
        //Checking sample type before proceeding
        if (mediaType == kCMMediaType_Audio)
        {return;}
    
    //Processing the sample...
    
    }
    

    【讨论】:

      【解决方案2】:

      Basel JD 在 Swift 4.0 中的回答。它对我有用

      func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
      
          guard let formatDescription: CMFormatDescription = CMSampleBufferGetFormatDescription(sampleBuffer) else { return }
      
          let mediaType: CMMediaType = CMFormatDescriptionGetMediaType(formatDescription)
      
          if mediaType == kCMMediaType_Audio {
              print("this was an audio sample....")
              return
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 2019-06-12
        • 2019-09-18
        • 1970-01-01
        • 2020-06-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-21
        • 2020-06-19
        相关资源
        最近更新 更多