Qt VCR

I am trying to create a video recorder with Qt. What I have done so far is to take a screenshot with a rectangle on the screen and save it. In the end, I use ffmpeg to get the video file from the images.

I connected the timeout() timer signal to my custom slot, which takes a snapshot and saves it in my tmp folder. The timer has an interval of 1000/30. It should be 30 times per second. But 1000/30 is a little over 33 milliseconds, so I cannot get 30 frames per second. This is a bit more.

I recorded a YouTube video with my recorder and everything was smooth, but a little faster / slower depending on the interval.

So my question basically is, how do I get really 30/40/50 / ... fps?

+2
source share
1 answer
  • Run QElapsedTimer when starting capture.

  • When you finish the frame (at the end of the time frame of the snapshot), multiply the number of next frames by the approximate frame duration (double-precision floating point value) in milliseconds (for example, for 30 frames per second, this is ~33.33333333333 , but do not write this - write (double)1000/30 ). Call this next_timestamp value.

  • Call elapsed() on your QElapsedTimer. Call this value current_timestamp .

  • Call the static function QTimer::singleShot() to sleep for next_timestamp - current_timestamp . Set the slot argument back to the snapshot slot. Note that if bedtime is <= 0, you are behind; your system cannot handle capture speed.

When QTimer::singleShot() , your snapshot slot will be called again with some time error. However, this error is inevitable in non-real-time operating systems such as Windows and Unix (OS X / Linux / etc). This is because you do not decide when the code is executed - the kernel of the OS. On average, you end up with exactly 30 frames per second (assuming that your computer can keep up with the load!), Because the time elapsed with QElapsedTimer will be accurate enough, and if the system lags, the frame capture is faster, and if he moves forward, he will capture frames more slowly.

+1
source

Source: https://habr.com/ru/post/1209002/


All Articles