python 螢幕錄製改進版,無opencv黑框顯示
阿新 • • 發佈:2020-12-08
# python 螢幕錄製改進版,無opencv黑框顯示! # @zhou 2020/1/29_ from PIL import ImageGrab import numpy as np import cv2 import datetime from pynput import keyboard import threading flag=False #停止標誌位 def video_record(): """ 螢幕錄製! :return: """ name = datetime.datetime.now().strftime('%Y-%m-%d %H-%M-%S') #當前的時間 p = ImageGrab.grab() # 獲得當前螢幕 a, b = p.size # 獲得當前螢幕的大小 fourcc = cv2.VideoWriter_fourcc(*'XVID') # 編碼格式 video = cv2.VideoWriter('%s.avi'%name, fourcc, 20, (a, b)) # 輸出檔案命名為test.mp4,幀率為16,可以自己設定 while True: im = ImageGrab.grab() imm=cv2.cvtColor(np.array(im), cv2.COLOR_RGB2BGR)#轉為opencv的BGR格式 video.write(imm) if flag: print("錄製結束!") break video.release() def on_press(key): """ 鍵盤監聽事件!!! :param key: :return: """ #print(key) global flag if key == keyboard.Key.esc: flag=True print("stop monitor!") return False #返回False,鍵盤監聽結束! if __name__=='__main__': th=threading.Thread(target=video_record) th.start() with keyboard.Listener(on_press=on_press) as listener: listener.join()