Introduction
The value of a screen recorder is known very well by those who like to share with the people something informative through live screen demonstration.
Tech YouTubers often use it. I even use a screen recorder to show the output of each project or program.
There are many applications available to record live screens. But this time we'll create our own, with the help of our lovely programming language.
In this tutorial, I will show, how you can create a Screen Recorder using Python.
You just need to install two python libraries(OpenCV, NumPy), and one module, pyautogui to record your screen through your own recorder application.
☛Visit Also: Send Messages Secretly using Python - The Art of Cryptography
Requirements and Installation
OpenCV: pip install opencv-python
NumPy: pip install numpy
PyAutoGUI: pip install PyAutoGUI
A Message for Linux Users
Scrot must be installed to use screenshot functions in Linux. Otherwise, the program will give "NotImplementedError".
Simply run this command to avoid the error.
☛Install scrot: sudo apt install scrot
How to use this Screen Recorder?
1. Run the Python Code.
2. Select the starting point of the region of the recording area(Place your mouse pointer there, then press ENTER).
3. Type the name of the file you want to save as output video.
4. Select the fps for capturing the video frame.
Next, Your screen recorder will start recording.
Import the modules
Let's create a python file with this name 'recorder.py' and start writing your code by importing these modules.
import cv2
import numpy as np
import pyautogui
Select the Starting Point
Create a message box to display a message for selecting starting point of the recording area. You just need to place their mouse pointer at the location from where you want to capture the screen.
Then, simply press Enter key and the program will get the current mouse location and print that.
pyautogui.alert('Place your mouse pointer at the Starting Point, then press ENTER.')
# Origin of the Startig Point.
startMouseX, startMouseY = pyautogui.position()
print(startMouseX, startMouseY)
Screen and Video Options
The next code will get the screen size through pyautogui.size() and display two message boxes for getting the file name(To save the output video) and fps of the video from the user.
# Resolution of the Screen
scr_width, scr_height = pyautogui.size()
# Video codec
fourcc = cv2.VideoWriter_fourcc(*"XVID")
# Name of the output video file
F_Name = pyautogui.prompt('Enter the file name: ')
F_Name = F_Name + '.avi'
# Frame rates.
fps = pyautogui.confirm('Select FPS: ', buttons=['3','5','10','15','20','25','30'])
Video Writer and Demo Window
Create a video writer object here and set a demo window for showing the live recording.
# Create a VideoWriter object
output = cv2.VideoWriter(F_Name, fourcc, int(fps), (scr_width, scr_height))
# Create a manually resizable opencv window
cv2.namedWindow("Demo", cv2.WINDOW_NORMAL)
# Resize the window
cv2.resizeWindow("Demo", 320, 240)
Continue Recording
Now we need to declare an infinite loop to take the screenshot of the screen from the selected region and save that in the video writer object('output').
A small live window will show at the left side of the screen. It will let the user know that the recording is ongoing.
The user has to press the 'q' button to stop recording and exit.
I have declared a single comment line before each line. It will help you to understand the objectives of the code better.
while True:
# Take screenshot
image = pyautogui.screenshot(region=(startMouseX, startMouseY, \
scr_width, scr_height))
# Converting screenshot to a numpy array
frame = np.array(image)
# Converting the image BGR color space to RGB
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# Write the image to the output file
output.write(frame)
# Display the recording screen for demonstration
cv2.imshow('Demo', frame)
# Press 'q' to stop recording
if cv2.waitKey(1) == ord('q'):
break
Release Video Writer and all windows
Don't forget to close all windows after the operation completed. Add this piece of code at the very end of your program.
# Release the Video writer
output.release()
# Destroy all windows
cv2.destroyAllWindows()
☛Visit Also: Censor Bad Words🤬 from a text - Profanity Filter in Python
Conclusion
In this tutorial, we build our own screen recorder using python. We used PyAutoGUI, OpenCV, and NumPy to create this python program.
We make it user-friendly by giving the options to the users to select the filename, fps of the video, and the region for the recording area.
Try it with your own and let me know how it's working. For any issue, leave your comment below; you will get an immediate response.
Thanks for reading!💙
PySeek