#coding:utf-8importtime,threadingfromdatetimeimportdatetimefromPILimportImageGrabimportnumpyasnpimportcv2frompynputimportkeyboarddefvideo_record(sttime): # 录入视频globalnamename=datetime.now().strftime('%Y-%m-%d %H-%M-%S') # 当前的时间(当文件名)screen=ImageGrab.grab() # 获取当前屏幕width, high=screen.size# 获取当前屏幕的大小fourcc=cv2.VideoWriter_fourcc('X', 'V', 'I', 'D') # MPEG-4编码,文件后缀可为.avi .asf .mov等video=cv2.VideoWriter('%s.avi'%name, fourcc, 15, (width, high)) # (文件名,编码器,帧率,视频宽高)print(str(sttime)+'秒后开始录制----') # 可选time.sleep(int(sttime)) print('开始录制!') globalstart_timestart_time=time.time() whileTrue: ifflag: print("录制结束!") globalfinal_timefinal_time=time.time() video.release() #释放breakim=ImageGrab.grab() # 图片为RGB模式imm=cv2.cvtColor(np.array(im), cv2.COLOR_RGB2BGR) # 转为opencv的BGR模式video.write(imm) #写入# time.sleep(5) # 等待5秒再次循环defon_press(key): # 监听按键globalflagifkey==keyboard.Key.esc: flag=True# 改变returnFalse# 返回False,键盘监听结束!defvideo_info(): # 视频信息video=cv2.VideoCapture('%s.avi'%name) # 记得文件名加格式不要错!fps=video.get(cv2.CAP_PROP_FPS) Count=video.get(cv2.CAP_PROP_FRAME_COUNT) size= (int(video.get(cv2.CAP_PROP_FRAME_WIDTH)), int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))) print('帧率=%.1f'%fps) print('帧数=%.1f'%Count) print('分辨率',size) print('视频时间=%.3f秒'%(int(Count)/fps)) print('录制时间=%.3f秒'%(final_time-start_time)) print('推荐帧率=%.2f'%(fps*((int(Count)/fps)/(final_time-start_time)))) if__name__=='__main__': flag=Falseprint("工具使用:输入1-9秒必须为整数的延迟时间,点击esc按钮结束录屏") sstime=input("请输入多少秒后开始录制(1-9秒)必须为整数:",) th=threading.Thread(target=video_record,args=sstime) th.start() withkeyboard.Listener(on_press=on_press) aslistener: listener.join() time.sleep(1) # 等待视频释放过后video_info()
测试PC的朋友不妨可以试试这款录屏工具,直接打包即可使用