![]() |
VOOZH | about |
Screen recording is a technique used for creating tutorials, demonstrations, presentations, and debugging sessions. Using Python, we can build a simple screen recorder by capturing screenshots continuously and combining them into a video. In this project, PyAutoGUI is used to capture the screen, NumPy processes the image frames, and OpenCV writes the frames to a video file.
pip install numpy pyautogui opencv-python
First, import all the required packages.
Now, before recording the screen, we have to create a VideoWriter object. Also, we have to specify the output file name, Video codec, FPS, and video resolution. In video codec, we have to specify a 4-byte code (such as XVID, MJPG, X264, etc.). We'll be using XVID here.
Note: Ensure that the resolution matches your screen size. Using an incorrect resolution may result in distorted or cropped recordings.
Optional: To display the recording in real-time, we have to create an Empty window and resize it.
Now, let's start recording our screen. We will be running an infinite loop and in each iteration of the loop, we will take a screenshot and write it to the output file with the help of the video writer.
After everything is done, we will release the writer and destroy all windows opened by OpenCV.
Full Code:
Output:
Explanation: