How to toggle on/off Qt video subtitles

0

I want to know whether I can toggle on/off subtitles of video using PyQt5. To enable subtitles I added

import os 
os.environ["QT_GSTREAMER_PLAYBIN_FLAGS"] = str(0x00000017)

before importing QGuiApplication. But we can't toggle it on/off while code is running. Is there any fix?

I can change the subtitle streams using these commands:

class PlayerHelper(QtCore.QObject):
    qmlplayerChanged = QtCore.pyqtSignal()

    def __init__(self, parent=None):
        super().__init__(parent)
        self.qml_player = None

    @QtCore.pyqtProperty(QtCore.QObject, notify=qmlplayerChanged)
    def qmlplayer(self):
        return self.qmlplayer

    @qmlplayer.setter
    def qmlplayer(self, player):
        self.qml_player = player
        self.qmlplayerChanged.emit()

    @staticmethod
    def get_stream_control(qmlplayer):
        mediaObject = qmlplayer.property("mediaObject")
        player = sip.cast(mediaObject, QtMultimedia.QMediaPlayer)
        control = player.service().requestControl(
            "org.qt-project.qt.mediastreamscontrol/5.0"
        )
        return sip.cast(control, QtMultimedia.QMediaStreamsControl)
    @QtCore.pyqtSlot(result=int)
    def subTitleCount(self):
        if not self.qml_player:
            return -1
        stream_control = self.get_stream_control(self.qml_player)
        count = 0
        for i in range(stream_control.streamCount()):
            if (
                stream_control.streamType(i)
                == QtMultimedia.QMediaStreamsControl.SubPictureStream
            ):
                count += 1
        return count
    @QtCore.pyqtSlot(result=int)
    def subTitleActive(self):
        if not self.qml_player:
            return -1
        stream_control = self.get_stream_control(self.qml_player)
        count = 0
        for i in range(stream_control.streamCount()):
            if (
                stream_control.streamType(i)
                == QtMultimedia.QMediaStreamsControl.SubPictureStream
            ):
                if stream_control.isActive(i):
                    return count
                count += 1
        return -1
    
    @QtCore.pyqtSlot(int)
    def setSubTitleActive(self, index):
        if not self.qml_player:
            return
        stream_control = self.get_stream_control(self.qml_player)
        count = 0
        for i in range(stream_control.streamCount()):
            if (
                stream_control.streamType(i)
                == QtMultimedia.QMediaStreamsControl.SubPictureStream
            ):
                if index == count:
                    stream_control.setActive(i, True)
                    return
                count += 1
    @QtCore.pyqtSolt()
    def nextTitle(self):
        if subTitleActive() == subTitleCount()-1:
            setSubTitleActive(0)
        else:
            setSubTitleActive(subTitleActive+1)

by running nextTitle() it would change currently playing captions.

if disabling is not possible is there any way to get Gstreamer subtitles as a string variable which changes dynamically.

python
python-3.x
pyqt
pyqt5
qmediaplayer
asked on Stack Overflow May 10, 2020 by Newtron Malayalam • edited Sep 14, 2020 by Jason Aller

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0