"""
python 屏幕录制改进版,无opencv黑框显示!
"""
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
))
while True:
im
= ImageGrab
.grab
()
imm
= cv2
.cvtColor
(np
.array
(im
), cv2
.COLOR_RGB2BGR
)
video
.write
(imm
)
if flag
:
print("录制结束!")
break
video
.release
()
def on_press(key
):
"""
键盘监听事件!!!
:param key:
:return:
"""
global flag
if key
== keyboard
.Key
.esc
:
flag
= True
print("stop monitor!")
return False
if __name__
== '__main__':
th
= threading
.Thread
(target
=video_record
)
th
.start
()
with keyboard
.Listener
(on_press
=on_press
) as listener
:
listener
.join
()
转载请注明原文地址:https://tech.qufami.com/read-15320.html