视频录制逻辑问题;填充空框架和同步FPS

发布于 2025-02-09 18:07:45 字数 827 浏览 1 评论 0原文

我的视频录制逻辑有一些问题。 我的录音算法以伪代码为下面。

fps = 30
msPerFrame = 1000 / fps
videoRecorder = VideoRecorder(fps)
timer.start()
while (true) {
  if ((timer.elapsed() >= msPerFrame) && (newFrame.isReady() == true)) {
    videoRecorder.push(newFrame)
    timer.restart()
  }
}

请注意,videorecorder在创建视频文件并开始录制时确定视频文件的fps。
问题在下面:

  1. (timer.elapsed()> = msperFrame)& amp; (newframe.isready()== false)?如果我只等待帧准备就绪,尽管fps记录了此差距,尽管大于实际msperframe
  2. 如何校准记录FPS错误?如果fps = 30msperframe = 33.3333 ...。但是,timer.elapsed()返回毫秒值so timer.elapsed()> = msperframemsperframe> = 34时可能是正确的。因此,将1020毫秒的30 newFrame s推到1000毫秒的结果视频。

I got some problems with my video recording logic.
My recording algorithm is below as pseudo code.

fps = 30
msPerFrame = 1000 / fps
videoRecorder = VideoRecorder(fps)
timer.start()
while (true) {
  if ((timer.elapsed() >= msPerFrame) && (newFrame.isReady() == true)) {
    videoRecorder.push(newFrame)
    timer.restart()
  }
}

Note that the videoRecorder determines the fps of the video file as soon as it is created and starts recording.
The problem is below:

  1. What is the best way to handle when (timer.elapsed() >= msPerFrame) && (newFrame.isReady() == false)? If I just wait for a frame to be ready, this gap is recorded at fps despite being larger than the actual msPerFrame.
  2. How to calibrate recording fps error? If fps=30, msPerFrame=33.3333.... However, the timer.elapsed() returns milliseconds value so timer.elapsed() >= msPerFrame may true when msPerFrame >= 34. So the 30 newFrames at 1020 milliseconds are pushed to 1000 milliseconds of resulting video.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉薄对峙 2025-02-16 18:07:45

我简单地使用包含newFrame作为FIFO订单的框架缓冲区解决了问题(1)。录音机现在在需要将框架推向记录缓冲区时引用框架缓冲区的最后帧。因此,如果newFrametimer.elapsed()> = msperframe时还没有准备好,则录音机再按下最后一个帧。

bufferMaxSize = MAX_BUF_SIZE
frameBuffer
isRecording = false

// This callback is called when newFrame is ready.
callbackFunction(newFrame) {
  frameBuffer.push(newFrame) // Push to the back of the buffer
  while (frameBuffer.size() > MAX_BUF_SIZE)
    frameBuffer.pop() // Remove front of the buffer
}

startRecordingFunction() {
  fps = 30
  msPerFrame = 1000 / fps
  videoRecorder = VideoRecorder(fps)
  timer.start()

  while (isRecording) {
    if (timer.elapsed() >= msPerFrame) {
      videoRecorder.push(frameBuffer.back())
      timer.restart()
    }
  }

  videoRecorder.save("video.mp4")
}

另外,运行StarTrecordingFunction()的线程对时间至关重要,因此给出更高的工作优先级,这给了我更稳定的结果。

I simply solved the question (1) using frame buffer which contains newFrame as FIFO order. The recorder now references the last frame of the frame buffer when needing to push the frame to the recording buffer. So, if newFrame is not ready when timer.elapsed() >= msPerFrame, then the recorder push the last frame one more.

bufferMaxSize = MAX_BUF_SIZE
frameBuffer
isRecording = false

// This callback is called when newFrame is ready.
callbackFunction(newFrame) {
  frameBuffer.push(newFrame) // Push to the back of the buffer
  while (frameBuffer.size() > MAX_BUF_SIZE)
    frameBuffer.pop() // Remove front of the buffer
}

startRecordingFunction() {
  fps = 30
  msPerFrame = 1000 / fps
  videoRecorder = VideoRecorder(fps)
  timer.start()

  while (isRecording) {
    if (timer.elapsed() >= msPerFrame) {
      videoRecorder.push(frameBuffer.back())
      timer.restart()
    }
  }

  videoRecorder.save("video.mp4")
}

Plus, the thread running startRecordingFunction() is critical for time, so giving higher job priority of that thread gave me more stable result.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文