使用PyQt5播放视频并添加字幕——最简单示例

参考链接:C版本

from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QLabel, QGraphicsTextItem, QGraphicsScene, QGraphicsView
from PyQt5.QtMultimedia import QMediaPlaylist, QMediaPlayer, QMediaContent
from PyQt5.QtMultimediaWidgets import QGraphicsVideoItem
from PyQt5.Qt import QUrl, QVideoWidget, QSizeF
from PyQt5.QtGui import QKeyEvent
from PyQt5.QtCore import Qt


class Video(QMainWindow):

    def __init__(self):
        super(Video, self).__init__()
        self.resize(1920, 1080)
        self._item = QGraphicsVideoItem()
        self._textItem = QGraphicsTextItem()
        self._view = QGraphicsView()
        self._scene = QGraphicsScene()
        self._view.resize(1920, 1080)
        self._view.setScene(self._scene)
        self._scene.addItem(self._item)
        self._scene.addItem(self._textItem)
        self._textItem.setPlainText('SRT TEXT')
        self._textItem.setDefaultTextColor(Qt.red)
        font = self._textItem.font()
        font.setPixelSize(50)
        self._textItem.setFont(font)
        self._view.show()
        self._item.setSize(QSizeF(1920, 1080))
        self._player = QMediaPlayer(self)
        self._player.setMedia(QMediaContent(
            QUrl.fromLocalFile('视频路径‘)))
        self._player.setVideoOutput(self._item)
        self._player.play()
        self.setCentralWidget(self._view)


if __name__ == '__main__':
    import sys

    app = QApplication(sys.argv)
    test = Video()
    test.show()
    sys.exit(app.exec_())
上一篇:anaconda安装pyqt5一般步骤与注意事项


下一篇:python-gui编程pyqt5视频