下面是我正在使用的测试脚本,以便为PyQt5播放示例视频。不幸的是,加载视频后不久,mediaPlayer mediaStatus切换到代码8(InvalidMedia)我也尝试使用本地版本的.mp4,但无济于事。我尝试过其他文件,但无法进一步诊断问题。我在Stack Overflow的其他地方发现了媒体播放器的这种实现,所以如果有人知道一种完全不同的实现方式,我对此持开放态度。
此外,我已经测试过"教程"我的.mp4上的视频播放器,但也没有播放任何内容。 Github
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtMultimedia import *
from PyQt5.QtMultimediaWidgets import *
import glob
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.setWindowTitle('Navon test')
self.setWindowFlags(Qt.FramelessWindowHint)
# exit option for the menu bar File menu
self.exit = QAction('Exit', self)
self.exit.setShortcut('Ctrl+q')
# message for the status bar if mouse is over Exit
self.exit.setStatusTip('Exit program')
# newer connect style (PySide/PyQT 4.5 and higher)
self.exit.triggered.connect(app.quit)
self.setWindowIcon(QIcon('icon.ico'))
self.centralwidget = VideoPlayer()
self.setCentralWidget(self.centralwidget)
def keyPressEvent(self, e):
player = self.centralwidget.mediaPlayer
print('state: ' + str(player.state()))
print('mediaStatus: ' + str(player.mediaStatus()))
print('error: ' + str(player.error()))
if e.key() == Qt.Key_L:
print('loading')
self.centralwidget.load()
# update : missed () here
if e.key() == Qt.Key_P:
print('playing')
self.centralwidget.play()
class VideoPlayer(QWidget):
def __init__(self, parent=None):
super(VideoPlayer, self).__init__(parent)
self.mediaPlayer = QMediaPlayer(None, QMediaPlayer.VideoSurface)
self.videoItem = QGraphicsVideoItem()
self.videoItem.setSize(QSizeF(640, 480))
scene = QGraphicsScene(self)
graphicsView = QGraphicsView(scene)
scene.addItem(self.videoItem)
layout = QVBoxLayout()
layout.addWidget(graphicsView)
self.setLayout(layout)
self.mediaPlayer.setVideoOutput(self.videoItem)
def load(self):
local = QUrl('http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4')
self.mediaPlayer.setMedia(QMediaContent(local))
def play(self):
self.mediaPlayer.play()
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
player = MainWindow()
player.show()
sys.exit(app.exec_())
答案 0 :(得分:0)
首先,附上我的代码。
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtMultimedia import *
from PyQt5.QtMultimediaWidgets import *
class VideoPlayer(QWidget):
def __init__(self, parent=None):
super(VideoPlayer, self).__init__(parent)
videoItem = QGraphicsVideoItem()
videoItem.setSize(QSizeF(640, 480))
scene = QGraphicsScene(self)
scene.addItem(videoItem)
graphicsView = QGraphicsView(scene)
layout = QVBoxLayout()
layout.addWidget(graphicsView)
self.setLayout(layout)
self.mediaPlayer = QMediaPlayer(None, QMediaPlayer.VideoSurface)
self.mediaPlayer.setVideoOutput(videoItem)
def keyPressEvent(self, e):
print('state: ' + str(self.mediaPlayer.state()))
print('mediaStatus: ' + str(self.mediaPlayer.mediaStatus()))
print('error: ' + str(self.mediaPlayer.error()))
if e.key() == Qt.Key_L:
print('loading')
self.load()
if e.key() == Qt.Key_P:
print('playing')
self.mediaPlayer.play()
def load(self):
local = QUrl.fromLocalFile('C:\\Users\\tank.sui\\Videos\\Wildlife.wmv')
media = QMediaContent(local)
self.mediaPlayer.setMedia(media)
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
player = VideoPlayer()
player.show()
sys.exit(app.exec_())
查看代码后,只需在下面列出根本原因。QUrl.fromLocalFile
local = QUrl.fromLocalFile('C:\\Users\\tank.sui\\Videos\\Wildlife.wmv')
更新@三分之二千〇一十七/ 15
要支持多种音频/视频格式,您需要安装相应的解码器。
LAVFilters
,我推荐它,您可以从RELEASE下载并安装它。