导航

Qt Designer设计界面三,Qlabel播放视频文件示例

Posted on 2022-03-09 15:40  初之萌萌  阅读(1556)  评论(4编辑  收藏  举报

功能:

1.播放,停止播放,循环播放一个视频文件

缺点:窗口大小固定不可调,因为一拉伸窗口就会导致程序停止运行,无解

 

效果图:

 

 代码:

from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        if not MainWindow.objectName():
            MainWindow.setObjectName(u"MainWindow")
        MainWindow.resize(800, 600)
        self.play_tool = QAction(MainWindow)
        self.play_tool.setObjectName(u"play_tool")
        icon = QIcon()
        icon.addFile(u"play.png", QSize(), QIcon.Normal, QIcon.Off)
        self.play_tool.setIcon(icon)
        self.stop_tool = QAction(MainWindow)
        self.stop_tool.setObjectName(u"stop_tool")
        icon1 = QIcon()
        icon1.addFile(u"stop.png", QSize(), QIcon.Normal, QIcon.Off)
        self.stop_tool.setIcon(icon1)
        self.loop_tool = QAction(MainWindow)
        self.loop_tool.setObjectName(u"loop_tool")
        icon2 = QIcon()
        icon2.addFile(u"loop.png", QSize(), QIcon.Normal, QIcon.Off)
        self.loop_tool.setIcon(icon2)
        self.centralwidget = QWidget(MainWindow)
        self.centralwidget.setObjectName(u"centralwidget")
        self.verticalLayout_2 = QVBoxLayout(self.centralwidget)
        self.verticalLayout_2.setObjectName(u"verticalLayout_2")
        self.verticalLayout = QVBoxLayout()
        self.verticalLayout.setSpacing(0)
        self.verticalLayout.setObjectName(u"verticalLayout")
        self.verticalLayout.setContentsMargins(-1, -1, 0, -1)
        self.video_lbl = QLabel(self.centralwidget)
        self.video_lbl.setObjectName(u"video_lbl")

        self.verticalLayout.addWidget(self.video_lbl)


        self.verticalLayout_2.addLayout(self.verticalLayout)

        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QMenuBar(MainWindow)
        self.menubar.setObjectName(u"menubar")
        self.menubar.setGeometry(QRect(0, 0, 800, 23))
        self.menumenu = QMenu(self.menubar)
        self.menumenu.setObjectName(u"menumenu")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QStatusBar(MainWindow)
        self.statusbar.setObjectName(u"statusbar")
        MainWindow.setStatusBar(self.statusbar)
        self.toolBar = QToolBar(MainWindow)
        self.toolBar.setObjectName(u"toolBar")
        self.toolBar.setAutoFillBackground(False)
        self.toolBar.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)
        MainWindow.addToolBar(Qt.TopToolBarArea, self.toolBar)

        self.menubar.addAction(self.menumenu.menuAction())
        self.toolBar.addAction(self.play_tool)
        self.toolBar.addSeparator()
        self.toolBar.addAction(self.stop_tool)
        self.toolBar.addSeparator()
        self.toolBar.addAction(self.loop_tool)
        self.toolBar.addSeparator()

        self.retranslateUi(MainWindow)

        QMetaObject.connectSlotsByName(MainWindow)
    # setupUi

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(QCoreApplication.translate("MainWindow", u"\u64ad\u653e\u89c6\u9891", None))
        self.play_tool.setText(QCoreApplication.translate("MainWindow", u"\u64ad\u653e", None))
#if QT_CONFIG(tooltip)
        self.play_tool.setToolTip(QCoreApplication.translate("MainWindow", u"\u64ad\u653e", None))
#endif // QT_CONFIG(tooltip)
        self.stop_tool.setText(QCoreApplication.translate("MainWindow", u"\u505c\u6b62\u64ad\u653e", None))
        self.loop_tool.setText(QCoreApplication.translate("MainWindow", u"\u5f53\u524d\u662f\u4e0d\u5faa\u73af\u64ad\u653e", None))
#if QT_CONFIG(tooltip)
        self.loop_tool.setToolTip(QCoreApplication.translate("MainWindow", u"\u5355\u51fb\u53ef\u5207\u6362\u5faa\u73af\u64ad\u653e/\u4e0d\u5faa\u73af\u64ad\u653e", None))
#endif // QT_CONFIG(tooltip)
        self.video_lbl.setText("")
        self.menumenu.setTitle(QCoreApplication.translate("MainWindow", u"\u83dc\u5355", None))
        self.toolBar.setWindowTitle(QCoreApplication.translate("MainWindow", u"toolBar", None))
    # retranslateUi

 

import sys
import cv2, time
from threading import Thread
from PySide2.QtGui import QImage, QPixmap
from PySide2.QtCore import Qt
from PySide2.QtWidgets import (QApplication, QMainWindow)
from p import Ui_MainWindow


class UI(QMainWindow, Ui_MainWindow):

    def __init__(self):
        super(UI, self).__init__()
        self.setupUi(self)
        # 只保留窗口关闭按钮, 禁止拉伸窗口大小
        self.setWindowFlags(Qt.WindowCloseButtonHint)
        self.setFixedSize(self.width(), self.height())
        self.b_close = False
        self.b_loop = False
        self.video_lbl.setStyleSheet("background-color: black")
        self.play_tool.triggered.connect(lambda : Thread(target=self.play_video).start())
        self.stop_tool.triggered.connect(self.stop_video)
        self.loop_tool.triggered.connect(self.loop_video)

    def stop_video(self):
        if self.b_close == False:
            self.b_close = True
            self.b_loop = True
        self.play_tool.setEnabled(True)

    def loop_video(self):
        if self.loop_tool.text() == '循环播放中':
            self.b_loop = False
            self.loop_tool.setText('当前为不循环播放')
        else:
            self.b_loop = True
            self.loop_tool.setText('循环播放中')

    def play_video(self):
        '''
        功能:播放影片
        :return:
        '''
        self.play_tool.setEnabled(False)
        self.b_close = False
        file = '1.mp4'
        self.cap = cv2.VideoCapture(file)
        if not self.cap.isOpened():
            print('Cannot open video')
            return
        while not self.b_close:
            while True:
                # 此循环用于循环播放判断
                if self.b_close == True:
                    break
                try:
                    ret, frame = self.cap.read()
                    if not ret:
                        if frame is None:
                            print('The video has end')
                            self.video_lbl.clear()
                            # 如果不循环播放就跳出
                            if self.b_loop == False:
                                break
                            else:
                                self.cap.release()
                                time.sleep(0.2)
                                self.cap = cv2.VideoCapture(file)
                        else:
                            print('Read video error!')
                        break
                    width = self.video_lbl.size().width()
                    height = self.video_lbl.size().height()
                    frame = cv2.resize(frame, (width,height), interpolation=cv2.INTER_AREA)
                    self.frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
                    self.qt_img = QImage(self.frame.data, self.frame.shape[1], self.frame.shape[0], self.frame.shape[1] * 3,
                                    QImage.Format_RGB888)
                    self.video_lbl.setPixmap(QPixmap.fromImage(self.qt_img))
                except:
                    pass
            if self.b_loop == False:
                break

        self.cap.release()
        self.video_lbl.clear()
        self.play_tool.setEnabled(True)

if __name__ == '__main__':
    app = QApplication([])
    server = UI()
    server.show()
    sys.exit(app.exec_())