Game Screen Recorder in Python

2 min read

Here’s a quick Python script to do it.

Install These

  1. pyautogui: Takes screenshots.
    pip install pyautogui
  2. opencv-python: Writes video frames.
    pip install opencv-python
  3. numpy: Handles arrays (i have it by default ).

The Code

import cv2 as cv
import pyautogui
import numpy as np
screen_size = pyautogui.size()
video = cv.VideoWriter(‘GameRecording.avi’,
cv.VideoWriter_fourcc(*‘MJPG’),
20, screen_size)
print(“Recording… Press ‘q’ to stop.”)
while True:
frame = np.array(pyautogui.screenshot())
frame = cv.cvtColor(frame, cv.COLOR_BGR2RGB)
video.write(frame)
cv.imshow(“Recording (Minimize this)”, frame)
if cv.waitKey(1) == ord(‘q’):
break
cv.destroyAllWindows()
video.release()

Problems I Faced

  • Lag: Minimize the preview window.
  • Colors Off: Convert BGR to RGB (fixed in the code).
  • Huge File.avi is big. Use another codec if you care.
    like XVID [weird name]

Done. Your recording is saved as GameRecording.avi.

It will be saved in your code container folder