- Home /
ScreenCapture.CaptureScreenshot skips frames
I need to get series of screenshots with constant frame rate to create video from it with FFMPEG.
I use ScreenCapture.CaptureScreenshot()
method to capture screenshots in every FixedUpdate()
:
void FixedUpdate()
{
var delta = Time.deltaTime;
if (Time.realtimeSinceStartup <= 180f)
{
CaptureScreenshot();
}
else
{
Debug.Log("Got " + currentFrame + " screenshots");
}
}
private void CaptureScreenshot()
{
var fileName = currentFrame++.ToString("000000000") + ".png";
var filePath = "/tmp/frames/" + fileName;
ScreenCapture.CaptureScreenshot(filePath);
}
But as I can see, it skips lots of screenshots:
$ ls -lah . | head
-rw-r--r-- 1 user wheel 139K Jan 18 21:05 000000001.png
-rw-r--r-- 1 user wheel 139K Jan 18 21:05 000000006.png
-rw-r--r-- 1 user wheel 139K Jan 18 21:05 000000009.png
-rw-r--r-- 1 user wheel 139K Jan 18 21:05 000000013.png
-rw-r--r-- 1 user wheel 139K Jan 18 21:05 000000016.png
-rw-r--r-- 1 user wheel 139K Jan 18 21:05 000000019.png
Note: I use FixedUpdate()
because I need constant frame rate. Some performance issues are OK, it's 2D scene that is not so complex.
So, what should I do to capture "screen" contents on every call or get constant frame rate?
Answer by KISP · Jan 18, 2019 at 09:23 PM
FixedUpdate doesn't usually run once a frame as it's based on the timestep of the physics engine. Try Update instead.
I know, but it runs every fixed amount of time. Update
runs at "random" time since last update, so it can't provide fixed frame rate.
You can't have it both ways. Fps can change over time, so if you want to take a picture every x frames, there's no way to guarantee it's happening every x seconds. Likewise, if you take a picture every x seconds, an arbitrary amount of frames occur in-between.
Currently I'm going to use this solution: get every frame and save it every fixed amount of time, even if it was not updated.