VOOZH about

URL: https://www.geeksforgeeks.org/python/create-a-screen-recorder-using-python/

⇱ Create a Screen recorder using Python - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

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. 

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:

  • 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.

Related Articles

Comment
Article Tags: