[PyQt] Access to QMediaStreamsControl

Maurizio Berti maurizio.berti at gmail.com
Sun Apr 19 21:13:23 BST 2020


The following code should work (taken from
https://stackoverflow.com/a/61296171/2001654 ):

import os
from PyQt5 import QtCore, QtWidgets, QtMultimedia, QtMultimediaWidgets
import sip

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        video_widget = QtMultimediaWidgets.QVideoWidget()
        self.player = QtMultimedia.QMediaPlayer(
            self, QtMultimedia.QMediaPlayer.VideoSurface
        )
        file = os.path.join(os.path.dirname(os.path.realpath(__file__)),
"test5.mkv")
        self.player.setMedia(
            QtMultimedia.QMediaContent(QtCore.QUrl.fromLocalFile(file))
        )
        self.player.setVideoOutput(video_widget)
        self.player.play()
        self.setCentralWidget(video_widget)

        control = self.player.service().requestControl(
            "org.qt-project.qt.mediastreamscontrol/5.0"
        )
        self.qcontrol = sip.cast(control, QtMultimedia.QMediaStreamsControl)
        self.resize(640, 480)

    def contextMenuEvent(self, event):
        menu = QtWidgets.QMenu()
        group = QtWidgets.QActionGroup(menu)
        group.setExclusive(True)
        index = 0
        for i in range(self.qcontrol.streamCount()):
            t = self.qcontrol.streamType(i)
            if t == QtMultimedia.QMediaStreamsControl.AudioStream:
                action = menu.addAction("Audio-{}".format(index))
                action.setCheckable(True)
                if self.qcontrol.isActive(i):
                    action.setChecked(True)
                action.setData(i)
                menu.addAction(action)
                index += 1
        action = menu.exec_(self.mapToGlobal(event.pos()))
        if action is not None:
            i = action.data()
            self.qcontrol.setActive(i, True)


I tried the pyside alternative proposed in the link, but unfortunately I'm
unable to successfully load the file on PySide due to the following error:
ERROR: Caught a segmentation fault while loading plugin file:
/usr/lib/x86_64-linux-gnu/gstreamer-1.0/libgsttypefindfunctions.so

The same file correctly loads on PyQt5 (but obviously there's no
QMediaStreamsControl), so I suppose there's some problem with my
installation of PySide.

The sample (test5.mkv) is available in the Matroska test files:
https://github.com/Matroska-Org/matroska-test-files

Thank you,
Maurizio

Il giorno dom 19 apr 2020 alle ore 10:17 Phil Thompson <
phil at riverbankcomputing.com> ha scritto:

> On 18/04/2020 19:06, Maurizio Berti wrote:
> > Il giorno sab 18 apr 2020 alle ore 14:24 Phil Thompson <
> > phil at riverbankcomputing.com> ha scritto:
> >
> >> When I don't understand an API properly then I tend to leave it until
> >> somebody complains and then I've got someone more knowledgeable who I
> >> can ask about it.
> >>
> >>  From what you say, all I need to do is to wrap the various
> >> QMediaControl
> >> sub-classes (all 39 of them). Is that right?
> >>
> >
> > It seems like so, but honestly I'm not an expert, I don't know if that
> > would be enough and, most importantly, I wouldn't be able to test it in
> > the
> > near future because I cannot update my current setups to newer PyQt
> > releases at the moment.
> >
> > Right now at least I know that it doesn't work because it's not been
> > implemented yet, but, of course, adding support for those classes could
> > be
> > a good thing, as I might be interested in using them in the future.
>
> Any chance you can send me a short script that *should* work and a test
> audio stream?
>
> Phil
>


-- 
È difficile avere una convinzione precisa quando si parla delle ragioni del
cuore. - "Sostiene Pereira", Antonio Tabucchi
http://www.jidesk.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20200419/e3868f01/attachment-0001.htm>


More information about the PyQt mailing list