Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
How to Build a Simple Auto-Login Bot with Python
Next article icon

Python is a widely used general-purpose language. It allows for performing a variety of tasks. One of them can be recording a video. It provides a module namedpyautogui which can be used for the same. This module along with NumPy and OpenCV provides a way to manipulate and save the images (screenshot in this case)

Modules needed

  • Numpy: To install Numpy type the below command in the terminal.
pip install numpy
  • pyautogui: To install pyautogui type the below command in the terminal.
pip install pyautogui
  • OpenCV: To install OpenCV type the below command in the terminal.
pip install opencv-python

Below is the implementation:

First, import all the required packages. 

Python3
# importing the required packagesimportpyautoguiimportcv2importnumpyasnp

Now, before recording the screen, we have to create a VideoWriter object. Also, we have to specify the output file name, Video codec, FPS, and video resolution. In video codec, we have to specify a 4-byte code (such as XVID, MJPG, X264, etc.). We'll be using XVID here.

Python3
# Specify resolutionresolution=(1920,1080)# Specify video codeccodec=cv2.VideoWriter_fourcc(*"XVID")# Specify name of Output filefilename="Recording.avi"# Specify frames rate. We can choose# any value and experiment with itfps=60.0# Creating a VideoWriter objectout=cv2.VideoWriter(filename,codec,fps,resolution)

Optional:To display the recording in real-time, we have to create an Empty window and resize it.

Python3
# Create an Empty windowcv2.namedWindow("Live",cv2.WINDOW_NORMAL)# Resize this windowcv2.resizeWindow("Live",480,270)

Now, let's start recording our screen. We will be running an infinite loop and in each iteration of the loop, we will take a screenshot and write it to the output file with the help of the video writer.

Python3
whileTrue:# Take screenshot using PyAutoGUIimg=pyautogui.screenshot()# Convert the screenshot to a numpy arrayframe=np.array(img)# Convert it from BGR(Blue, Green, Red) to# RGB(Red, Green, Blue)frame=cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)# Write it to the output fileout.write(frame)# Optional: Display the recording screencv2.imshow('Live',frame)# Stop recording when we press 'q'ifcv2.waitKey(1)==ord('q'):break

After everything is done, we will release the writer and destroy all windows opened by OpenCV.

Python3
# Release the Video writerout.release()# Destroy all windowscv2.destroyAllWindows()

Full Code:

Python3
# importing the required packagesimportpyautoguiimportcv2importnumpyasnp# Specify resolutionresolution=(1920,1080)# Specify video codeccodec=cv2.VideoWriter_fourcc(*"XVID")# Specify name of Output filefilename="Recording.avi"# Specify frames rate. We can choose any# value and experiment with itfps=60.0# Creating a VideoWriter objectout=cv2.VideoWriter(filename,codec,fps,resolution)# Create an Empty windowcv2.namedWindow("Live",cv2.WINDOW_NORMAL)# Resize this windowcv2.resizeWindow("Live",480,270)whileTrue:# Take screenshot using PyAutoGUIimg=pyautogui.screenshot()# Convert the screenshot to a numpy arrayframe=np.array(img)# Convert it from BGR(Blue, Green, Red) to# RGB(Red, Green, Blue)frame=cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)# Write it to the output fileout.write(frame)# Optional: Display the recording screencv2.imshow('Live',frame)# Stop recording when we press 'q'ifcv2.waitKey(1)==ord('q'):break# Release the Video writerout.release()# Destroy all windowscv2.destroyAllWindows()

Output:

Code Explanation:

  1. The code first imports the required packages: PyAutoGUI, cv2, and numpy.
  2. The next line specifies the resolution of the video recording: (1920, 1080).
  3. Next, we specify the video codec to be used: XVID.
  4. We then create a VideoWriter object named out and set its properties as follows: filename, codec, fps (frames per second), resolution.
  5. We then create an Empty window named Live and resize it to have a width of 480 pixels and a height of 270 pixels.
  6. We also make sure that it is displayed in front of all other windows on the computer screen.
  7. We keep this window open while we run the rest of the code.
  8. Next we use PyAutoGUI to take a screenshot using img as input.
  9. The output image frame is stored in frame variable.
  10. The image is converted from BGR format to RGB format before being written to out using write().
  11. If you want to see what's happening inside pyautogui while taking screenshots, you can enable debug mode by setting DEBUG = True in pyautogui configuration file like so: DEBUG = True .
  12. Then when you press F5 key or run python main_program.py -d ,
  13. The code imports the required packages and sets up some basic parameters.
  14. The resolution parameter specifies the width and height of the output file, while the codec parameter specifies the video codec to be used.
  15. The next step is to create an instance of the VideoWriter class.
  16. This object will take care of writing the recorded video to an output file.
  17. The fps parameter specifies how often the video should be recorded at, while the filename and resolution parameters specify where and in what format the recording should be saved.
  18. Once everything is set up, we can start recording by issuing a call to out.write().
  19. This method takes as input a numpy array representing the current frame of video footage.
  20. Once written to disk, we can stop recording by pressing 'q'.

Create a Screen recorder using Python
Improve
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp