Screen Recorder using Python
Hey there! In this tutorial, we will be learning to build a Screen Recorder using Python in PyCharm.
Python is a widely-used general-purpose language, that allows a variety of tasks to be performed. One of them is recording the display screen, that is, creating a screen recorder using the pyautogui module. Below attached is a simple demonstration of this functionality.
Firstly, open PyCharm and create a project titled Screen_Recorder. Then, open the terminal and type the below-listed commands to install the respective libraries:
pip install opencv-python pip install numpy pip install pyautogui pip install pywin32
- opencv-python: OpenCV packages for Python
- NumPy: Library that supports n-dimensional arrays along with a large collection of high-level mathematical functions to operate on these arrays
- pyautogui: Cross-platform GUI automation Python module, used to programmatically control the mouse and keyboard
- pywin32: Module encapsulating the Windows Win32 API
Then, within the main.py file in this project, type the below-specified code. Refer to the code’s comments for an explanation regarding the code.
# Import necessary libraries: import cv2 import numpy as np import pyautogui from win32api import GetSystemMetrics import time # Access screen width: w = GetSystemMetrics(0) # Access screen height: h = GetSystemMetrics(1) # Store screen dimensions within a tuple: dimension = (w, h) # Define codec -> FourCC is a 4-byte code used to specify the video codec fourcc = cv2.VideoWriter_fourcc(*"XVID") # VideoWriter -> This class provides C++ API for writing video files or image sequences # Constructor parameters-> video filename, video codec, video frame-rate(fps), screen dimensions output = cv2.VideoWriter("recording.mp4", fourcc, 20.0, dimension) # Access current system time: now = time.time() # Read screen recording duration via user input: # time() -> Returns the time as a floating point number expressed in seconds duration = int(input('Specify recording duration in seconds: ')) # Buffer time to ensure that the recorded video duration is as specified by user: # This is done because, code must be executed up till line #33, prior to recording initiation. duration += duration # Identify the time at which recording must stop: end_time = now + duration while True: # Take a screenshot: # screenshot() -> Returns an Image object img = pyautogui.screenshot() # Import image data into NumPy array: frame = np.array(img) # Use cvtColor() method to convert image from BGR to RGB color format: # This conversion ensures that the recording exactly resembles the content that had been recorded frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # Write the frame into the file 'recording.mp4': output.write(frame) # Access current system time: current_time = time.time() # Check if it is time to stop recording. If so, break out of while loop. if current_time>end_time: break # Release the capture output.release()
Once the code has been executed and the user has entered the desired video duration, the recording begins. Execution of the code terminates once the recording has been completed and the mp4 file has been created.
The recorded video can be found within this project as shown below.