How to Create a Screen Recorder Using Python


· ·

As a programmer you can always utilize your skills by developing tools that can serve the same purpose as other software’s you need, like in this case a screen recorder. In this article we are going to make use of Python. Python performs a variety of tasks, one of them being able to create a screen recorder.

In this tutorial guide we will be using Python to create a screen recorder which can help you accomplish some common basic tasks just like any other software available out there. We will also be able to add some integrations to our app such as the webcam functionalities.

What is a Screen Recorder?

A screen recorder can enable one to create and showcase demo videos, record games, and create tutorial videos that can be shared online on social media platforms such as YouTube and Twitch. In the market today many software exists that can help you achieve all this easily though. In this tutorial, you will learn how to create your own screen recorder using Python that you can further extend to your own needs.

Python Modules Needed 

In order for us to have our application up and running, we will need to make use of some Python modules that will make it easier develop the application They include:

  • Numpy – This module will help us convert images captured into an array and pass them to openCV
  • openCV – It will enable us to save the captured images in a video format.
  • Pyautogui – to capture images on the screen.

Creating a Screen Recorder 

Now, before we get started with writing the code for our application, we will need to make sure that we have all necessary tools for this task. That is:

  • Python should be installed in our system.
  • A code editor to help us write our code – in this case any code editor between Pycharm, VS Code, Atom or any of your choice will be fine.

Having ensured that we have the first step completed now we can move ahead and begin with our development. Earlier we listed some of the modules that we will need to create our app, now let’s begin by first installing them into our system.

Step 1: Installing the modules

To install numpy, we will use the pip command on either the terminal or command prompt.

pip install numpy

Similarly to install pyautogui and opencv we make use of pip by executing the commands below: 

pip install pyautogui


pip install opencv-python
Step 2: Import installed modules

In order for us to use the modules that we have installed, we will need to import them. The code below allows us to do this. But first make sure that you do have a python file already created where we will be writing our code:

import cv2
import numpy as np
import pyautogui
Step 3: Set screen resolution

Depending on the screen size you are using you are most likely to assign different resolution parameters at this stage. It is also important to note that not everyone is likely to get or know the resolution of their screen, so using pyautogui.size() function will help us automatically return the correct height and width of the screen.

SCREEN_SIZE = tuple(pyautogui.size())

But still if you are able to get the screen resolution you can opt to use this method instead.

resolution = (1920, 1080)
Step 4: Create VideoWriter object

First we begin by specifying the video codec, this is basically the encoding format of the video that will be captured. Next we specify the format in which our output will be stored. In this case our preferred mode is .avi, you can set any format you are comfortable with like .mp4.

It’s also in this line that you can specify the path where you want to store the recorded file, in this case it will be stored in the same folder as our .py file. We also go ahead and set our fps value, in this case we assign it to 20.

Finally, we use the VideoCapture() function to activate the webcam and capture from it.

fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi', fourcc, 20.0, (SCREEN_SIZE))
webcam = cv2.VideoCapture(0)
Step 5: Capture the screen

Now what we need to do is keep capturing the screen and writing the file until the process is terminated by pressing q on the keyboard. This is made possible by this code block:

while True:
    # Capture the screen
    img = pyautogui.screenshot()

    # Convert the image into numpy array
    img = np.array(img)

    # Convert the color space from BGR to RGB
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

    _, frame =
    # Finding the width, height and shape of our webcam image
    fr_height, fr_width, _ = frame.shape
    # setting the width and height properties
    img[0:fr_height, 0: fr_width, :] = frame[0:fr_height, 0: fr_width, :]

    #cv2.imshow('frame', img)

    # Write the frame into the file 'output.avi'

    # Press 'q' to quit
    if cv2.waitKey(1) & 0xFF == ord('q'):
        print("Recording Stopped")

This code block begins by first making use of pyautogui.screenshot() which captures the image and returns an image object which is then converted into a numpy array by np.array().

We then convert the color format from BGR which is the default standard of openCV into RGB. We then capture input from our webcam/camera which is connected to the computer.

The cv2.imshow() function will enable you to see your recording in real-time, but if you would like to record “blindly” then you can disable this by commenting it out.

Finally this loop will finish upon pressing q.

Step 6: Destroy OpenCV windows

After everything is done and you have already stopped recording the screen. The video writer is released meaning it closes he video file and all the windows we created are destroyed

Step 7: Output

To run the above code, can be done via the terminal using the command:

python [filename]

This will start our app and the recording too. The output in return should be what’s on your screen, in my case attached below is my output:

Link to the code on GitHub.


We have created our own screen recorder which is also able to capture video input from the webcam. There is no limitation on the design of the app. We have endless ideas that can be integrated to extend the functionality of our app. 

Therefore, you can redesign it to meet your own specification and style, you can even add other interactive features like keyboard shortcuts, integrated audio recording functionality and more. But as for now that is it, go ahead and start using your own software.

Hillary ("Lary") Nyakundi is a Growing Developer, with great interest in technology, open-source and the Python programming language. He is also a technical writer aiming to help share knowledge with other developers through informative articles. Through this, he has been able to work with tech companies from the US, India and Kenya. His passion in the developer world led him to start a podcast, “Let’s Talk Developer” where he gets to connect with other developers, learn from them and share their stories to help inspire upcoming developers.


Click on a tab to select how you'd like to leave your comment

Leave a Comment

Your email address will not be published.

Skip to toolbar