# coding: utf-8
from PIL import ImageGrab
import numpy as np
import cv2
import datetime
fps = 30
start = 3 # 延时录制
end = 15 # 自动结束时间
curScreen = ImageGrab.grab() # 获取屏幕对象
height, width = curScreen.size
filename = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S") + ".avi"
video = cv2.VideoWriter(filename, cv2.VideoWriter_fourcc(*'XVID'), fps, (height, width))
imageNum = 0
while True:
imageNum += 1
captureImage = ImageGrab.grab() # 抓取屏幕
frame = cv2.cvtColor(np.array(captureImage), cv2.COLOR_RGB2BGR)
# 显示无图像的窗口
cv2.imshow('encoding,press"q" exit', np.zeros((1, 255), np.uint8))
# 控制窗口显示位置,方便通过按键方式退出
cv2.moveWindow('encoding,press"q" exit', height - 100, width - 100)
if imageNum > fps * start:
video.write(frame)
# 退出条件
if cv2.waitKey(50) == ord('q') or imageNum > fps * end:
break
video.release()
cv2.destroyAllWindows()
运行需要安装依赖:
pip install pillow
pip install pyautogui
pip install opencv-python
很简单的一个功能。多平台都可使用。