从CVPixelBuffer创建CMSampleBuffer

时间:2017-12-27 13:57:39

标签: avfoundation arkit core-video core-media

我从ARSessionDelegate获得CVPixelBuffer:

func session(_ session: ARSession, didUpdate frame: ARFrame) {
    frame.capturedImage // CVPixelBufferRef
}

但我的应用程序的另一部分(我无法更改)使用CMSampleBuffer。

CMSampleBuffer是CVPixelBuffer的容器。

为了创建CMSampleBuffer,我可以使用此功能:

func CMSampleBufferCreateReadyWithImageBuffer(_ allocator: CFAllocator?, 
                                            _ imageBuffer: CVImageBuffer, 
                                            _ formatDescription: CMVideoFormatDescription, 
                                            _ sampleTiming: UnsafePointer<CMSampleTimingInfo>, 
                                            _ sBufOut: UnsafeMutablePointer<CMSampleBuffer?>) -> OSStatus

我唯一缺少的参数是sampleTiming - 如何从CVPixelBuffer中提取它?

1 个答案:

答案 0 :(得分:1)

sampleTiming主要包含presentationTimeStamp,您可以通过以下代码轻松创建它:

let scale = CMTimeScale(NSEC_PER_SEC)
let pts = CMTime(value: CMTimeValue(frame.timestamp * Double(scale)),
                 timescale: scale)
var timingInfo = CMSampleTimingInfo(duration: kCMTimeInvalid,
                                    presentationTimeStamp: pts,
                                    decodeTimeStamp: kCMTimeInvalid)