Screen Recorder using Python

Hey there! In this tutorial, we will be learning to build a Screen Recorder using Python in PyCharm.

Python is a widely-used general-purpose language, that allows a variety of tasks to be performed. One of them is recording the display screen, that is, creating a screen recorder using the pyautogui module. Below attached is a simple demonstration of this functionality.


Step 1

Firstly, open PyCharm and create a project titled Screen_Recorder. Thenopen the terminal and type the below-listed commands to install the respective libraries:

pip install opencv-python
pip install numpy
pip install pyautogui
pip install pywin32
  • opencv-python: OpenCV packages for Python
  • NumPy: Library that supports n-dimensional arrays along with a large collection of high-level mathematical functions to operate on these arrays
  • pyautogui: Cross-platform GUI automation Python module, used to programmatically control the mouse and keyboard
  • pywin32:  Module encapsulating the Windows Win32 API

Step 2

Then, within the file in thiproject, type the below-specified code. Refer to the code’s comments for an explanation regarding the code.

# Import necessary libraries:
import cv2
import numpy as np
import pyautogui
from win32api import GetSystemMetrics
import time

# Access screen width:
w = GetSystemMetrics(0)
# Access screen height:
h = GetSystemMetrics(1)
# Store screen dimensions within a tuple:
dimension = (w, h)
# Define codec -> FourCC is a 4-byte code used to specify the video codec
fourcc = cv2.VideoWriter_fourcc(*"XVID")
# VideoWriter -> This class provides C++ API for writing video files or image sequences
# Constructor parameters-> video filename, video codec, video frame-rate(fps), screen dimensions
output = cv2.VideoWriter("recording.mp4", fourcc, 20.0, dimension)

# Access current system time:
now = time.time()
# Read screen recording duration via user input:
# time() -> Returns the time as a floating point number expressed in seconds
duration = int(input('Specify recording duration in seconds: '))
# Buffer time to ensure that the recorded video duration is as specified by user:
# This is done because, code must be executed up till line #33, prior to recording initiation.
duration += duration
# Identify the time at which recording must stop:
end_time = now + duration

while True:
    # Take a screenshot:
    # screenshot() -> Returns an Image object
    img = pyautogui.screenshot()
    # Import image data into NumPy array:
    frame = np.array(img)
    # Use cvtColor() method to convert image from BGR to RGB color format:
    # This conversion ensures that the recording exactly resembles the content that had been recorded
    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    # Write the frame into the file 'recording.mp4':
    # Access current system time:
    current_time = time.time()
    # Check if it is time to stop recording. If so, break out of while loop.
    if current_time>end_time:

# Release the capture


Once the code has been executed and the user has entered the desired video duration, the recording begins. Execution of the code terminates once the recording has been completed and the mp4 file has been created.
The recorded video can be found within this project as shown below.

screen recording


Also read, Capture and save webcam video in Python using OpenCV

Leave a Reply

Your email address will not be published. Required fields are marked *