pip install numpy opencv-python pillow
import threading
import time
from selenium import webdriver
from PIL import ImageGrab
import numpy as np
import cv2
FPS = 20 # 录制帧数
def shot(dr):
screen = ImageGrab.grab()
height, width = screen.size
video = cv2.VideoWriter('video.avi', cv2.VideoWriter_fourcc(*'XVID'), FPS, (height, width))
while True:
capture = ImageGrab.grab() # 抓取屏幕
frame = cv2.cvtColor(np.array(capture), cv2.COLOR_RGB2BGR)
[video.write(frame) for i in range(15)] # 一次填15帧相同图像
try:
dr.title
except Exception as ex:
print(ex)
break
# cv2.imshow('imm', np.zeros((200,200), np.uint8))
# if cv2.waitKey(1) & 0xFF == ord('q'):
# break
video.release()
# cv2.destroyAllWindows()
dr = webdriver.Chrome()
t = threading.Thread(target=shot, args=(dr,)) # 新建线程
t.start() # 启动截图线程
dr.get('https://www.baidu.com')
time.sleep(1)
dr.find_element('id', 'kw').send_keys('简书 韩志超')
dr.find_element('id', 'su').click()
time.sleep(1)
dr.get('https://www.qq.com')
dr.back()
time.sleep(3)
dr.quit()
问题,录制出的视频依然像幻灯片一样。