CMSampleBufferGetImageBuffer 返回空

CMSampleBufferGetImageBuffer returning null

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

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

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

    CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);

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

}

我还有什么遗漏吗?

经过几个小时的调试,发现样本可能是视频或音频样本。因此,尝试从音频缓冲区中获取 CVPixelBufferRef returns null.

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

- (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...

}

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
    }

}