Overview
In this tutorial, we will learn how to create a screen recorder using the OpenCV and PyAutoGUI library.
PyAutoGUI is a cross-platform GUI automation module in Python used to programmatically control the mouse & keyboard.
Installing the modules
To install the OpenCV and PyAutoGUI module and some other associated dependencies, we can use the pip command:
pip install opencv-python
pip install PyAutoGUI
pip install numpy
The Code
import pyautogui
import cv2
import numpy as np
from datetime import datetime
resolution = pyautogui.size()
now = datetime.now()
timestamp = datetime.timestamp(now)
codec = cv2.VideoWriter_fourcc(*"XVID")
filename = str(int(timestamp))+".avi"
fps = 60.0
out = cv2.VideoWriter(filename, codec, fps, resolution)
cv2.namedWindow("Live", cv2.WINDOW_NORMAL)
cv2.resizeWindow("Live", 480, 270)
while True:
img = pyautogui.screenshot()
frame = np.array(img)
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
out.write(frame)
cv2.imshow("Live", frame)
if cv2.waitKey(1) == ord('q'):
break
out.release()
cv2.destroyAllWindows()