How to make your own Screen Recorder with Python - Python OpenCV Tutorial

   Idea


 Prerequisites


 Install Modules

  pip install pillow  

  pip install numpy  

  pip install opencv-python  

Type these commands one by one in your terminal: cmd, bash, etc.. and press enter (run).

Or you can install all of these at once by typing the below command and pressing enter...

  pip install pillow numpy opencv-python 

  Code

Import modules

from PIL import ImageGrab
import numpy as np
import cv2, datetime
from win32api import GetSystemMetrics
from screeninfo import get_monitors

Note: We import opencv module with the name cv2 in Python.

The logic


for m in get_monitors():
    width = str(m.width)
    height = str(m.height)

width = GetSystemMetrics(0)
height = GetSystemMetrics(1)

time_stamp = datetime.datetime.now().strftime('%Y-%m-%d %H-%M-%S')
file_name = f'{time_stamp}.mp4'

enc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
cap_vd = cv2.VideoWriter(file_name, enc, 20.0, (width, height))

def app():
    img = ImageGrab.grab(bbox=(0, 0, width, height))
    img_np = np.array(img)
    img_final = cv2.cvtColor(img_np, cv2.COLOR_BGR2RGB)
    cv2.imshow('Screen Capture', img_final)
    cap_vd.write(img_final)

while True:
    app()
    if cv2.waitKey(10) == ord('q'):
        break

 Output

Now your app is ready. Run the code and wait. Everything on the screen will be recorded. Press q on your keyboard to stop the recording. The recording is then saved to the directory where the file is.


 Summary

In this article you learnt how you can make your own screen recorder using Python. You learnt about the use of python pillow module and numpy. We made the screen recorder using opencv.

Thanks for reading. Hope you liked the idea and project. Do comment down you opinions. Also, share this project with your friends and programmers.


Comments