从 CVPixelBuffer 创建 CMSampleBuffer

Create a CMSampleBuffer from a CVPixelBuffer

我从 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 中提取它?

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)