Skip to main content

Command Palette

Search for a command to run...

Screen Recorder: A detailed Walk-through

Updated
2 min read
Screen Recorder: A detailed Walk-through

This script creates a screen recorder that captures your entire desktop at 60 FPS and saves it as "Recording.avi" while showing a smaller live preview. It uses PyAutoGUI for screen capture and OpenCV for video processing.

Key Components

1. Imports and Setup

import pyautogui  # For taking screenshots
import cv2  # OpenCV for video processing
import numpy as np  # For array operations

2. Configuration

resolution = (1920, 1080)  # Recording resolution (adjust to your screen)
codec = cv2.VideoWriter_fourcc(*"XVID")  # Video codec for AVI format
filename = "Recording.avi"  # Output filename
fps = 60.0  # Frames per second (higher = smoother but larger file)

3. Video Writer Initialization

out = cv2.VideoWriter(filename, codec, fps, resolution)

This creates a VideoWriter object that will encode and save the frames to "Recording.avi" using the XVID codec at 60 FPS.

4. Preview Window Setup

cv2.namedWindow("Live", cv2.WINDOW_NORMAL)
cv2.resizeWindow("Live", 480, 270)

A resizable window named "Live" is created and resized to 480x270 pixels for a compact preview.

5. Main Recording Loop

while True:
    # Capture screenshot
    img = pyautogui.screenshot()

    # Convert to numpy array (OpenCV format)
    frame = np.array(img)

    # Convert RGB (PyAutoGUI) to BGR (OpenCV format)
    frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)

    # Save frame to video file
    out.write(frame)

    # Display frame in preview window
    cv2.imshow('Live', frame)

    # Exit on 'q' key press
    if cv2.waitKey(1) == ord('q'):
        break

The loop continuously:

  1. Takes a screenshot of the entire screen

  2. Converts it to OpenCV format

  3. Saves each frame to the video file

  4. Shows a live preview

  5. Checks for the 'q' key to stop recording

6. Cleanup

out.release()  # Save and close video file
cv2.destroyAllWindows()  # Close preview window

Releases system resources and properly closes the video file when recording stops.

Usage Instructions

  1. Install Dependencies:
pip install pyautogui opencv-python numpy
  1. Run the Script:
python screen_recorder.py
  1. Controls:

    • The recording starts immediately

    • A small preview window (480x270) will appear

    • Press 'q' to stop recording and save the file

  2. Output:

    • Video saved as "Recording.avi" in the current directory

    • Uses XVID codec (compatible with most media players)

More from this blog

H

Human Firewall: Where Digital Safety Meets Real Life

21 posts

Here, we don't just talk about firewalls, encryption, and threat detection-we explore how these principles apply to protecting what matters most in our lives, relationships, and personal growth.