1

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

我有一个函数每 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 以确保准确性,但不确定如何执行。

4

1 回答 1

1

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

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

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

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

currentCaptureMillisecondsTime = currentTimeInMilliSeconds()

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

将毫秒转换为秒,并将其设置为 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)

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

self.currentSampleTime = CMTimeAdd(currentSampleTime, sampleTimeOffset)

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

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

于 2019-11-07T22:35:01.180 回答