我正在尝试从CMSampleBufferRef中检索CVPixelBufferRef,以便改变CVPixelBufferRef以动态覆盖水印。
我正在使用CMSampleBufferGetImageBuffer(sampleBuffer)
来实现这一目标。我正在打印返回的CVPixelBufferRef的结果,但它始终为null。
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
NSLog(@"PixelBuffer %@",pixelBuffer);
...
}
我有什么我想念的吗?
答案 0 :(得分:6)
经过数小时的调试后,结果可能是视频或音频样本。因此,尝试从音频缓冲区获取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...
}
答案 1 :(得分:0)
巴塞尔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
}
}