DEV Community

Cover image for Screen Recording using python
Aditya Chaudhary👨‍💻
Aditya Chaudhary👨‍💻

Posted on

Screen Recording using python

In this article we are going to take a look at how we can record our screen using Python. Before getting into it, one should firstly understand what actually a video is. Basically, a video is nothing but a sequence of images (called frames) displayed at a given frequency.

So a workaround to make a screen recorder would be to capture each frame of the screen and write it as a video file. To do this python comes with handy libraries. For the screen recording we will be using OpenCV, PyAutoGUI, and Numpy.

So let's get started!
Make sure you have all the above mentioned necessary libraries installed.

  1. OpenCV : To install OpenCV, run pip install opencv-python
  2. PyAutoGui : To install PyAutoGui, run pip install pyautogui
  3. Numpy : To install Numpy, run pip install numpy You are all set!

First step is to import all the modules needed.

import cv2 #OpenCV
import pyautogui
import numpy as np
Enter fullscreen mode Exit fullscreen mode

The next step is to specify video codec using fourCC. It is a 4 byte code used for specifying the video codec. For the sake of simplicity we can pass the FourCC code for AVI video format which is *"XVID"

codec = cv2.VideoWriter_fourcc(*"XVID")
Enter fullscreen mode Exit fullscreen mode

In the next step, we will be creating a videoWriter object.
The video writer method of cv2 module needs 4 mandatory arguments.
First being the filename, second being the codec, third being the FPS (Frame per second) and the last being the video (or Screen) resolution. For a better screen recording 60 FPS is a good frame rate. You can experiment around the FPS value to get the optimum results.

out = cv2.VideoWriter("Recorded.avi", codec , 60, (1366, 768)) #Here screen resolution is 1366 x 768, you can change it depending upon your need
Enter fullscreen mode Exit fullscreen mode

The next step is to implicitly create a window using namedWindow() method with the WINDOW_NORMAL flag so that the user can resize the window depending upon the need.

cv2.namedWindow("Recording", cv2.WINDOW_NORMAL)
cv2.resizeWindow("Recording", 480, 270) #Here we are resizing the window to 480x270 so that the program doesn't run in full screen in the beginning
Enter fullscreen mode Exit fullscreen mode

Now we need to capture screenshot of each frame of the screen, write it to the video file and display the screen being recorded.
Here is the code snippet:

while True:
    img = pyautogui.screenshot() #capturing screenshot
    frame = np.array(img) #converting the image into numpy array representation 
    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) #converting the BGR image into RGB image
    out.write(frame) #writing the RBG image to file
    cv2.imshow('Recording', frame) #display screen/frame being recorded
    if cv2.waitKey(1) == ord('q'): #Wait for the user to press 'q' key to stop the recording
        break

out.release() #closing the video file
cv2.destroyAllWindows() #destroying the recording window
Enter fullscreen mode Exit fullscreen mode

Notice that the screenshot captured is in BGR format but in order to write the frame to the video file we need to convert it into RGB format using cvt.Color() method with COLOR_BGR2RBG flag. The condition if cv2.waitKey(1) == ord('q') waits for the user to press 'q' key to quit the recording. Lastly we have closed the video file and destroyed the recording window by release() and destroyAllWindows() method respectively.
PS. Linux users please don't be happy after hearing "Destroy all Windows" ;)

You are all done!
It's time to test the results.

Normal sized window :
Normal Sized window

Maximized window: (Horrible infinite loop)
Alt Text

Video Demonstration:

Thanks for reading!

Top comments (6)

Collapse
 
claudiofinizio profile image
Claudio Finizio

Very well explained and for me it worked; you really deserve kudos :-)

Only ownside, on my machine the video recorded is then played at very high speed, I need to solve this but I suppose it is another topic

Collapse
 
veerkoti profile image
Veeresh Koti

Hi
Is there a way to downscale the resolution?
My 1080p screen recording is 109 MB for 5 minutes of video.
And cv2.resize() is not working.
I'm getting an output file which is 1KB.
Can you please help me with this?

Collapse
 
ttechprenuers profile image
TheTechprenuers

I am deleting obs now 😉

Collapse
 
itsaditya profile image
Aditya Chaudhary👨‍💻

Haha.. No need.. OBS is better... But yeah when it comes to low end PC, bandicam or my recorder would perform great

Collapse
 
spiritupbro profile image
spiritupbro

man that was cool

Collapse
 
itsaditya profile image
Aditya Chaudhary👨‍💻

Thank you :)