The Ultimate Python Project: Creating a Screen Recorder

Create A Screen Recorder Using Python

Are you looking for a simple way to record your screen using Python? Whether you’re building a productivity tool or just want to automate screen capturing, Python makes it easy. In this detailed tutorial, we’ll walk you through how to build your own screen recorder using Python—step by step.

This guide is perfect for beginners and intermediate Python programmers who want to build a practical GUI-less tool.

Why Use Python for Screen Recording?

Python offers powerful libraries like OpenCV, PyAutoGUI, and NumPy, which make it easy to capture screenshots, process images, and write video files—all with minimal code. It’s a lightweight solution compared to full-featured tools, and you can fully customize the behavior.

Prerequisites

Before we begin, make sure you have the following Python packages installed:

  • Numpy: To install Numpy type the below command in the terminal.
pip install numpy

pyautogui: To install pyautogui type the below command in the termina

pip install pyautogui
  • OpenCV: To install OpenCV type the below command in the terminal.
pip install opencv-python

Step-by-Step Code Walkthrough

Here’s the complete code for your Python screen recorder:


import cv2
import numpy as np
import pyautogui
import time

# Get screen resolution
screen_width, screen_height = pyautogui.size()
resolution = (screen_width, screen_height)

# Output settings
output_filename = "screen_recording.mp4"
fps = 30.0
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
out = cv2.VideoWriter(output_filename, fourcc, fps, resolution)

# Check if VideoWriter is initialized
if not out.isOpened():
    print("Error: Failed to initialize VideoWriter")
    exit()

# Duration
recording_duration = 5  # seconds

print("Recording started...")

for _ in range(int(fps * recording_duration)):
    screenshot = pyautogui.screenshot()
    frame = np.array(screenshot)
    frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
    out.write(frame)
    time.sleep(1 / fps)  # maintain fps

out.release()
print(f"Recording finished. Saved as {output_filename}")
  

How to Customize Your Screen Recorder

Want to enhance your screen recorder? Here are a few ways:

  • Increase recording time: Adjust the recording_duration variable.

  • Change output quality: Modify fps for smoother video.

  • Add audio recording: Integrate pyaudio for audio capture (advanced).

  • Preview window: Use cv2.imshow() to preview frames in real-time.

Common Issues & Fixes

Problem Solution
Output video is blank Ensure the screen resolution is set correctly
File won’t play on some players Try using "XVID" codec instead of "mp4v"
App crashes during loop Reduce fps if system resources are limited

Conclusion

You’ve just created a fully functional screen recorder using Python with only a few lines of code! This project is perfect for building your portfolio or automating screen-based tasks. With additional features like audio recording or real-time preview, you can take it to the next level.

Ready to try it out? Open your code editor and start recording!

FAQs

Q1: Can this code record audio too?
A: Not by default. You’ll need to use libraries like pyaudio to add audio support.

Q2: Does it work on Mac or Linux?
A: Yes, but you may need to modify the codec depending on the OS.

Q3: How can I stop the recording with a key press?
A: Use keyboard module to listen for a stop key (like esc) inside the loop.

Post You May Also Like: