Fix the speed of Video Screen_recorder.py

Asked

Viewed 21 times

0

Hello, I’m doing a small project to better understand how screen recordings work. I am making a program in Python, which is working perfectly, however, at the time of running the video, the speed is changed, it is recording at a higher speed.

This is my code:

import cv2
import numpy as np
import pyautogui

# display screen resolution, get it from your OS settings
SCREEN_SIZE = (1920, 1080) #pyautogui.size()
# define the codec
fourcc = cv2.VideoWriter_fourcc(*"XVID")
# create the video write object
out = cv2.VideoWriter("output.avi", fourcc, 20.0, (SCREEN_SIZE))

while True:
    # make a screenshot
    img = pyautogui.screenshot()
    # img = pyautogui.screenshot(region=(0, 0, 300, 400))
    # convert these pixels to a proper numpy array to work with OpenCV
    frame = np.array(img)
    # convert colors from BGR to RGB
    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    # write the frame
    out.write(frame)
    # show the frame
    cv2.imshow("screenshot", frame)
    # if the user clicks q, it exits
    if cv2.waitKey(1) == ord("q"):
        break

# make sure everything is closed when exited
cv2.destroyAllWindows()
out.release()

These are the commands to install libraries from the terminal:

pip3 install cv2

pip3 install numpy

pip3 install pyautogui

I’d like to know what I can change in my code to record a video at normal speed, that’s all :)

I tried to figure out how to fix it, but I couldn’t find anyone talking about it. I tried to change the FPS rate but I didn’t get any results, I tried to change the CODEC of the video and switch to MP4, but also I didn’t get any results, I don’t have as many ideas of what to do to fix this problem. Maybe it’s something related to pyautogui.screenshot()? Or how frames are organized?

Let me tell you a little bit about this code so I don’t get lost.

SCREEN_SIZE - It is the variable of the screen size of the monitor, in my case, 1920 x 1080. If you don’t know your monitor’s screen size, change the value from "(1920, 1080)" to "pyautogui.size()"

FOURCC - It is the variable that will indicate the CODEC of the video, if you want you can change.

OUT - The variable "Out" gathers all the information of the other variables (At least, that’s what I studied, I’m still not sure if that’s it but it’s okay!). In that same variable, there is a part written "output.avi", that is the file name + the format. Where it says "20.0", this is the FPS rate (Frames per Second), you can change it if you want.

And then in "while True:" he starts taking the screenshots and putting it all together in a video.

If you have any questions about the code, you can tell me here in comments :D

Thank you for your reply!!

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.