繁体   English   中英

尝试以预期的帧速率将 append CVPixelBuffers 转换为 AVAssetWriterInputPixelBufferAdaptor

[英]Trying to append CVPixelBuffers to AVAssetWriterInputPixelBufferAdaptor at the intended framerate

我正在尝试以预期的帧速率将 append CVPixelBuffers 转换为 AVAssetWriterInputPixelBufferAdaptor,但它似乎太快了,而且我的数学已经关闭。 这不是从相机捕捉,而是捕捉变化的图像。 实际视频比捕获它的经过时间快得多。

我有一个 function 每 1/24 秒附加一次 CVPixelBuffer。 所以我试图在最后一次添加 1/24 秒的偏移量。

我试过了:

let sampleTimeOffset = CMTimeMake(value: 100, timescale: 2400)

和:

let sampleTimeOffset = CMTimeMake(value: 24, timescale: 600)

和:

let sampleTimeOffset = CMTimeMakeWithSeconds(0.0416666666, preferredTimescale: 1000000000)

我添加到 currentSampleTime 并像这样附加:

self.currentSampleTime = CMTimeAdd(currentSampleTime, sampleTimeOffset)

let success = self.assetWriterPixelBufferInput?.append(cv, withPresentationTime: currentSampleTime)

我想到的另一种解决方案是获取上次时间和当前时间之间的差异,并将其添加到 currentSampleTime 以确保准确性,但不确定如何执行。

我找到了一种方法来准确捕获时间延迟,通过比较最后一次以毫秒为单位与当前时间以毫秒为单位进行比较。

首先,我有一个通用的当前毫秒时间function:

func currentTimeInMilliSeconds()-> Int
{
    let currentDate = Date()
    let since1970 = currentDate.timeIntervalSince1970
    return Int(since1970 * 1000)
}

当我创建一个作家时,(当我开始录制视频时)我将 class 中的一个变量设置为当前时间(以毫秒为单位):

currentCaptureMillisecondsTime = currentTimeInMilliSeconds()

然后在我的 function 中,这应该被称为 1/24 秒并不总是准确的,所以我需要得到我开始写作时或我最后一次 function 调用之间的毫秒差异。

将毫秒转换为秒,并将其设置为 CMTimeMakeWithSeconds。

let lastTimeMilliseconds = self.currentCaptureMillisecondsTime
let nowTimeMilliseconds = currentTimeInMilliSeconds()
let millisecondsDifference = nowTimeMilliseconds - lastTimeMilliseconds

// set new current time
self.currentCaptureMillisecondsTime = nowTimeMilliseconds

let millisecondsToSeconds:Float64 = Double(millisecondsDifference) * 0.001

let sampleTimeOffset = CMTimeMakeWithSeconds(millisecondsToSeconds, preferredTimescale: 1000000000)

我现在可以用实际发生的准确延迟 append 我的帧。

self.currentSampleTime = CMTimeAdd(currentSampleTime, sampleTimeOffset)

let success = self.assetWriterPixelBufferInput?.append(cv, withPresentationTime: currentSampleTime)

当我写完视频并将其保存到我的相机胶卷时,它就是我录制时的确切持续时间。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM