一、界面如下
包含控件 qcombox、qtablewidget、qpushbotton、qverticalslider
二、运行代码
media_player.py
import sys
from 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 = 0服务器托管网
self.list_i = 0
self.maxValue = 1000
self.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) # pause
self.ui.btn_next.clicked.connect(self.playnextvideo) # next video
self.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.m服务器托管网edia_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 += 1
self.ui.bar_slider.setMaximum(round(self.media_duration*10))
self.time_count = 0
self.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 = 0
self.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 0:
self.list_i -= 1
item = 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 = 0
self.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 += 1
print(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 = 0
def get_duration_from_cv2(filename):
cap = cv2.VideoCapture(filename)
if cap.isOpened():
rate = cap.get(5)
frame_num =cap.get(7)
duration = frame_num/rate
return duration
return -1
if __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()
test.ui
MainWindow
0
0
800
600
MainWindow
220
10
511
351
10
10
181
361
文件列表
220
400
511
22
Qt::Horizontal
32
400
181
20
时间显示
350
460
51
41
播放
430
460
51
41
暂停
510
460
41
41
下一个
270
460
51
41
上一个
600
462
61
41
打开文件
0
0
800
21
QVideoWidget
QWidget
PyQt5.QtMultimediaWidgets
1
三、运行效果
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
独立服务器和云服务器的区别是很多用户在选择服务器时要做的课程,那么独立服务器和云服务器的区别有哪些呢? 独立服务器和云服务器是两种不同的服务器部署方式,它们在性能、成本、资源利用、安全性和维护等方面存在显著差异。 1. **性能对比**: …