Screen Recorder using Python and OpenCV


Want to record your screen to make a video tutorial for Youtube and can't find any good software for your need. Hey dude, you are a programmer. Why don't you code one by yourself? So let's make a very simple Python script to record the screen.

Screen Recorder using Python and OpenCV


The main agenda of this tutorial is not to build a screen recorder program and release it into the market. Our primary focus will on learning how to use Python to develop solutions for real-world problems. Since we are building a screen recorder in Python, I  think it is better to write a script that will let you learn the basics of working with images and videos.




This is the algorithm, I made for this screen recorder:

1. Create a directory to temporarily store the screenshots.
2. Take screenshots and save them in the directory.
3. Then, on a specific keypress, this process of taking screenshots is killed, which means the recording is stopped.
4. Images in the directory are sorted.
5. These images are combined to form the screencast.
6. At last the directory with all the screenshots is deleted.


With that been said, let's dive directly into the code.


screen_recorder_python.py


Start by importing the necessary modules. You might be familiar with modules like cv2, numpy, and os, so let me introduce you to the other two. mss is an ultra-fast cross-platform multiple screenshots module in pure python using ctypes and the shutil module offers several high-level operations on files and collections of files. In particular, functions are provided which support file copying and removal.

import numpy
import os
import mss
import shutil
import cv2


Specify a directory to temporarily store the screenshots. You can change the region of the screen you want to record. mss will take screenshots and OpenCV will save these in the following directory with different filenames.

os.mkdir(directory)
directory = "C:\\Users\Cyril Tom Mathew\Desktop\cap_faces"
 
try:
    while True:
        with mss.mss() as sct:
             monitor = {"top": 0, "left": 0, "width": 1280, "height": 720}
             i=0
                while 1:

                     img = numpy.array(sct.grab(monitor))
                  cv2.imwrite("C:\\Users\Cyril Tom Mathew\Desktop\cap_faces\%s.png" %str(i), img)
                   i+=1


This will stop the recording operation when ctrl+c is pressed on the keyboard.


except KeyboardInterrupt:
    pass


This function is used to create video files with OpenCV.

out = cv2.VideoWriter("screencast.avi",cv2.VideoWriter_fourcc(*'DIVX'), 5, (1280,720))
 
You need to provide the filename, FourCC code which is a sequence of four bytes used to uniquely identify data formats, frames per second, the height and width of the video(make sure that it is the same as the values provided in the dictionary) file as arguments, like this cv2.VideoWriter('filename.avi',fourcc,video_fps, (video_wdth,video_hight)). Remember that you need to adjust the frame rate for better results.


Here we are numerically sorting the screenshots since I have saved these images with filenames as numbers in the ascending order, '1.png',  '2png', '3.png' etc. Sorting is very important because the initially taken screenshots must be the first frames of the video and we do this to arrange those images in the order in which they are created.

 drfiles = os.listdir(directory)
 drfiles.sort(key=lambda f: int(''.join(filter(str.isdigit, f))))


We loop through all the images in the directory, open then in order and write them into a video file.


for filename in drfiles:
    frame = cv2.imread(os.path.join(directory, filename))
     out.write(frame)

Finally, to use this program multiple times we need to delete the directory and also we don't need the screenshots anymore.


shutil.rmtree(directory) 
out.release()
 print("Screen Recorded Successfully")


That's it guys, let's run the code and check the results.

screen recorder in python
Screencast


Oh Yeah...Here is the Screencast. We have made a fully functional screen recorder in Python.

I hope you found this useful and if you have any trouble implementing this or if you need any help, feel free to comment below.


Thank You.
Screen Recorder using Python and OpenCV Screen Recorder using Python and OpenCV Reviewed by Cyril Tom Mathew on July 17, 2019 Rating: 5

No comments:

Powered by Blogger.