How to record your screen using Python ๐Ÿ

python opencv image-processing

How to record your screen using Python
In this article, we are going to take a look at how we can record our screen using Python.

Before getting into it, one should first understand what actually a video is. Basically, a video is nothing but a sequence of images (called frames) displayed at a given frequency.

So a workaround to make a screen recorder would be to capture each frame of the screen and write it as a video file.

To do this python comes with handy libraries. For the screen recording, we will be using OpenCV, PyAutoGUI, and Numpy.

Read more about OpenCV in our last article.

So letโ€™s get started!

Make sure you have all the above mentioned necessary libraries installed.

  1. OpenCV: To install OpenCV, run pip install opencv-python
  2. PyAutoGui: To install PyAutoGui, run pip install pyautogui
  3. Numpy: To install Numpy, run pip install numpy
You are all set!

The first step is to import all the modules needed.

import cv2 #OpenCV
import pyautogui
import numpy as np
The next step is to specify the video codec using fourCC. It is a 4-byte code used for specifying the video codec. For the sake of simplicity we can pass the FourCC code for AVI video format which is *"XVID".

codec = cv2.VideoWriter_fourcc(*"XVID")
In the next step, we will be creating a videoWriter object. The video writer method of cv2 module needs 4 mandatory arguments.

First is the filename, second being the codec, third being the FPS (Frame per second) and the last being the video (or Screen) resolution.

For a better screen recording, 60 FPS is a good frame rate. You can experiment around the FPS value to get the optimum results.

out = cv2.VideoWriter("Recorded.avi", codec , 60, (1366, 768)) #Here screen resolution is 1366 x 768, you can change it depending upon your need
The next step is to implicitly create a window using namedWindow() method with the WINDOW_NORMAL flag so that the user can resize the window depending upon the need.

cv2.namedWindow("Recording", cv2.WINDOW_NORMAL)
cv2.resizeWindow("Recording", 480, 270) #Here we are resizing the window to 480x270 so that the program doesn't run in full screen in the beginning
Now we need to capture a screenshot of each frame of the screen, write it to the video file and display the screen being recorded.

Here is the code snippet,

while True:
    img = pyautogui.screenshot() #capturing screenshot
    frame = np.array(img) #converting the image into numpy array representation
    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) #converting the BGR image into RGB image
    out.write(frame) #writing the RBG image to file
    cv2.imshow('Recording', frame) #display screen/frame being recorded
    if cv2.waitKey(1) == ord('q'): #Wait for the user to press 'q' key to stop the recording

out.release() #closing the video file
cv2.destroyAllWindows() #destroying the recording window
Notice that the screenshot captured is in BGR format but in order to write the frame to the video file, we need to convert it into RGB format using cvt.Color() method with COLOR_BGR2RBG flag.

The condition if cv2.waitKey(1) == ord('q') waits for the user to press โ€˜qโ€™ key to quit the recording.

Lastly, we have closed the video file and destroyed the recording window by release() and destroyAllWindows() method respectively.

PS: Linux users please donโ€™t be happy after hearing โ€œDestroy all Windowsโ€๐Ÿ˜‚

You are all done!

Itโ€™s time to test the results.

Normal-sized window

Normal Sized window

Maximized window: (Horrible infinite loop) Alt Text

Thanks for reading!

About Author

Aditya Chaudhary
17 year old | Programmer๐Ÿ‘จโ€๐Ÿ’ป | Game Developer ๐ŸŽฎ | Blogger ๐Ÿ“ Loves CSS ๐ŸŒˆ| Python Enthusiast ๐Ÿ Discord : I might be slow here to reply so connect with me on twitter (DMs open)

Please share your Feedback:

Did you enjoy reading or think it can be improved? Donโ€™t forget to leave your thoughts in the comments section below! If you liked this article, please share it with your friends, and read a few more!