Skip to main content
Python

πŸŽ₯ How to Build Your Own Screen Recorder in Python (Command Line + GUI Version)

Capture your screen effortlessly β€” whether you're a developer, gamer, or content creator, this tutorial will teach you how to create a simple yet powerful screen recording tool in Python! πŸš€

G

gs_admin

Author & Reviewer

Published

Apr 27, 2025

Read Time

2 min read

article.txt
πŸ“°
Python

Capture your screen effortlessly β€” whether you're a developer, gamer, or content creator, this tutorial will teach you how to create a simple yet powerful screen recording tool in Python! πŸš€

In this guide, we'll show you two ways to build your own Python screen recorder:

  • A command-line version (super simple & lightweight 🎯)
  • A GUI version (beautiful interface with countdown ⏳)

No prior experience? No problem! By the end, you'll have your own custom screen recording app ready to use.

Build a screen recorder using Python
Build a screen recorder using Python

πŸ“¦ Prerequisites

Before diving in, install these Python packages:

bash
1
pip install opencv-python pyautogui numpy

Tkinter (used for the GUI version) usually comes built-in with Python, but install it separately if it's missing.

1️⃣ Command-Line Screen Recorder

Let's start with a barebones but powerful command-line screen recorder.

python
1234567891011121314151617181920212223
import cv2
import numpy as np
import pyautogui
import time

output_file = 'recording1.avi'
fps = 20.0
duration = 100  # seconds

screen_size = pyautogui.size()
fourcc = cv2.VideoWriter_fourcc(*"XVID")
out = cv2.VideoWriter(output_file, fourcc, fps, screen_size)

print("Recording started...")
start_time = time.time()

while time.time() - start_time < duration:
    frame = np.array(pyautogui.screenshot())
    frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
    out.write(frame)

out.release()
print(f"Recording saved as {output_file}")

How it works: it captures a screenshot every frame with pyautogui, converts it to the color format OpenCV expects, and writes each frame into a .avi video file. The recording length is controlled by a simple timer.

🧠 Pro tips: change duration to set how long the recording lasts, and adjust fps for smoother (or smaller) videos.

2️⃣ Professional GUI Screen Recorder with Countdown ⏳

Now let's level up with a Graphical User Interface β€” this one looks and feels slick and professional.

python
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
import cv2
import numpy as np
import pyautogui
import time
import threading
import tkinter as tk
from tkinter import ttk

class ScreenRecorder:
    def __init__(self):
        self.recording = False
        self.out = None
        self.fps = 20.0
        self.output_file = &#039;recording.avi'

    def start_recording(self):
        screen_size = pyautogui.size()
        fourcc = cv2.VideoWriter_fourcc(*"XVID")
        self.out = cv2.VideoWriter(self.output_file, fourcc, self.fps, screen_size)

        self.recording = True
        threading.Thread(target=self.record).start()

    def stop_recording(self):
        self.recording = False

    def record(self):
        for i in range(3, 0, -1):
            countdown_var.set(f"Recording starts in {i}…")
            time.sleep(1)
        countdown_var.set("Recording in progress...")

        while self.recording:
            img = pyautogui.screenshot()
            frame = np.array(img)
            frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
            self.out.write(frame)

            if cv2.waitKey(1) == ord(&#039;q'):
                break

        self.out.release()
        cv2.destroyAllWindows()
        countdown_var.set("Recording saved as recording.avi")


def start():
    countdown_var.set("")
    recorder.start_recording()

def stop():
    recorder.stop_recording()


if __name__ == "__main__":
    recorder = ScreenRecorder()

    root = tk.Tk()
    root.title("πŸ–₯️ Screen Recorder")
    root.geometry("400x300")
    root.configure(bg="#1e1e1e")

    style = ttk.Style()
    style.theme_use("clam")
    style.configure("TButton",
                    font=("Segoe UI", 12),
                    padding=10,
                    background="#3c3f41",
                    foreground="white")
    style.map("TButton",
              background=[("active", "#5c5f61")])

    title_label = tk.Label(root, text="Screen Recorder", font=("Segoe UI", 20, "bold"), fg="white", bg="#1e1e1e")
    title_label.pack(pady=20)

    start_button = ttk.Button(root, text="▢️ Start Recording", command=start)
    start_button.pack(pady=10)

    stop_button = ttk.Button(root, text="⏹️ Stop Recording", command=stop)
    stop_button.pack(pady=10)

    countdown_var = tk.StringVar()
    countdown_label = tk.Label(root, textvariable=countdown_var, font=("Segoe UI", 12), fg="lime", bg="#1e1e1e")
    countdown_label.pack(pady=10)

    root.mainloop()

How it works: Tkinter builds a clean, dark-themed GUI; the Start Recording button triggers a 3-second countdown; the Stop Recording button ends recording and saves the file automatically; and the countdown/status is displayed in real time.

✨ Why Build Your Own Screen Recorder in Python?

  • 100% customizable β€” add webcam, annotations, make it yours!
  • Lightweight β€” no bloatware, only the features you want.
  • Learning experience β€” understand real-world coding applications.
  • Fun project β€” impress friends, employers, or clients!

πŸš€ Conclusion

And there you have it β€” a full-fledged Python screen recorder, with both a quick command-line version and a gorgeous GUI version. Whether you want to capture gameplay, create tutorials, or record meetings, this Python tool has you covered.

Ready to take it even further? Add webcam capture, record system audio, or schedule recordings automatically β€” great next steps to explore!

G

About the Author: gs_admin

A senior technical contributor specializing in architectural designs, software optimization, database structures, and developer education. Passionate about writing clean code and sharing engineering knowledge.