Vscode python pyside6 制作视频播放器

一、界面如下

包含控件 qcombox、qtablewidget、qpushbotton、qverticalslider

Vscode python pyside6 制作视频播放器_第1张图片

二、运行代码

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.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 += 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 < self.i-1:
            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 playpreviousvideo(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() < 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)
    # setupUi

    def 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



 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

三、运行效果

Vscode python pyside6 制作视频播放器_第2张图片

你可能感兴趣的:(Python,编程和应用实现,QT,python,音视频,开发语言)