【简便的PyQt5】制作一个极具特色的视频播放器
写在前面
效果展示
开源代码
loadInputVideo.py
demo.py
实现了以下功能 :
❤ PyQt5本地上传视频
❤ PyQt5配合OpenCv播放所有类型的视频
❤ PyQt5标签里文本居中
❤ PyQt5标签里文本重写
❤ 视频可多次上传播放
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'loadInputVideo.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(872, 638)
MainWindow.setStyleSheet("background-color:black;")
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(280, 550, 60, 60))
self.pushButton.setMinimumSize(QtCore.QSize(60, 60))
self.pushButton.setMaximumSize(QtCore.QSize(60, 60))
self.pushButton.setStyleSheet("font: 11pt \"楷体\";\n"
"background-color:black;\n"
"color:red;\n"
"border-radius:30px;\n"
"border:2px groove blue;\n"
"border-style:outset;\n"
"")
self.pushButton.setObjectName("pushButton")
self.label_4 = QtWidgets.QLabel(self.centralwidget)
self.label_4.setGeometry(QtCore.QRect(30, 40, 800, 450))
self.label_4.setMaximumSize(QtCore.QSize(905, 650))
self.label_4.setStyleSheet("color:red;\n"
"border-width:1px;\n"
"border-style:solid;\n"
"border-color:red;\n"
"background-color:black;\n"
"font: 18pt \"华文行楷\";")
self.label_4.setObjectName("label_4")
self.label_8 = QtWidgets.QLabel(self.centralwidget)
self.label_8.setGeometry(QtCore.QRect(30, 500, 801, 41))
font = QtGui.QFont()
font.setFamily("楷体")
font.setPointSize(13)
font.setBold(False)
font.setItalic(False)
font.setWeight(50)
self.label_8.setFont(font)
self.label_8.setStyleSheet("font: 13pt \"楷体\";\n"
"color:red;\n"
"border-width:1px;\n"
"border-style:solid;\n"
"border-color:red;\n"
"background-color:black;")
self.label_8.setText("")
self.label_8.setObjectName("label_8")
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(500, 550, 60, 60))
self.pushButton_2.setMinimumSize(QtCore.QSize(60, 60))
self.pushButton_2.setMaximumSize(QtCore.QSize(60, 60))
self.pushButton_2.setStyleSheet("font: 11pt \"楷体\";\n"
"background-color:black;\n"
"color:red;\n"
"border-radius:30px;\n"
"border:2px groove blue;\n"
"border-style:outset;\n"
"text-align:centet;")
self.pushButton_2.setObjectName("pushButton_2")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 872, 23))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "Load"))
self.label_4.setText(_translate("MainWindow", " Waiting for video..."))
self.pushButton_2.setText(_translate("MainWindow", "Play"))
import cv2
import shutil
import time
from scipy import signal
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np
import sys
import os
from GuiData.loadInputVideo import Ui_MainWindow as playVideo
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import Qt
class playVideoWindow(QtWidgets.QMainWindow, playVideo):
def __init__(self):
super(playVideoWindow, self).__init__()
self.setupUi(self)
self.cap = []
self.videoName = ''
# 居中
self.label_8.setAlignment(Qt.AlignCenter)
self.reSet_label('please load video...')
self.timer_camera = QtCore.QTimer()
self.timer_camera.setTimerType(Qt.PreciseTimer)
self.pushButton.clicked.connect(self.load_video)
self.pushButton_2.clicked.connect(self.play_video)
def load_video(self):
""" Slot function to start the progamme
"""
self.videoName, _ = QtWidgets.QFileDialog.getOpenFileName(self, "Open", "", "*.MOV;;*.avi;;*.mp4;;All Files(*)")
self.label_4.repaint()
self.label_4.setText('It is ready...')
self.label_4.repaint()
self.reSet_label('please click play button')
def play_video(self):
if self.videoName != "":
self.cap = cv2.VideoCapture(self.videoName)
self.timer_camera.start(1)
self.timer_camera.timeout.connect(self.openFrame)
def openFrame(self):
""" Slot function to capture frame and process it
"""
if (self.cap.isOpened()):
self.reSet_label('playing...')
ret, self.frame = self.cap.read()
if ret:
frame = cv2.cvtColor(self.frame, cv2.COLOR_BGR2RGB)
height, width, bytesPerComponent = frame.shape
bytesPerLine = bytesPerComponent * width
q_image = QtGui.QImage(frame.data, width, height, bytesPerLine,
QtGui.QImage.Format_RGB888).scaled(self.label_4.width(), self.label_4.height())
self.label_4.setPixmap(QtGui.QPixmap.fromImage(q_image))
else:
self.reSet_label('over!')
self.label_4.repaint()
self.label_4.setText('Waiting for video...')
self.label_4.repaint()
self.cap.release()
self.timer_camera.stop()
def reSet_label(self, text):
self.label_8.repaint()
self.label_8.setText(text)
self.label_8.repaint()
# 使用案例
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
contorller = playVideoWindow()
contorller.show()
sys.exit(app.exec_())
完毕!
如果觉得有帮助,麻烦点个赞+收藏喔!