How to Create a Screen Recorder Using Python

Screen recorders are used by many people for many purposes like: for example, recording classes, YouTube streaming and many more. But finding the perfect screen recorder may be difficult. So why not build our own screen recorder with a few lines of code.

Lets get started.

Screen Recorder Project

Requirements:  

Basic knowledge of python(go through Learn Python Tutorial) . We can use any ide ( for example:  https://code.visualstudio.com/download , https://jupyter.org/).

Modules:

1. PyAutoGUI : This library provides a cross-platform support for controlling mouse and keyboard programmatically. It helps us to click at an exact position in the screen, scroll, drag and move.

2. NumPy : NumPy (an acronym for “Numeric Python” or “Numerical Python”) is a library for the Python programming language. It is used for working with large ,multi-dimensional arrays and matrices.

3. OpenCV : It is a python open-source library, we use it for all sorts of image and video analysis, for instance, facial recognition and detection, photo editing etc.

Installation:

Firstly, we have to open command prompt in administrator mode.

NumPy :To install Numpy we will type the below command in the terminal.

pip install numpy

pyautogui: To install pyautogui we will type the below command in the terminal.

pip install pyautogui

OpenCV: Similarly, to install OpenCV we have to type the below command in the terminal.

pip install opencv-python

Approach:

1. Firstly, we will import the modules one by one.

import pyautogui  
import cv2  
import numpy as np

2. 2. Secondly, we have to specify the video resolution, video codec, name of the output file and the fps (frame rate per second, 60 FPS is a good frame rate ).Then, we will create a VideoWriter object.

resolution = (1920, 1080) 
codec = cv2.VideoWriter_fourcc(*"XVID") 
filename = "MyRecording.avi"
fps = 60.0
out = cv2.VideoWriter(filename, codec, fps, resolution)

3. Thirdly, we should create an empty window and resize it for displaying the recording in real-time.

cv2.namedWindow("MyLive", cv2.WINDOW_NORMAL)
cv2.resizeWindow("MyLive", 460, 260)

4. Fourthly, for screen recording we will take screenshots of each frame and will write it to the video file . Then, we have to display the screen recording.

img = pyautogui.screenshot()  
frame = np.array(img)     
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)  
out.write(frame) 
cv2.imshow('MyLive', frame)  
if cv2.waitKey(1) == ord('q'):  
    break

5. Finally, we will release the video writer and destroy all the windows, opened by OpenCV.

 out.release()
 cv2.destroyAllWindows() 

Source code: 

import pyautogui 
import cv2 
import numpy as np 
 
resolution = (1920, 1080) 
codec = cv2.VideoWriter_fourcc(*"XVID") 
filename = "MyRecording.avi"
fps = 60.0
out = cv2.VideoWriter(filename, codec, fps, resolution) 
 
cv2.namedWindow("MyLive", cv2.WINDOW_NORMAL) 
cv2.resizeWindow("MyLive", 460, 260) 
 
while True: 
    img = pyautogui.screenshot() 
    frame = np.array(img) 
    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) 
    out.write(frame) 
    cv2.imshow('MyLive', frame) 
    if cv2.waitKey(1) == ord('q'): 
         break
out.release() 
cv2.destroyAllWindows()