如何创建 Windows GUI 应用程序来播放视频
Posted
技术标签:
【中文标题】如何创建 Windows GUI 应用程序来播放视频【英文标题】:How to create Windows GUI application to play videos 【发布时间】:2020-03-14 09:46:08 【问题描述】:这是我在下面尝试的代码。 我想播放 .avi、.mp4 等。 我可以选择视频但无法播放。
import sys
from PyQt5 import QtCore, QtGui, QtWidgets, QtMultimedia, QtMultimediaWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtMultimedia import QMediaContent, QMediaPlayer
from PyQt5.QtMultimediaWidgets import QVideoWidget
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
self.mediaPlayer = QtMultimedia.QMediaPlayer(None, QtMultimedia.QMediaPlayer.VideoSurface)
videoWidget = QtMultimediaWidgets.QVideoWidget()
MainWindow.setObjectName("MainWindow")
MainWindow.resize(881, 578)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.frame = QtWidgets.QFrame(self.centralwidget)
self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame.setObjectName("frame")
self.gridLayout_2 = QtWidgets.QGridLayout(self.frame)
self.gridLayout_2.setObjectName("gridLayout_2")
self.widget = QVideoWidget(self.frame)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.widget.sizePolicy().hasHeightForWidth())
self.widget.setSizePolicy(sizePolicy)
self.widget.setMinimumSize(QtCore.QSize(711, 471))
self.widget.setMaximumSize(QtCore.QSize(711, 471))
self.widget.setObjectName("widget")
self.gridLayout_2.addWidget(self.widget, 0, 0, 3, 1)
self.audiobar = QtWidgets.QSlider(self.frame)
self.audiobar.setMinimumSize(QtCore.QSize(22, 160))
self.audiobar.setMaximumSize(QtCore.QSize(22, 160))
self.audiobar.setOrientation(QtCore.Qt.Vertical)
self.audiobar.setObjectName("audiobar")
self.gridLayout_2.addWidget(self.audiobar, 2, 2, 1, 1)
self.videobar = QtWidgets.QSlider(self.frame)
self.videobar.setMinimumSize(QtCore.QSize(711, 22))
self.videobar.setMaximumSize(QtCore.QSize(711, 22))
self.videobar.setOrientation(QtCore.Qt.Horizontal)
self.videobar.setObjectName("videobar")
self.gridLayout_2.addWidget(self.videobar, 3, 0, 1, 1)
self.frame_2 = QtWidgets.QFrame(self.frame)
self.frame_2.setMinimumSize(QtCore.QSize(120, 111))
self.frame_2.setMaximumSize(QtCore.QSize(120, 111))
self.frame_2.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame_2.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame_2.setObjectName("frame_2")
self.playbtn = QtWidgets.QPushButton(self.frame_2)
self.playbtn.setGeometry(QtCore.QRect(20, 10, 75, 23))
self.playbtn.setObjectName("playbtn")
self.playbtn.setEnabled(False)
self.pausebtn = QtWidgets.QPushButton(self.frame_2)
self.pausebtn.setGeometry(QtCore.QRect(20, 40, 75, 23))
self.pausebtn.setObjectName("pausebtn")
self.stopbtn = QtWidgets.QPushButton(self.frame_2)
self.stopbtn.setGeometry(QtCore.QRect(20, 70, 75, 23))
self.stopbtn.setObjectName("stopbtn")
self.gridLayout_2.addWidget(self.frame_2, 0, 2, 1, 1)
spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.gridLayout_2.addItem(spacerItem, 0, 1, 1, 1)
self.gridLayout.addWidget(self.frame, 1, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 881, 21))
self.menubar.setObjectName("menubar")
self.menuFile = QtWidgets.QMenu(self.menubar)
self.menuFile.setObjectName("menuFile")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.actionOpen = QtWidgets.QAction(MainWindow)
self.actionOpen.setObjectName("actionOpen")
self.actionClose = QtWidgets.QAction(MainWindow)
self.actionClose.setObjectName("actionClose")
self.menuFile.addAction(self.actionOpen)
self.menuFile.addAction(self.actionClose)
self.menubar.addAction(self.menuFile.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.mediaPlayer.setVideoOutput(videoWidget)
self.actionOpen.triggered.connect(self.openFile)
self.playbtn.clicked.connect(self.play)
self.mediaPlayer.setVideoOutput(videoWidget)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MPlayer"))
self.playbtn.setText(_translate("MainWindow", "Play"))
self.pausebtn.setText(_translate("MainWindow", "Pause"))
self.stopbtn.setText(_translate("MainWindow", "Stop"))
self.menuFile.setTitle(_translate("MainWindow", "File"))
self.actionOpen.setText(_translate("MainWindow", "Open"))
self.actionClose.setText(_translate("MainWindow", "Close"))
def openFile(self):
fileName, _ = QFileDialog.getOpenFileName(None, "Select Video", " ", "Video Files (*.mp4 *.avi)",
QtCore.QDir.homePath())
if fileName != '':
self.mediaPlayer.setMedia(
QtMultimedia.QMediaContent(QtCore.QUrl.fromLocalFile(fileName)))
self.playbtn.setEnabled(True)
self.statusbar.showMessage(fileName)
self.play()
def play(self):
if self.mediaPlayer.state() == QtMultimedia.QMediaPlayer.PlayingState:
self.mediaPlayer.pause()
else:
self.mediaPlayer.play()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
【问题讨论】:
【参考方案1】:您将视频输出设置为videoWidget
,但您没有将其添加到界面中。
不过,您已经创建了一个视频小部件,它是 self.widget
,所以您只需要使用它(而且只需要一次):
self.mediaPlayer.setVideoOutput(self.widget)
请注意,您正在为您的程序使用 pyuic 的输出,这是非常不鼓励的。请遵循关于 using Designer 的指南。
【讨论】:
它工作但无法播放 .mp4 视频如何播放 .mp4 视频。 @user3030327 你用的是什么操作系统?如果是linux,可能需要安装gstreamer的相关编解码器。 我使用的是 Windows。 @user3030327 可能仍然需要安装所需的编解码器(即使 Windows 的 MediaPlayer 能够播放它们,但这并不意味着 Qt 可以)。尝试从命令提示符运行您的程序,如果它说找不到所需的插件,就是这样。看看LAVFilters以上是关于如何创建 Windows GUI 应用程序来播放视频的主要内容,如果未能解决你的问题,请参考以下文章
如何在Visual C ++中创建非托管Windows GUI?
在没有 IDE 的 C++ 中手动编写 Windows GUI