I am trying to make a screen recording script with python. This is what I have so far:
import cv2
import numpy as np
import pyautogui
import time
img = pyautogui.screenshot()
img = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter("video.mp4", fourcc, 60.0, (1680, 1050))
t1 = time.time()
for i in range(0, 300):
img = pyautogui.screenshot()
image = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
out.write(image)
t2 = time.time()
print(t2-t1)
out.release()
Adding a delay doesn't help much either. I have tried time.sleep(1/60) because the framerate is 60 FPS.
This code should make a 5 second long video (which it does) but to actually take 300 frames (screenshots) it takes 21 seconds, making the video really fast. I assume this could be solved if the frame-catching procedure was way faster. I have timed on how long it takes pyautogui.screenshot() and PIL.ImageGrab.grab() to take a screenshot, both roughly around 0.05 seconds. The current "solution" I have is to lower the framerate to somewhere around 10 fps but then it becomes really choppy. How can I fix this?