3

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

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

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

    CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);

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

}

我有什么我想念的吗?

4

2 回答 2

7

经过数小时的调试,结果证明样本可能是视频或音频样本。因此,尝试从音频缓冲区获取 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...

}
于 2016-07-27T09:12:14.387 回答
-1

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
    }

}
于 2018-07-09T18:24:10.540 回答