[PyQt] Rewinding a video via QMediaPlayer.setPlaybackRate

Božo Stojković bozo.stojkovic at gmail.com
Wed Sep 12 17:37:09 BST 2018


Hello,

This is my first time using the mailing list, so please be gentle :)

I am trying to rewind a video by setting a negative value for
QMediaPlayer.playbackRate by using QMediaPlayer.setPlaybackRate.

Per documentation
<http://doc.qt.io/qt-5/qmediaplayer.html#playbackRate-prop>:

> Values less than zero can be set and indicate the media will rewind at the
> multiplier of the standard pace.


But instead of playing in reverse (rewinding; take a look at this video
<https://www.youtube.com/watch?v=UBnI-oH0YtI&t=49s> if you're not sure what
I mean), the video just keeps playing at last positive speed which was set.

I tried searching up and down through everything I could think of. I found
nothing.

I tried to search the mailing list archive using Nabble
<http://python.6.x6.nabble.com/PyQt-f1792048.html>, but the search turned
out with nothing when I used following queries: qmediaplayer (rewind OR
reverse)
<http://python.6.x6.nabble.com/template/NamlServlet.jtp?macro=search_page&node=1792048&query=message%3Aqmediaplayer+message%3A%28rewind+OR+reverse%29&days=0>
, qmediaplayer rewind
<http://python.6.x6.nabble.com/template/NamlServlet.jtp?macro=search_page&node=1792048&query=message%3Aqmediaplayer+message%3Arewind&days=0>
, qmediaplayer reverse
<http://python.6.x6.nabble.com/template/NamlServlet.jtp?macro=search_page&node=1792048&query=message%3Aqmediaplayer+message%3Areverse&days=0>
, mediaplayer rewind
<http://python.6.x6.nabble.com/template/NamlServlet.jtp?macro=search_page&node=1792048&query=message%3Amediaplayer+message%3Arewind&days=0>
, mediaplayer reverse
<http://python.6.x6.nabble.com/template/NamlServlet.jtp?macro=search_page&node=1792048&query=message%3Amediaplayer+message%3Areverse&days=0>
, player rewind
<http://python.6.x6.nabble.com/template/NamlServlet.jtp?macro=search_page&node=1792048&query=message%3Aplayer+message%3Arewind&days=0>
, player reverse
<http://python.6.x6.nabble.com/template/NamlServlet.jtp?macro=search_page&node=1792048&query=message%3Aplayer+message%3Areverse&days=0>,
playback rewind
<http://python.6.x6.nabble.com/template/NamlServlet.jtp?macro=search_page&node=1792048&query=message%3Aplayback+message%3Arewind&days=0>,
playback negative
<http://python.6.x6.nabble.com/template/NamlServlet.jtp?macro=search_page&node=1792048&query=message%3Aplayback+message%3Anegative&days=0>

I only got results when I tried qmediaplayer playback
<http://python.6.x6.nabble.com/template/NamlServlet.jtp?macro=search_page&node=1792048&query=message%3Aqmediaplayer+message%3Aplayback&days=0>
but
those were irrelevant.

I asked the same question on Stack Overflow
<https://stackoverflow.com/questions/52155722/qmediaplayer-negative-playbackrate-doesnt-rewind-video>,
where you can find my minimal, complete, and verifiable example code. I am
also sending my code as attachment for convenience, I guess.

A user on SO mentioned
<https://stackoverflow.com/questions/52155722/qmediaplayer-negative-playbackrate-doesnt-rewind-video#comment91545440_52155722>
that this feature might not be supported by the backend, so I'm not sure
where else to look than here.

My specs:
OS Name: Microsoft Windows 10 Pro
OS Version: 10.0.17134
Python version: 3.7.0
PyQt version: 5.11.2

I read through the whole "Getting Answers" guide
<https://www.mikeash.com/getting_answers.html>, and hope I did well.

Thank you in advance!

Regards,
Božo Stojković
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20180912/ac0eaaee/attachment.html>
-------------- next part --------------
import sys

from PyQt5.QtCore import Qt, QUrl, QEvent
from PyQt5.QtMultimedia import QMediaContent, QMediaPlayer
from PyQt5.QtMultimediaWidgets import QVideoWidget
from PyQt5.QtWidgets import QMainWindow, QApplication

class VideoWindow(QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)

        videoWidget = QVideoWidget()
        self.setCentralWidget(videoWidget)

        self.mediaPlayer = QMediaPlayer(None, QMediaPlayer.VideoSurface)
        self.mediaPlayer.setVideoOutput(videoWidget)
        self.mediaPlayer.setMedia(
            QMediaContent(QUrl.fromLocalFile(r"<some video file>.mp4"))
        )
        self.mediaPlayer.play()

        app.installEventFilter(self)

    def eventFilter(self, source, event):
        if event.type() == QEvent.KeyPress:
            key = event.key()

            if key == Qt.Key_A:
                self.mediaPlayer.setPlaybackRate(1.0)
            elif key == Qt.Key_Y:
                self.mediaPlayer.setPlaybackRate(-1.0) # This doesn't work! :(

        return super().eventFilter(source, event)

    def closeEvent(self, event):
        self.mediaPlayer.setMedia(QMediaContent())

if __name__ == '__main__':
    app = QApplication(sys.argv)

    player = VideoWindow()
    player.resize(640, 480)
    player.show()

    exitCode = app.exec_()
    sys.exit(exitCode)


More information about the PyQt mailing list