簡體   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