Created
June 1, 2022 03:16
-
-
Save zjplab/cd14dc23b4755a16420101503c8cd887 to your computer and use it in GitHub Desktop.
毫秒级录屏
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
毫秒级录屏 | |
按Backspace键停止录屏 | |
''' | |
from PIL import ImageGrab | |
import numpy as np | |
import cv2 | |
import datetime | |
from pynput import keyboard | |
import threading | |
import os | |
flag=False #停止标志位 | |
def convert_avi_to_mp4(avi_file_path, output_name): | |
os.popen("ffmpeg -i ./{input} -ac 2 -b:v 2000k -c:a aac -c:v libx264 -b:a 160k -vprofile high -bf 0 -strict experimental -f mp4 {output}.mp4 & del {input}".format(input = avi_file_path, output = output_name)) | |
return True | |
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, 30, (a, b)) # 输出文件命名为test.mp4,帧率为16,可以自己设置 | |
# font | |
font = cv2.FONT_HERSHEY_SIMPLEX | |
# org | |
org = (50, 50) | |
# fontScale | |
fontScale = 2 | |
# Blue color in BGR | |
color = (0, 0, 255) | |
# Line thickness of 2 px | |
thickness = 4 | |
while True: | |
im = ImageGrab.grab() | |
imm=cv2.cvtColor(np.array(im), cv2.COLOR_RGB2BGR)#转为opencv的BGR格式 | |
imm=cv2.putText(imm, datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3], org, font, fontScale, color, thickness, cv2.LINE_AA) | |
video.write(imm) | |
if flag: | |
print("录制结束!") | |
break | |
video.release() | |
convert_avi_to_mp4('%s.avi'%name, '%s'%name) | |
def on_press(key): | |
""" | |
键盘监听事件!!! | |
:param key: | |
:return: | |
""" | |
#print(key) | |
global flag | |
if key == keyboard.Key.backspace: | |
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment