一、界面如下
包含控件 qcombox、qtablewidget、qpushbotton、qverticalslider
二、运行代码
media_player.py
import sysfrom PySide6 import QtWidgets
from PySide6.QtWidgets import *
from PySide6.QtMultimedia import *
from PySide6.QtMultimediaWidgets import QVideoWidget
from PySide6.QtCore import QTimer
#from PySide6.QtMultimedia import QMediaContent
from test_ui import Ui_MainWindow
import cv2 class MainWindow(QMainWindow):def __init__(self, parent = None) :super().__init__(parent)self.ui = Ui_MainWindow()#self.ui.tableWidget = QTableWidget()self.ui.setupUi(self)self.ui.tableWidget.setRowCount(6)self.ui.tableWidget.setColumnCount(1)self.i = 0self.list_i = 0self.maxValue = 1000self.timer = QTimer()self.video_name_t = []# 播放器self.player = QMediaPlayer()self.player.setVideoOutput(self.ui.wdt_camera)#self.ui.tableWidget = QTableWidget(1,6)# 按钮打开文件self.ui.btn_open.clicked.connect(self.openVideoFile)# 播放self.ui.btn_play.clicked.connect(self.playVideo) # play# 暂停self.ui.btn_pause.clicked.connect(self.pauseVideo) # pauseself.ui.btn_next.clicked.connect(self.playnextvideo) # next videoself.ui.btn_pre.clicked.connect(self.playpreviousvideo)def openVideoFile(self):#self.player.setMedia(QMediaContent(QFileDialog.getOpenFileUrl()[0]))#self.player.setSource(QUrl.fromLocalFile(QFileDialog.getOpenFileUrl()[0]))video_name = QFileDialog.getOpenFileName()[0]print(video_name)self.player.setSource(video_name)self.media_duration = get_duration_from_cv2(video_name)print(self.media_duration)if video_name not in self.video_name_t:self.video_name_t.append(video_name)item = QTableWidgetItem(video_name)item.setText(video_name)self.ui.tableWidget.setItem(0,self.i, item)self.i += 1self.ui.bar_slider.setMaximum(round(self.media_duration*10))self.time_count = 0self.player.play()self.timer.setInterval(100)self.timer.start()self.timer.timeout.connect(self.onTimerOut)#print(self.player.availableMetaData())def playVideo(self):self.player.play()self.ui.bar_slider.setMaximum(round(self.media_duration*10))#self.time_count = 0self.timer.setInterval(100)self.timer.start()self.timer.timeout.connect(self.onTimerOut)def pauseVideo(self):self.player.pause()self.timer.stop()def playnextvideo(self):if self.list_i < self.i-1:self.list_i += 1item = self.ui.tableWidget.item(self.list_i,0)video_name = item.text()#video_name = QFileDialog.getOpenFileName()[0]print(video_name)self.player.setSource(video_name)self.media_duration = get_duration_from_cv2(video_name)print(self.media_duration)self.ui.bar_slider.setMaximum(round(self.media_duration*10))self.time_count = 0self.player.play()self.timer.setInterval(100)self.timer.start()self.timer.timeout.connect(self.onTimerOut)def playpreviousvideo(self):if self.list_i > 0:self.list_i -= 1item = self.ui.tableWidget.item(self.list_i,0)video_name = item.text()#video_name = QFileDialog.getOpenFileName()[0]print(video_name)self.player.setSource(video_name)self.media_duration = get_duration_from_cv2(video_name)print(self.media_duration)self.ui.bar_slider.setMaximum(round(self.media_duration*10))self.time_count = 0self.player.play()self.timer.setInterval(100)self.timer.start()self.timer.timeout.connect(self.onTimerOut) def onTimerOut(self):#if self.player.playbackState()==self.player.isPlaying():#if self.player.duration()>0:self.time_count += 1print(self.time_count*0.1)#self.ui.bar_slider.setValue(round(self.player.position()*self.maxValue/self.media_duration))self.ui.bar_slider.setValue(round(self.time_count))m, s = divmod(round(self.time_count*0.1), 60)h, m = divmod(m, 60)self.ui.label.setText("%02d:%02d:%02d" % (h, m, s))if self.time_count == round(self.media_duration*10):self.timer.stop()self.time_count = 0def get_duration_from_cv2(filename):cap = cv2.VideoCapture(filename)if cap.isOpened():rate = cap.get(5)frame_num =cap.get(7)duration = frame_num/ratereturn durationreturn -1if __name__ == '__main__':app = QtWidgets.QApplication(sys.argv)win = MainWindow()win.setWindowTitle("视频播放器")win.show()app.exit(app.exec_())
test_ui.py
# -*- coding: utf-8 -*-################################################################################
## Form generated from reading UI file 'test.ui'
##
## Created by: Qt User Interface Compiler version 6.5.1
##
## WARNING! All changes made in this file will be lost when recompiling UI file!
################################################################################from PySide6.QtCore import (QCoreApplication, QDate, QDateTime, QLocale,QMetaObject, QObject, QPoint, QRect,QSize, QTime, QUrl, Qt)
from PySide6.QtGui import (QBrush, QColor, QConicalGradient, QCursor,QFont, QFontDatabase, QGradient, QIcon,QImage, QKeySequence, QLinearGradient, QPainter,QPalette, QPixmap, QRadialGradient, QTransform)
from PySide6.QtMultimediaWidgets import QVideoWidget
from PySide6.QtWidgets import (QApplication, QHeaderView, QLabel, QMainWindow,QMenuBar, QPushButton, QSizePolicy, QSlider,QStatusBar, QTableWidget, QTableWidgetItem, QWidget)class Ui_MainWindow(object):def setupUi(self, MainWindow):if not MainWindow.objectName():MainWindow.setObjectName(u"MainWindow")MainWindow.resize(800, 600)self.centralwidget = QWidget(MainWindow)self.centralwidget.setObjectName(u"centralwidget")self.wdt_camera = QVideoWidget(self.centralwidget)self.wdt_camera.setObjectName(u"wdt_camera")self.wdt_camera.setGeometry(QRect(220, 10, 511, 351))self.tableWidget = QTableWidget(self.centralwidget)if (self.tableWidget.columnCount() < 1):self.tableWidget.setColumnCount(1)__qtablewidgetitem = QTableWidgetItem()self.tableWidget.setHorizontalHeaderItem(0, __qtablewidgetitem)self.tableWidget.setObjectName(u"tableWidget")self.tableWidget.setGeometry(QRect(10, 10, 181, 361))self.bar_slider = QSlider(self.centralwidget)self.bar_slider.setObjectName(u"bar_slider")self.bar_slider.setGeometry(QRect(220, 400, 511, 22))self.bar_slider.setOrientation(Qt.Horizontal)self.label = QLabel(self.centralwidget)self.label.setObjectName(u"label")self.label.setGeometry(QRect(32, 400, 181, 20))self.btn_play = QPushButton(self.centralwidget)self.btn_play.setObjectName(u"btn_play")self.btn_play.setGeometry(QRect(350, 460, 51, 41))self.btn_pause = QPushButton(self.centralwidget)self.btn_pause.setObjectName(u"btn_pause")self.btn_pause.setGeometry(QRect(430, 460, 51, 41))self.btn_next = QPushButton(self.centralwidget)self.btn_next.setObjectName(u"btn_next")self.btn_next.setGeometry(QRect(510, 460, 41, 41))self.btn_pre = QPushButton(self.centralwidget)self.btn_pre.setObjectName(u"btn_pre")self.btn_pre.setGeometry(QRect(270, 460, 51, 41))self.btn_open = QPushButton(self.centralwidget)self.btn_open.setObjectName(u"btn_open")self.btn_open.setGeometry(QRect(600, 462, 61, 41))MainWindow.setCentralWidget(self.centralwidget)self.menubar = QMenuBar(MainWindow)self.menubar.setObjectName(u"menubar")self.menubar.setGeometry(QRect(0, 0, 800, 21))MainWindow.setMenuBar(self.menubar)self.statusbar = QStatusBar(MainWindow)self.statusbar.setObjectName(u"statusbar")MainWindow.setStatusBar(self.statusbar)self.retranslateUi(MainWindow)QMetaObject.connectSlotsByName(MainWindow)# setupUidef retranslateUi(self, MainWindow):MainWindow.setWindowTitle(QCoreApplication.translate("MainWindow", u"MainWindow", None))___qtablewidgetitem = self.tableWidget.horizontalHeaderItem(0)___qtablewidgetitem.setText(QCoreApplication.translate("MainWindow", u"\u6587\u4ef6\u5217\u8868", None));self.label.setText(QCoreApplication.translate("MainWindow", u"\u65f6\u95f4\u663e\u793a", None))self.btn_play.setText(QCoreApplication.translate("MainWindow", u"\u64ad\u653e", None))self.btn_pause.setText(QCoreApplication.translate("MainWindow", u"\u6682\u505c", None))self.btn_next.setText(QCoreApplication.translate("MainWindow", u"\u4e0b\u4e00\u4e2a", None))self.btn_pre.setText(QCoreApplication.translate("MainWindow", u"\u4e0a\u4e00\u4e2a", None))self.btn_open.setText(QCoreApplication.translate("MainWindow", u"\u6253\u5f00\u6587\u4ef6", None))# retranslateUi
test.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0"><class>MainWindow</class><widget class="QMainWindow" name="MainWindow"><property name="geometry"><rect><x>0</x><y>0</y><width>800</width><height>600</height></rect></property><property name="windowTitle"><string>MainWindow</string></property><widget class="QWidget" name="centralwidget"><widget class="QVideoWidget" name="wdt_camera" native="true"><property name="geometry"><rect><x>220</x><y>10</y><width>511</width><height>351</height></rect></property></widget><widget class="QTableWidget" name="tableWidget"><property name="geometry"><rect><x>10</x><y>10</y><width>181</width><height>361</height></rect></property><column><property name="text"><string>文件列表</string></property></column></widget><widget class="QSlider" name="bar_slider"><property name="geometry"><rect><x>220</x><y>400</y><width>511</width><height>22</height></rect></property><property name="orientation"><enum>Qt::Horizontal</enum></property></widget><widget class="QLabel" name="label"><property name="geometry"><rect><x>32</x><y>400</y><width>181</width><height>20</height></rect></property><property name="text"><string>时间显示</string></property></widget><widget class="QPushButton" name="btn_play"><property name="geometry"><rect><x>350</x><y>460</y><width>51</width><height>41</height></rect></property><property name="text"><string>播放</string></property></widget><widget class="QPushButton" name="btn_pause"><property name="geometry"><rect><x>430</x><y>460</y><width>51</width><height>41</height></rect></property><property name="text"><string>暂停</string></property></widget><widget class="QPushButton" name="btn_next"><property name="geometry"><rect><x>510</x><y>460</y><width>41</width><height>41</height></rect></property><property name="text"><string>下一个</string></property></widget><widget class="QPushButton" name="btn_pre"><property name="geometry"><rect><x>270</x><y>460</y><width>51</width><height>41</height></rect></property><property name="text"><string>上一个</string></property></widget><widget class="QPushButton" name="btn_open"><property name="geometry"><rect><x>600</x><y>462</y><width>61</width><height>41</height></rect></property><property name="text"><string>打开文件</string></property></widget></widget><widget class="QMenuBar" name="menubar"><property name="geometry"><rect><x>0</x><y>0</y><width>800</width><height>21</height></rect></property></widget><widget class="QStatusBar" name="statusbar"/></widget><customwidgets><customwidget><class>QVideoWidget</class><extends>QWidget</extends><header>PyQt5.QtMultimediaWidgets</header><container>1</container></customwidget></customwidgets><resources/><connections/>
</ui>
三、运行效果