Follow

Screen Recording using python

1. Import the required modules: cv2 and numpy are required to handle the video frames, and pyautogui is required to take screenshots of the screen.
2. Define the screen size: Set the screen size to the resolution of the screen that you want to record.
3. Define the FourCC code: The FourCC (Four Character Code) is a sequence of four bytes used to specify the video codec. In this case, we use the XVID codec.
4. Initialize the video writer: Create a VideoWriter object using the cv2.VideoWriter constructor and pass in the filename, FourCC code, frame rate, and screen size.
5. Start the loop for recording: In the loop, use the pyautogui.screenshot() function to take a screenshot of the screen, convert it to a format that OpenCV can handle using numpy, and write the frame to the video using the out.write() method.
6. Display the frame: Use cv2.imshow() to display the current frame in a window.
7. Check for quitting: Use cv2.waitKey(1) to wait for the 'q' key to be pressed, and if it is, break the loop.
8. Release the video writer and close windows: After quitting, use out.release() to release the video writer, and cv2.destroyAllWindows() to close all windows.

screen recoding

import cv2
import numpy as np
import pyautogui
screen_size = (1920, 1080)
fourcc = cv2.VideoWriter_fourcc(*"XVID")
out = cv2.VideoWriter("screen_recording.avi", fourcc, 20.0, screen_size)
while True:
img = pyautogui.screenshot()
frame = np.array(img)
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
out.write(frame)
cv2.imshow("Screen recording", frame)
key = cv2.waitKey(1)
if key == ord("q"):
break
out.release()
cv2.destroyAllWindows()


No comments:

Post a Comment

Tell us how you like it.