简体   繁体   English

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

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

I'm trying to append CVPixelBuffers to AVAssetWriterInputPixelBufferAdaptor at the intended framerate, but it seems to be too fast, and my math is off.我正在尝试以预期的帧速率将 append CVPixelBuffers 转换为 AVAssetWriterInputPixelBufferAdaptor,但它似乎太快了,而且我的数学已经关闭。 This isn't capturing from the camera, but capturing changing images.这不是从相机捕捉,而是捕捉变化的图像。 The actual video is much to fast than the elapsed time it was captured.实际视频比捕获它的经过时间快得多。

I have a function that appends the CVPixelBuffer every 1/24 of a second.我有一个 function 每 1/24 秒附加一次 CVPixelBuffer。 So I'm trying to add an offset of 1/24 of a second to the last time.所以我试图在最后一次添加 1/24 秒的偏移量。

I've tried:我试过了:

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

and:和:

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

and:和:

let sampleTimeOffset = CMTimeMakeWithSeconds(0.0416666666, preferredTimescale: 1000000000)

I'm adding onto the currentSampleTime and appending like so:我添加到 currentSampleTime 并像这样附加:

self.currentSampleTime = CMTimeAdd(currentSampleTime, sampleTimeOffset)

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

One other solution I thought of is get the difference between the last time and the current time, and add that onto the currentSampleTime for accuracy, but unsure how to do it.我想到的另一种解决方案是获取上次时间和当前时间之间的差异,并将其添加到 currentSampleTime 以确保准确性,但不确定如何执行。

I found a way to accurately capture the time delay by comparing the last time in milliseconds compared to the current time in milliseconds.我找到了一种方法来准确捕获时间延迟,通过比较最后一次以毫秒为单位与当前时间以毫秒为单位进行比较。

First, I have a general current milliseconds time function:首先,我有一个通用的当前毫秒时间function:

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

When I create a writer, (when I start recording video) I set a variable in my class to the current time in milliseconds:当我创建一个作家时,(当我开始录制视频时)我将 class 中的一个变量设置为当前时间(以毫秒为单位):

currentCaptureMillisecondsTime = currentTimeInMilliSeconds()

Then in my function that's supposed to be called 1/24 of a second is not always accurate, so I need to get the difference in milliseconds between when I started writing, or my last function call.然后在我的 function 中,这应该被称为 1/24 秒并不总是准确的,所以我需要得到我开始写作时或我最后一次 function 调用之间的毫秒差异。

Do a conversion of milliseconds to seconds, and set that to CMTimeMakeWithSeconds.将毫秒转换为秒,并将其设置为 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)

I can now append my frame with the accurate delay that actually occurred.我现在可以用实际发生的准确延迟 append 我的帧。

self.currentSampleTime = CMTimeAdd(currentSampleTime, sampleTimeOffset)

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

When I finish writing the video and I save it to my camera roll, it is the exact duration from when I was recording.当我写完视频并将其保存到我的相机胶卷时,它就是我录制时的确切持续时间。

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

相关问题 将缓冲区附加到 AVAssetWriterInputPixelBufferAdaptor *不* 按时间顺序? - Append buffers to an AVAssetWriterInputPixelBufferAdaptor *not* chronologically? AVAssetWriterInputPixelBufferAdaptor appendPixelBuffer - AVAssetWriterInputPixelBufferAdaptor appendPixelBuffer AVAssetWriterInputPixelBufferAdaptor 和 CMTime - AVAssetWriterInputPixelBufferAdaptor and CMTime 使用ASScreenRecorder的AVAssetWriterInputPixelBufferAdaptor appendPixelBuffer - AVAssetWriterInputPixelBufferAdaptor appendPixelBuffer using ASScreenRecorder AVAssetWriter中AVAssetWriterInputPixelBufferAdaptor的重要性 - Importance of AVAssetWriterInputPixelBufferAdaptor in AVAssetWriter AVAssetWriter和AVAssetWriterInputPixelBufferAdaptor appendingPixelBuffer组合失败 - Failure of combination of AVAssetWriter and AVAssetWriterInputPixelBufferAdaptor appendingPixelBuffer 尝试附加转发的推文 - Trying to Append Retweeted Tweet AVAssetWriterInputPixelBufferAdaptor appendPixelBuffer:buffer withPresentationTime返回错误NSURLErrorCannotCreateFile - AVAssetWriterInputPixelBufferAdaptor appendPixelBuffer:buffer withPresentationTime returning error NSURLErrorCannotCreateFile AVAssetWriter / AVAssetWriterInputPixelBufferAdaptor - 黑帧和帧速率 - AVAssetWriter / AVAssetWriterInputPixelBufferAdaptor - black frames and frame rate AVAssetWriterInputPixelBufferAdaptor appendPixelBuffer:EXC_BAD_ACCESS - AVAssetWriterInputPixelBufferAdaptor appendPixelBuffer: EXC_BAD_ACCESS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM