|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +""" |
| 4 | +@author: 闲欢 |
| 5 | +""" |
| 6 | + |
| 7 | +# coding:utf-8 |
| 8 | +import time, threading |
| 9 | +from datetime import datetime |
| 10 | +from PIL import ImageGrab |
| 11 | +import numpy as np |
| 12 | +from cv2.cv2 import VideoCapture, VideoWriter_fourcc, VideoWriter, cvtColor, CAP_PROP_FPS, CAP_PROP_FRAME_COUNT, \ |
| 13 | + CAP_PROP_FRAME_WIDTH, CAP_PROP_FRAME_HEIGHT, COLOR_RGB2BGR |
| 14 | +from pynput import keyboard |
| 15 | + |
| 16 | + |
| 17 | +# 录入视频 |
| 18 | +def video_record(sttime): |
| 19 | + global name |
| 20 | + # 当前的时间(当文件名) |
| 21 | + name = datetime.now().strftime('%Y-%m-%d %H-%M-%S') |
| 22 | + # 获取当前屏幕 |
| 23 | + screen = ImageGrab.grab() |
| 24 | + # 获取当前屏幕的大小 |
| 25 | + width, high = screen.size |
| 26 | + # MPEG-4编码,文件后缀可为.avi .asf .mov等 |
| 27 | + fourcc = VideoWriter_fourcc('X', 'V', 'I', 'D') |
| 28 | + # (文件名,编码器,帧率,视频宽高) |
| 29 | + video = VideoWriter('%s.avi' % name, fourcc, 15, (width, high)) |
| 30 | + print(str(sttime) + '秒后开始录制----') |
| 31 | + time.sleep(int(sttime)) |
| 32 | + print('开始录制!') |
| 33 | + global start_time |
| 34 | + start_time = time.time() |
| 35 | + while True: |
| 36 | + if flag: |
| 37 | + print("录制结束!") |
| 38 | + global final_time |
| 39 | + final_time = time.time() |
| 40 | + # 释放 |
| 41 | + video.release() |
| 42 | + break |
| 43 | + # 图片为RGB模式 |
| 44 | + im = ImageGrab.grab() |
| 45 | + # 转为opencv的BGR模式 |
| 46 | + imm = cvtColor(np.array(im), COLOR_RGB2BGR) |
| 47 | + # 写入 |
| 48 | + video.write(imm) |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | +# 监听按键 |
| 53 | +def on_press(key): |
| 54 | + global flag |
| 55 | + if key == keyboard.Key.esc: |
| 56 | + flag = True |
| 57 | + # 返回False,键盘监听结束! |
| 58 | + return False |
| 59 | + |
| 60 | + |
| 61 | + |
| 62 | +# 视频信息 |
| 63 | +def video_info(): |
| 64 | + # 记得文件名加格式不要错! |
| 65 | + video = VideoCapture('%s.avi' % name) |
| 66 | + fps = video.get(CAP_PROP_FPS) |
| 67 | + count = video.get(CAP_PROP_FRAME_COUNT) |
| 68 | + size = (int(video.get(CAP_PROP_FRAME_WIDTH)), int(video.get(CAP_PROP_FRAME_HEIGHT))) |
| 69 | + print('帧率=%.1f' % fps) |
| 70 | + print('帧数=%.1f' % count) |
| 71 | + print('分辨率', size) |
| 72 | + print('视频时间=%.3f秒' % (int(count) / fps)) |
| 73 | + print('录制时间=%.3f秒' % (final_time - start_time)) |
| 74 | + print('推荐帧率=%.2f' % (fps * ((int(count) / fps) / (final_time - start_time)))) |
| 75 | + |
| 76 | + |
| 77 | +if __name__ == '__main__': |
| 78 | + flag = False |
| 79 | + print("工具使用:输入1-9秒必须为整数的延迟时间,点击esc按钮结束录屏") |
| 80 | + sstime = input("请输入多少秒后开始录制(1-9秒)必须为整数:", ) |
| 81 | + th = threading.Thread(target=video_record, args=sstime) |
| 82 | + th.start() |
| 83 | + with keyboard.Listener(on_press=on_press) as listener: |
| 84 | + listener.join() |
| 85 | + # 等待视频释放过后 |
| 86 | + time.sleep(1) |
| 87 | + video_info() |
0 commit comments