Create a Screen recorder using Python
Python is a widely used general-purpose language. It allows performing a variety of tasks. One of them can be recording a video. It provides a module named pyautogui which can be used for the same. This module along with NumPy and OpenCV provides the way to manipulate and save the images (screenshot in this case)
- Numpy: To install Numpy type the below command in the terminal.
pip install numpy
- pyautogui: To install pyautogui type the below command in the terminal.
pip install pyautogui
- OpenCV: To install OpenCV type the below command in the terminal.
pip install opencv-python
Below is the implementation.
First, import all the required packages.
Now, before recording the screen, we have to create a VideoWriter object. Also, we have to specify the output file name, Video codec, FPS, and video resolution. In video codec, we have to specify a 4-byte code (such as XVID, MJPG, X264, etc.). We’ll be using XVID here.
Optional: To display the recording in real-time, we have to create an Empty window and resize it.
Now, let’s start recording our screen. We will be running an infinite loop and in each iteration of the loop, we will take a screenshot and write it to the output file with the help of the video writer.
After everything is done, we will release the writer and destroy all windows opened by OpenCV.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course