尝试以预期的帧速率将CVPixelBuffers附加到AVAssetWriterInputPixelBufferAdaptor

问题描述 投票:0回答: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中以确保准确性,但不确定如何执行。

ios swift avassetwriter cmtime
1个回答
0
投票

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

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

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)

当我写完视频并将其保存到相机胶卷时,这是我录制时的确切时长。

© www.soinside.com 2019 - 2024. All rights reserved.