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:
Takes a screenshot of the entire screen
Converts it to OpenCV format
Saves each frame to the video file
Shows a live preview
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
- Install Dependencies:
pip install pyautogui opencv-python numpy
- Run the Script:
python screen_recorder.py
Controls:
The recording starts immediately
A small preview window (480x270) will appear
Press 'q' to stop recording and save the file
Output:
Video saved as "Recording.avi" in the current directory
Uses XVID codec (compatible with most media players)





