<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[High CPU usage in QGraphicsVideoItem]]></title><description><![CDATA[<p dir="auto">I am trying to simply play an MP4 video (1920x1080) using a QGraphicsVideoItem within a QGraphicsView, however, it consumes around 70% of CPU and after a while, it makes the application freeze! I tried the OpenGLWidget in the QGraphicsView to improve the performance but it did not make any noticeable difference!<br />
While, if I substitute the QGraphicsView with a QVideoWidget, the CPU usage drastically drops to ~6%.<br />
However, the problem is that I have to use the QGraphicsView to play the video because I need to draw some lines and texts over the video based on the user's mouse clicks.<br />
My OS is macOS 10.15.7 and I am using PyQt5 version 5.15.2. I would appreciate it if someone please help me to know why there is such a difference between the performance of QVideoWidget and QGraphicsVideoItem.<br />
Is there any particular setting in the QGraphicsVideoItem to improve its rendering performance?<br />
It could help me to solve this problem.</p>
]]></description><link>https://forum.qt.io/topic/121387/high-cpu-usage-in-qgraphicsvideoitem</link><generator>RSS for Node</generator><lastBuildDate>Sat, 02 May 2026 09:48:07 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/121387.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 02 Dec 2020 20:23:07 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to High CPU usage in QGraphicsVideoItem on Thu, 10 Dec 2020 17:51:27 GMT]]></title><description><![CDATA[<p dir="auto">Hi <a class="plugin-mentions-user plugin-mentions-a" href="/user/kent-dorfman">@<bdi>Kent-Dorfman</bdi></a> , thanks for your reply.<br />
In order to show the video, I am using a <em>QGraphicsVideoItem</em> added in a <em>QGraphicsScene</em>. So, the drawings are added in the same <em>QGraphicsScene</em> as <em>QGraphicsItem</em>. In this method, I do not override the paint(); everything including the rendering of drawings and video as well as the scaling of the drawings -when I resize the window- are managed by <em>QGraphicsView</em>.<br />
Regarding using a regular <em>QVideoWidget</em>, I tried to override the paintEvent() of the <em>QVideoWidget</em> but it did not work! Because after playing the video, the graphics are obscured by the video.  Also, I tried to put a transparent background <em>QWidget</em> over a <em>QVideoWidget</em>, but I did not manage to show the <em>QWidget</em> over the video! I think the point is that I did not override paint() in each video frame as you said. However, regarding that I am new in PyQt5 I would appreciate it if you please kindly help me to solve the problem.</p>
<p dir="auto">Besides, as for the MP4 format, is it required to take any particular action?</p>
<p dir="auto">This is the sample code I used to show the video and overlaying a drawing. Reference: <a href="https://stackoverflow.com/questions/53899740/how-to-draw-qtgraphicsview-on-top-of-qvideowidget-with-transparency" target="_blank" rel="noopener noreferrer nofollow ugc">https://stackoverflow.com/questions/53899740/how-to-draw-qtgraphicsview-on-top-of-qvideowidget-with-transparency</a>.</p>
<pre><code>class Widget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(Widget, self).__init__(parent)

        self._scene = QtWidgets.QGraphicsScene(self)

        self._gv = QtWidgets.QGraphicsView(self._scene)

        self._videoitem = QGraphicsVideoItem() 
        self._scene.addItem(self._videoitem)

       self._line_item = QtWidgets.QGraphicsLineItem(0, 0, 320, 240, self._videoitem)
        self._line_item.setPen(QtGui.QPen(QtCore.Qt.red))
        self._scene.addItem(self._line_item)

        self._player = QMediaPlayer(self, QMediaPlayer.VideoSurface)
        self._player.setVideoOutput(self._videoitem.videoSurface())
        file = os.path.join(os.path.dirname(__file__), "GOPR0196.MP4")
        self._player.setMedia(QMediaContent(QtCore.QUrl.fromLocalFile(file)))

        button = QtWidgets.QPushButton("Play")
        button.clicked.connect(self.play)

        self.resize(640, 480)
        lay = QtWidgets.QVBoxLayout(self)
        lay.addWidget(self._gv)
        lay.addWidget(button)

    def play(self):
        oglWidget = QOpenGLWidget()
        self._gv.setViewport(oglWidget)

        if self._player.state() == QMediaPlayer.PlayingState:
            self._player.pause()
        else:
            self._player.play()

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())
</code></pre>
]]></description><link>https://forum.qt.io/post/631884</link><guid isPermaLink="true">https://forum.qt.io/post/631884</guid><dc:creator><![CDATA[Sh.m.z]]></dc:creator><pubDate>Thu, 10 Dec 2020 17:51:27 GMT</pubDate></item><item><title><![CDATA[Reply to High CPU usage in QGraphicsVideoItem on Thu, 10 Dec 2020 06:47:35 GMT]]></title><description><![CDATA[<p dir="auto">Am I correct in assuming that you override paint() in the class to add your drawing overlay?  That would seem the most straightforward method to add drawing to the video frame.</p>
<p dir="auto">Other option (I haven't tested) would be to use regular QVideoWidget and overlay a transparent background widget where you paint() the overlay primitives after display of each video frame.</p>
<p dir="auto">And keep in mind that MP4 is a container format that could contain different codes, which may be hogs, especially with HD format video.</p>
<p dir="auto">Just some thoughts.</p>
]]></description><link>https://forum.qt.io/post/631782</link><guid isPermaLink="true">https://forum.qt.io/post/631782</guid><dc:creator><![CDATA[Kent-Dorfman]]></dc:creator><pubDate>Thu, 10 Dec 2020 06:47:35 GMT</pubDate></item></channel></rss>