Create a Screen recorder using Python

Last Updated : 15 Jun, 2026

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.

Working of the Screen Recorder

  • Capture the Screen: The program continuously takes screenshots using PyAutoGUI.
  • Convert Screenshots to Frames: Each screenshot is converted into a NumPy array that can be processed by OpenCV.
  • Write Frames to a Video: OpenCV stores the captured frames in a video file using a specified codec and frame rate.
  • Display Live Preview (Optional): A preview window shows the recording while it is in progress.
  • Stop Recording: Pressing the q key stops the recording and saves the video.

Install Required Libraries

  • PyAutoGUI: Captures screenshots of the screen.
  • NumPy: Converts screenshots into arrays for processing.
  • OpenCV: Handles video creation and frame processing.

pip install numpy pyautogui opencv-python

Implementation

First, import all the required packages. 

Python
# importing the required packages
import pyautogui
import cv2
import numpy as np

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.

Python
# Specify resolution
resolution = (1920, 1080)

# Specify video codec
codec = cv2.VideoWriter_fourcc(*"XVID")

# Specify name of Output file
filename = "Recording.avi"

# Specify frames rate. We can choose 
# any value and experiment with it
fps = 60.0

# Creating a VideoWriter object
out = cv2.VideoWriter(filename, codec, fps, resolution)

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.

Python
# Create an Empty window
cv2.namedWindow("Live", cv2.WINDOW_NORMAL)

# Resize this window
cv2.resizeWindow("Live", 480, 270)

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.

Python
while True:

    # Take screenshot using PyAutoGUI
    img = pyautogui.screenshot()

    # Convert the screenshot to a numpy array
    frame = np.array(img)

    # Convert the screenshot from RGB to BGR
    # for OpenCV processing
    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

    # Write it to the output file
    out.write(frame)
    
    # Optional: Display the recording screen
    cv2.imshow('Live', frame)
    
    # Stop recording when we press 'q'
    if cv2.waitKey(1) == ord('q'):
        break

After everything is done, we will release the writer and destroy all windows opened by OpenCV.

Python
# Release the Video writer
out.release()

# Destroy all windows
cv2.destroyAllWindows()

Full Code:

Python
# importing the required packages
import pyautogui
import cv2
import numpy as np

# Specify resolution
resolution = (1920, 1080)

# Specify video codec
codec = cv2.VideoWriter_fourcc(*"XVID")

# Specify name of Output file
filename = "Recording.avi"

# Specify frames rate. We can choose any 
# value and experiment with it
fps = 60.0


# Creating a VideoWriter object
out = cv2.VideoWriter(filename, codec, fps, resolution)

# Create an Empty window
cv2.namedWindow("Live", cv2.WINDOW_NORMAL)

# Resize this window
cv2.resizeWindow("Live", 480, 270)

while True:
    # Take screenshot using PyAutoGUI
    img = pyautogui.screenshot()

    # Convert the screenshot to a numpy array
    frame = np.array(img)

    # Convert it from BGR(Blue, Green, Red) to
    # RGB(Red, Green, Blue)
    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

    # Write it to the output file
    out.write(frame)
    
    # Optional: Display the recording screen
    cv2.imshow('Live', frame)
    
    # Stop recording when we press 'q'
    if cv2.waitKey(1) == ord('q'):
        break

# Release the Video writer
out.release()

# Destroy all windows
cv2.destroyAllWindows()

Output:

Explanation:

  • Import Libraries: Imports PyAutoGUI, NumPy, and OpenCV for screen capture, image processing, and video creation.
  • Configure Recording Settings: Sets the video resolution, codec, filename, and frame rate.
  • Create Video Writer: Initializes a VideoWriter object to save recorded frames.
  • Capture Screen Frames: Continuously takes screenshots and converts them into OpenCV-compatible frames.
  • Save and Preview Frames: Writes each frame to the video file and optionally displays a live preview.
  • Stop Recording: Ends the recording when the q key is pressed.
  • Release Resources: Closes the video file and destroys all OpenCV windows.
Comment