先看下效果
点击打开视频->进行视频选择,可以暂停和播放,双击可以全屏,代码也是网上收集的
有2个demo,第一个是简单版,第二个demo增加了播放,暂停,进度条,全屏
需要安装一个视频解码器,K-Lite Codec Pack,下载地址http://www.codecguide.com/download_kl.htm
demo1.py
from PyQt5.QtWidgets import *
from PyQt5.QtMultimedia import *
from PyQt5.QtMultimediaWidgets import QVideoWidget
import sys
if __name__ == '__main__':
app = QApplication(sys.argv)
player = QMediaPlayer()
vw = QVideoWidget()
#定义视频显示的widget
vw.show()
player.setVideoOutput(vw)
#视频播放输出的widget,就是上面定义的
player.setMedia(QMediaContent(QFileDialog.getOpenFileUrl()[0])) #选取视频文件
player.play() #播放视频
sys.exit(app.exec_())
demo2中包含3个文件
demo2.py
from PyQt5.QtWidgets import *
from PyQt5.QtMultimedia import *
from VideoUi import VideoUi
from VideoWidget import VideoWidget
import sys
class MainWindow(VideoUi, QMainWindow):
def __init__(self):
super(VideoUi, self).__init__()
self.setupUi(self)
self.videoFullScreen = False # 判断当前widget是否全屏
self.videoFullScreenWidget = VideoWidget() # 创建一个全屏的widget
self.videoFullScreenWidget.setFullScreen(1)
self.videoFullScreenWidget.hide() # 不用的时候隐藏起来
self.player = QMediaPlayer()
self.player.setVideoOutput(self.wgt_video) # 视频播放输出的widget,就是上面定义的
self.btn_open.clicked.connect(self.openVideoFile) # 打开视频文件按钮
self.btn_play.clicked.connect(self.playVideo) # play
self.btn_stop.clicked.connect(self.pauseVideo) # pause
self.player.positionChanged.connect(self.changeSlide) # change Slide
self.videoFullScreenWidget.doubleClickedItem.connect(self.videoDoubleClicked) #双击响应
self.wgt_video.doubleClickedItem.connect(self.videoDoubleClicked) #双击响应
def openVideoFile(self):
self.player.setMedia(QMediaContent(QFileDialog.getOpenFileUrl()[0])) # 选取视频文件
self.player.play() # 播放视频
def playVideo(self):
self.player.play()
def pauseVideo(self):
self.player.pause()
def changeSlide(self,position):
self.vidoeLength = self.player.duration()+0.1
self.sld_video.setValue(round((position/self.vidoeLength)*100))
self.lab_video.setText(str(round((position/self.vidoeLength)*100,2))+'%')
def videoDoubleClicked(self,text):
if self.player.duration() > 0: # 开始播放后才允许进行全屏操作
if self.videoFullScreen:
self.player.pause()
self.videoFullScreenWidget.hide()
self.player.setVideoOutput(self.wgt_video)
self.player.play()
self.videoFullScreen = False
else:
self.player.pause()
self.videoFullScreenWidget.show()
self.player.setVideoOutput(self.videoFullScreenWidget)
self.player.play()
self.videoFullScreen = True
if __name__ == '__main__':
app = QApplication(sys.argv)
mainWindow = MainWindow()
mainWindow.show()
sys.exit(app.exec_())
VideoUi.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'GUI.ui'
#
# Created by: PyQt5 UI code generator 5.6
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QAction, qApp
from PyQt5.QtWidgets import (QWidget, QLabel, QLineEdit,
QTextEdit, QGridLayout, QApplication)
from VideoWidget import VideoWidget
class VideoUi(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 650)
#获取画布
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
#视频播放控件
self.wgt_video = VideoWidget(self.centralwidget)
self.wgt_video.setGeometry(QtCore.QRect(0, 0, 800, 500))
self.wgt_video.setObjectName("wgt_video")
#选择文件按钮
self.btn_open = QtWidgets.QPushButton(self.centralwidget)
self.btn_open.setGeometry(QtCore.QRect(5, 570, 80, 30))
self.btn_open.setObjectName("btn_open")
#进度条
self.sld_video = QtWidgets.QSlider(self.centralwidget)
self.sld_video.setGeometry(QtCore.QRect(5, 530, 750, 30))
self.sld_video.setMaximum(100)
self.sld_video.setOrientation(QtCore.Qt.Horizontal)
self.sld_video.setObjectName("sld_video")
#进度值
self.lab_video = QtWidgets.QLabel(self.centralwidget)
self.lab_video.setGeometry(QtCore.QRect(765, 529, 50, 30))
self.lab_video.setObjectName("lab_video")
#播放按钮
self.btn_play = QtWidgets.QPushButton(self.centralwidget)
self.btn_play.setGeometry(QtCore.QRect(90, 570, 50, 30))
self.btn_play.setObjectName("btn_play")
#停止按钮
self.btn_stop = QtWidgets.QPushButton(self.centralwidget)
self.btn_stop.setGeometry(QtCore.QRect(145, 570, 50, 30))
self.btn_stop.setObjectName("btn_stop")
MainWindow.setCentralWidget(self.centralwidget)
exitAct = QAction(QIcon('exit.png'), '&Exit', self)
exitAct.setShortcut('Ctrl+Q')
exitAct.setStatusTip('Exit application')
exitAct.triggered.connect(qApp.quit)
self.statusBar()
menubar = self.menuBar()
fileMenu = menubar.addMenu('&File')
fileMenu.addAction(exitAct)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.btn_open.setText(_translate("MainWindow", "打开视频文件"))
self.btn_play.setText(_translate("MainWindow", "播放"))
self.btn_stop.setText(_translate("MainWindow", "暂停"))
self.lab_video.setText(_translate("MainWindow", "0%"))
VideoWidget.py
from PyQt5.QtMultimediaWidgets import QVideoWidget
from PyQt5.QtCore import *
class VideoWidget(QVideoWidget):
doubleClickedItem = pyqtSignal(str) # 创建双击信号
def __init__(self,parent=None):
super(QVideoWidget,self).__init__(parent)
def mouseDoubleClickEvent(self,QMouseEvent): #双击事件
self.doubleClickedItem.emit("double clicked")
新手可以通过pycharm建立一个新工程,然后把这4个文件放到工程中,添加依赖然后就可以运行了
代码下载地址