python录屏


#录制屏幕30秒
import pyautogui
import cv2
import numpy as np

# Set the recording resolution and framerate
resolution = (1920,1080)
fps = 30

# Start recording the screen
#screen_capture = pyautogui.screenshot()
# Convert the screenshot to a numpy array
#screen_capture = np.array(screen_capture)
# Create a VideoWriter object to save the video
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter('output.mp4', fourcc, fps, resolution)

# Record the screen for 10 seconds
for i in range(10 * fps):
   # Capture a screenshot
   screen_capture = pyautogui.screenshot()
   # Convert the screenshot to a numpy array
   screen_capture = np.array(screen_capture)
   # Write the numpy array to the video file
   out.write(screen_capture)

# Release the VideoWriter object and close the video file
out.release()
cv2.destroyAllWindows()
print("saved to output.mp4....")