30 Sept 2023

Building a Screen Recorder with Python: Capture Desktop Activities

In this blog post, we will explore the exciting world of screen recording and learn how to build a screen recorder using Python. Screen recording is a valuable tool for various purposes, including creating tutorials, demonstrating software functionality, capturing gameplay, and more. Python, with its powerful libraries and ease of use, makes it a great choice for developing such applications. By the end of this tutorial, you will have a functional screen recorder that captures your desktop activities in real-time.


Before we dive into the coding process, make sure you have the following prerequisites installed on your system 

1. Python (version 3.x)
2. Required Python libraries: pyautogui, opencv-python, and numpy

You can install the necessary libraries using pip:pip install pyautogui opencv-python numpy

Understanding the Process

Before we begin writing code, let's understand the basic steps involved in building our screen recorder

With this plan in mind, let's start coding!

Import Libraries

First, create a new Python file, e.g., screen_recorder.py, and import the required libraries

import cv2
import numpy as np
import pyautogui
import screeninfo

cv2 (OpenCV) is a popular computer vision library used for image and video processing.
numpy is a powerful library for numerical computations.
pyautogui is a library that allows us to control the mouse and keyboard, and we'll use it to capture the screen.

Set Up Configurations

Now, let's define some configurations for our screen recorder

# Set the resolution of the screen (width, height)
screen_width, screen_height = pyautogui.size()

# Set the output video filename and frame rate
output_filename = "screen_recording.mp4"
frame_rate = 30.0

# Define the codec and create a VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
out = cv2.VideoWriter(output_filename, fourcc, frame_rate, (screen_width, screen_height))

Capture Desktop Activities

In this step, we'll capture the desktop activities and write them to the output video file

# Get the number of milliseconds between each frame capture based on the frame rate
frame_interval = int(1000 / frame_rate)

    while True:
        # Capture the screen and convert the RGB image to BGR format (required by OpenCV)
        screen = pyautogui.screenshot()
        frame = np.array(screen)
        frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)

        # Write the frame to the output video file

        # Wait for the specified interval before capturing the next frame

        # Stop the screen recording when the user presses the 'q' key
        if cv2.waitKey(1) & 0xFF == ord('q'):

except KeyboardInterrupt:
    # Stop the screen recording if the user interrupts the process (Ctrl+C)

# Release the VideoWriter and destroy all OpenCV windows

Run the Screen Recorder

Save the Python file, and you are now ready to run your screen recorder! Open a terminal or command prompt, navigate to the directory where you saved screen_recorder.py, and run the following command

python screen_recorder.py
During the execution, the screen recorder will continuously capture your desktop activities and save them to the specified output video file. To stop the recording, press the q key, and the recording will be saved.


Congratulations! You have successfully built a screen recorder using Python, capable of capturing desktop activities and saving them as a video file. With this basic implementation, you can further enhance the screen recorder by adding features like audio recording, video compression, or a graphical user interface (GUI) for user interaction.

Screen recording is a valuable tool for various purposes, including creating educational content, software testing, and content creation for social media. By combining Python's simplicity and powerful libraries like OpenCV, you can create a wide range of applications that leverage screen recording capabilities.

I hope you found this tutorial helpful and that it sparked your creativity to explore more possibilities with screen recording using Python. Happy coding!