我想在我的 Qt GUI 中進行視頻預覽,我很高興看到 PyQt5 支持 QMediaPlayer。
我在 SO 上找到了幾個基本示例,下面的這個只是一個:
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):
if e.key() == Qt.Key_L:
print('loading')
self.load()
if e.key() == Qt.Key_P:
print('playing')
self.mediaPlayer.play()
print('state: ' str(self.mediaPlayer.state()))
print('mediaStatus: ' str(self.mediaPlayer.mediaStatus()))
print('error: ' str(self.mediaPlayer.error()))
print('------------------------')
def load(self):
# H264 MPEG4 AVC not working
file = 'C:/Users/Antonio/Videos/test.wmv'
local = QUrl.fromLocalFile(file)
media = QMediaContent(local)
self.mediaPlayer.setMedia(media)
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
player = VideoPlayer()
player.show()
sys.exit(app.exec_())
我想預覽的視頻通常是用 H264 MPEG4 AVC 編碼的,我可以使用 VLC 在電腦上觀看它們。但是當我嘗試使用上面的腳本打開相同的視頻時,我收到一條 InvalidMedia 作為錯誤訊息。
我嘗試使用 VLC 將視頻轉換為 WMV WMA,然后它按預期作業。
閱讀 Qt 檔案(參見此處)我的印象是在 Windows 上僅支持 WMF 檔案。這是真的?
是否有可能通過安裝編解碼器包將 QMediaPlayer 擴展到更大的格式系列?
如果是,我怎樣才能知道我的 pyqt5 安裝在哪里可以找到相關的編解碼器?
在此先感謝歡呼
uj5u.com熱心網友回復:
Qt 在 Windows 上使用 DirectShow 后端,默認情況下,它只支持專有的 MS 格式。對于非本地格式,用戶必須始終安裝額外的編解碼器,除非他們使用的應用程式自己捆綁了它們。Qt 完全依賴于平臺后端的功能,因此如果您想在 Windows 上支持更廣泛的流行格式,您幾乎肯定必須安裝第三方編解碼器包。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/509840.html
標籤:Pythonqtpyqt5h.264qmediaplayer
