其他分享
首页 > 其他分享> > 使用PyQt5播放视频并添加字幕——最简单示例

使用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_())

标签:__,示例,self,PyQt5,字幕,._,textItem,import
来源: https://blog.csdn.net/m0_46175974/article/details/121124233