#要求安装pillow opencv-python两个库
#python + opencv 实现屏幕录制
from PIL import ImageGrab
import numpy as np
import cv2
screen = ImageGrab.grab()#获得当前屏幕
length,width=screen.size#获得当前屏幕的大小
video_decode_style = cv2.VideoWriter_fourcc(*'XVID')#编码格式
filename = input("Please input filename, aaa.avi for esample:") #get the filename from input
video = cv2.VideoWriter(filename, video_decode_style, 12, (length, width))#输出文件命名为a.mp4,帧率为32,可以调节
print("screen record is doing, press <ctrl+c> to stop........")
while True:
im = ImageGrab.grab()
imm=cv2.cvtColor(np.array(im), cv2.COLOR_RGB2BGR)#转为opencv的BGR格式
video.write(imm)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
video.release()
cv2.destroyAllWindows()