Screen recording enables you to create demonstration videos, record gaming achievements and create videos that can be shared online on social media. Many industrial softwares exist out there that can help you do that very easily though. In this tutorial, you will learn how to make your own simple screen recorder in Python that you can further extend to your own needs.
Let's get started, first, install the required dependencies for this tutorial:
pip3 install numpy opencv-python pyautogui
The process is as follows:
Importing necessary modules:
import cv2 import numpy as np import pyautogui
Let's initialize the format we gonna use to write our video file ( named "output.avi" ):
# display screen resolution, get it from your OS settings SCREEN_SIZE = (1920, 1080) # define the codec fourcc = cv2.VideoWriter_fourcc(*"XVID") # create the video write object out = cv2.VideoWriter("output.avi", fourcc, 20.0, (SCREEN_SIZE))
Note: You need to get the correct
SCREEN_SIZE from your operating system, that is the screen resolution, otherwise writing to the file won't work (alternatively, you can use
pyautogui.size() function to get the size of the primary monitor).
fourcc is the video codec library that OpenCV will use to write the video file, we specified XVID here. The 20.0 float value passed as third parameter to cv2.VideoWriter corresponds to the FPS (Frame Per Second).
Now we need to keep capturing screenshots and writing to the file in a loop until the user clicks the "q" button, here is the main loop for that:
while True: # make a screenshot img = pyautogui.screenshot() # convert these pixels to a proper numpy array to work with OpenCV frame = np.array(img) # convert colors from BGR to RGB frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # write the frame out.write(frame) # show the frame cv2.imshow("screenshot", frame) # if the user clicks q, it exits if cv2.waitKey(1) == ord("q"): break # make sure everything is closed when exited cv2.destroyAllWindows() out.release()
First, we use the screenshot() function which returns an image object, so we need to convert it to a proper numpy array. After that, we need to convert that frame into RGB, that's because OpenCV uses BGR by default.
As mentioned in pyautogui's official documentation, you can also record only regions of your screen, by passing region keyword argument which is a four-integer tuple representing the top, left, width and height of the region to capture, here is how it's done:
img = pyautogui.screenshot(region=(0, 0, 300, 400))
After you are done with recording, just click "q", it will destroy the window and finish writing to the file, try it out!
Also, you can replace the
while True statement with a
for loop as follows:
for i in range(200): # make a screenshot img = pyautogui.screenshot() # the rest of the code...
This will record your screen for 10 seconds, that's because we set the FPS to 20 (which makes sense because 200 is 20 times 10)
Alright, there are endless of ideas you can use to extend this. For example, you can combine this with an audio recorder, and you'll come up with a Python tool that records your screen and voice simultaneously, you will need to use a thread that records audio and another for the screen recorder, let us know your progress in the comments below !
Or you can create keyboard shortcuts that starts, pauses and stops recording, this tutorial can help you.
Finally, many of the Python concepts aren't discussed in detail here, if you feel you want to dig more to Python, I highly suggest you get one of these amazing courses:
Learn also: How to Download All Images from a Web Page in Python.
Happy Coding ♥View Full Code