Create Screen Recorder in Python:
We can use the PyCharm code editor for this example. If you do not know about it then follow this link- How to install PyCharm for Python and create a program in it. |
(1) Open the PyCharm code editor and create a new project in it. We named the project screen_recorder.py. To build a screen recorder in Python, we need to install some packages. For this open the terminal in PyCharm and install four packages i.e. opencv-python, pywin32, numpy, and pyautogui. You install all packages at the same time by simply typing in the terminal following command i.e. pip install opencv-python pywin32 numpy pyautogui and then press enter as shown below.

Note: If any package shows an error while using in the code, you can simply install the package by clicking on the red bulb which appears below the error and you can directly install the package from here as shown below.

(2) To create a screen recorder in Python write the following code as shown below.

Source Code: Do proper Code Indentation otherwise, the Source Code does not work and gives an error.
import cv2
import pyautogui
from win32api import GetSystemMetrics
import numpy as np
import time
width=GetSystemMetrics(0)
height=GetSystemMetrics(1)
dimension=(width,height)
x=cv2.VideoWriter_fourcc(*"XVID")
output=cv2.VideoWriter("A screen_recording.mp4",x,30.0,dimension)
start_time=time.time()
duration=10
end_time=start_time+duration
while True:
img=pyautogui.screenshot()
frame_1= np.array(img)
color=cv2.cvtColor(frame_1,cv2.COLOR_BGR2RGB)
output.write(color)
current_time=time.time()
if current_time > end_time:
break
output.release()
print("Screen Recorded Successfully......")
(3) Now the video is saved in the path where you have created the Python Project as shown below.

Comments (No)