Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to overlay a text with an video (QVideoWiget)



  • I would like a text on a video. I use a QVideoWidget to play the video.

    from PyQt5 import QtCore, QtGui, QtWidgets, QtMultimedia
    
    from GUI import Ui_MainWindow
    
    class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
        def __init__(self, *args, **kwargs):
            QtWidgets.QMainWindow.__init__(self, *args, **kwargs)
            self.setupUi(self)
    
            self.mediaPlayer = QtMultimedia.QMediaPlayer(self)
            self.mediaPlayer.setVideoOutput(self.widget)
            # fileName = "/path/of/your/local_file"
            # url = QtCore.QUrl.fromLocalFile(fileName)
            url = QtCore.QUrl("http://clips.vorwaerts-gmbh.de/VfE_html5.mp4")
            self.mediaPlayer.setMedia(QtMultimedia.QMediaContent(url))
            self.mediaPlayer.play()
    
    if __name__ == "__main__":
        import sys
        app = QtWidgets.QApplication(sys.argv)
        w = MainWindow()
        w.show()
        sys.exit(app.exec_())
    


  • @amaimon02
    Google for qvideowidget text. There are a few hits, they will show if it can be done at all.



  • I have never used QVideoWidget, but based on some prior experience with different GUI frameworks, I would caution that your results may vary by operating system, and by whatever underlying video playback engine is ultimately responsible for getting the video pixels on the screen.

    I tried to do something like this a while ago, and on one platform the text window (a transparent window with opaque text) that I tried to place on top of the video window worked just fine. On another platform the text was barely noticeable. You could only notice it flicker into view occasionally, like some kind of subliminal messaging. I believe the reason for the unwanted "flicker behavior" on that system was due to the video engine constantly writing directly to the video card, bypassing any attempts at layering/compositing.


Log in to reply