Python 练习之 屏幕录像

# Time: 2020/06/16

#Author: Xiaohong

# 运行环境: OS: Windows 10

#  Python: 3.7

# 功能: 屏幕录像

主界面:

主文件(Tl_C02.py)

import sys
import time
import os
from PyQt5.QtWidgets import *
from PyQt5 import QtGui, QtCore, QtWidgets
# from PyQt5.QtWidgets import QTableWidget, QProgressBar, QLineEdit, QComboBox, QFrame, QTableWidgetItem
from PyQt5.QtCore import *
from PyQt5.QtGui import *
# 这个test_pyqt是ui文件对应的py文件的文件名
from Ui_qt_C02 import Ui_qt_c02

from PIL import ImageGrab  # pip3 install pillow
import numpy as np    # pip3 install numpy
import cv2             # pip3 install opencv2
import datetime
from pynput import keyboard   # pip3 install pynput
import threading

flag = False  # 停止标志位

# 调用 QT 设计的界面对应的类


class WC02(QDialog):
    def __init__(self, parent=None):
        super(WC02, self).__init__(parent)
        self.child = Ui_qt_c02()
        self.child.setupUi(self)
        self.child.btn_start.clicked.connect(self.start_screen)
        self.child.btn_select.clicked.connect(self.sel_directory)
        self.child.btn_open_path.clicked.connect(self.open_path)

    # 设立函数,来取得当前时间

    def get_sysdate(self):
        now = time.strftime("%Y%m%d %H%M%S", time.localtime(time.time()))
        # print(now)
        return now

    def sel_directory(self):
        pass
        #     打开文件有以下3种:
        # 1、单个文件打开 QFileDialog.getOpenFileName()
        # 2、多个文件打开 QFileDialog.getOpenFileNames()
        # 3、打开文件夹 QFileDialog.getExistingDirectory()
        dir_path = QFileDialog.getExistingDirectory(self, "请选择文件夹路径", "/")
        # print(dir_path)
        self.child.t_directory.setText(dir_path)        

    def video_record(self):
        # """
        # 屏幕录制!
        # :return:
        # """
        name = self.get_sysdate()+'.avi'   #当前的时间
        t_directory = self.child.t_directory.text()   #新生成文件, 要保存的文件夹
        if t_directory == '':
            pass
        else:
            new_file_name = os.path.join(t_directory, name)   #新生成的文件,含路径
            # print(new_file_name)

            p = ImageGrab.grab()  # 获得当前屏幕
            a, b = p.size  # 获得当前屏幕的大小
            fourcc = cv2.VideoWriter_fourcc(*'XVID')  # 编码格式   常用的有 “DIVX"、”MJPG"、“XVID”、“X264"
            # 输出文件 帧率为16,可以自己设置
            # VideoWriter(filename, fourcc, fps, frameSize[, isColor]) -> <VideoWriter object>
            # 第一个参数是要保存的文件的路径
            # fourcc 指定编码器
            # fps 要保存的视频的帧率,本例为 20fps (frame per second)
            # frameSize 要保存的文件的画面尺寸,本例为 当前屏幕的大小
            # isColor 指示是黑白画面还是彩色的画面            
            video = cv2.VideoWriter(new_file_name, fourcc, 20, (a, b))
            while True:
                im = ImageGrab.grab()
                # CvtColor是Opencv里的 转换图像的颜色空间
                # cvtColor(src, code, dst=None, dstCn=None)
                # src: 原图像; code: 指定颜色空间转换类型;
                # dst: 目标图像;与原图像大小深度一致;dstCn: 指定目标图像通道数;默认None,则会根据src、code自动计算;
                # https://www.cnblogs.com/chenzhen0530/p/10741264.html 有说明
                # 转为opencv的BGR格式
                imm = cv2.cvtColor(np.array(im), cv2.COLOR_RGB2BGR)
                video.write(imm)
                if flag:
                    print("录制结束!")
                    break
            video.release()

    def on_press(self, key):
        # """
        # 键盘监听事件!!!
        # :param key:
        # :return:
        # """
        # print(key)
        global flag
        if key == keyboard.Key.esc:
            flag = True
            print("stop monitor!")
            return False  # 返回False,键盘监听结束!

    def start_screen(self):
        pass
        reply = QMessageBox.information(
            self, '提醒', '准备开始录制... 按ESC 结束录制', QMessageBox.Yes, QMessageBox.Yes)
        th = threading.Thread(target=self.video_record)
        th.start()
        with keyboard.Listener(on_press=self.on_press) as listener:
            listener.join()
        reply = QMessageBox.information(
            self, '提醒', '已录制完毕', QMessageBox.Yes, QMessageBox.Yes)

    def open_path(self):
        dir_path = self.child.t_directory.text()
        if os.path.exists(dir_path):
            os.startfile(dir_path)
        #     打开文件有以下3种:
        # 1、单个文件打开 QFileDialog.getOpenFileName()
        # 2、多个文件打开 QFileDialog.getOpenFileNames()
        # 3、打开文件夹 QFileDialog.getExistingDirectory()


if __name__ == "__main__":
    app = QApplication(sys.argv)
    myc02 = WC02()
    myc02.show()
    sys.exit(app.exec_())

QT5 的界面设计(qt_C02.ui) 

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>qt_c02</class>
 <widget class="QDialog" name="qt_c02">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>726</width>
    <height>300</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>录屏软件</string>
  </property>
  <widget class="QPushButton" name="btn_start">
   <property name="geometry">
    <rect>
     <x>70</x>
     <y>70</y>
     <width>121</width>
     <height>23</height>
    </rect>
   </property>
   <property name="text">
    <string>开始录屏</string>
   </property>
  </widget>
  <widget class="QPushButton" name="btn_exit">
   <property name="geometry">
    <rect>
     <x>70</x>
     <y>150</y>
     <width>121</width>
     <height>23</height>
    </rect>
   </property>
   <property name="text">
    <string>关闭程序</string>
   </property>
  </widget>
  <widget class="QLineEdit" name="t_directory">
   <property name="geometry">
    <rect>
     <x>110</x>
     <y>20</y>
     <width>371</width>
     <height>20</height>
    </rect>
   </property>
  </widget>
  <widget class="QLabel" name="label_2">
   <property name="geometry">
    <rect>
     <x>30</x>
     <y>20</y>
     <width>71</width>
     <height>16</height>
    </rect>
   </property>
   <property name="text">
    <string>文件夹路径</string>
   </property>
  </widget>
  <widget class="QPushButton" name="btn_select">
   <property name="geometry">
    <rect>
     <x>480</x>
     <y>20</y>
     <width>31</width>
     <height>23</height>
    </rect>
   </property>
   <property name="text">
    <string>...</string>
   </property>
  </widget>
  <widget class="QPushButton" name="btn_open_path">
   <property name="geometry">
    <rect>
     <x>70</x>
     <y>110</y>
     <width>121</width>
     <height>23</height>
    </rect>
   </property>
   <property name="text">
    <string>打开下载目录</string>
   </property>
  </widget>
 </widget>
 <resources/>
 <connections>
  <connection>
   <sender>btn_exit</sender>
   <signal>clicked()</signal>
   <receiver>qt_c02</receiver>
   <slot>close()</slot>
   <hints>
    <hint type="sourcelabel">
     <x>115</x>
     <y>89</y>
    </hint>
    <hint type="destinationlabel">
     <x>296</x>
     <y>142</y>
    </hint>
   </hints>
  </connection>
 </connections>
</ui>

 


版权声明:本文为leader_ww原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。