pythonpyqt5twitchqwebengineview

PyQT5 QWebEngineView Twitch


I'm experimenting with QWebEngineView and Twitch streams. Currently it seems that it is unable to play the streams in the web engine. I'm getting the following error while trying to load a stream.

js: Player stopping playback - error MasterPlaylist:11 (ErrorNotAvailable code 404 - Failed to load playlist)
js: Player stopping playback - error Player:2 (ErrorNotSupported code 0 - No playable format)

I'm wondering if anyone know how this might be fixed. Thanks. Also if you have a completely different approach to loading twitch streams in python feel free.

Here is some test code.

from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEngineSettings
from qframelesswindow import FramelessWindow, StandardTitleBar
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import QUrl
import sys


class CustomTitleBar(StandardTitleBar):
    """ Custom title bar """

    def __init__(me, parent):
        super().__init__(parent)


class Window(FramelessWindow):
    def __init__(me, parent=None):
        super().__init__(parent=parent)
        me.setTitleBar(CustomTitleBar(me))
        me.titleBar.raise_()
        screen = me.screen().geometry()
        x = int((screen.width() - 800) / 2)
        y = int((screen.height() - 450) / 2)
        me.setGeometry(x, y, 800, 450)

    def showLive(me, follow):
        # open stream in qwebengineview
        me.stream = QWebEngineView(me)
        settings = me.stream.settings()
        settings.setAttribute(QWebEngineSettings.PluginsEnabled, True)
        settings.setAttribute(
            QWebEngineSettings.FullScreenSupportEnabled, True)
        me.stream.setGeometry(0, 64, me.width(), me.height() - 64)
        me.stream.load(QUrl(f"https://www.twitch.tv/{follow}"))
        me.stream.show()


app = QApplication(sys.argv)
window = Window()
window.show()
window.showLive("3v1lxd")
sys.exit(app.exec_())


Solution

  • I've found a solution using streamlink and VLC

    from qframelesswindow import FramelessWindow, StandardTitleBar
    from PyQt5.QtWidgets import QApplication, QFrame
    import streamlink
    import sys
    import vlc
    
    
    class CustomTitleBar(StandardTitleBar):
        def __init__(me, parent):
            super().__init__(parent)
    
    
    class Window(FramelessWindow):
        def __init__(me, parent=None):
            super().__init__(parent=parent)
            me.setTitleBar(CustomTitleBar(me))
            me.titleBar.raise_()
            screen = me.screen().geometry()
            x = int((screen.width() - 800) / 2)
            y = int((screen.height() - 450) / 2)
            me.setGeometry(x, y, 800, 450)
            me.stream = QFrame(me)
            me.stream.setGeometry(0, 0, 800, 450)
            me.instance = vlc.Instance()
    
        def showLive(me, follow):
            # open stream in streamlink
            session = streamlink.Streamlink()
            streams = session.streams(f"https://www.twitch.tv/{follow}")
            if streams is not None:
                stream = streams["best"]
                # open stream in vlc player and add to window
                player = vlc.MediaPlayer(stream.url)
                player.set_hwnd(me.stream.winId())
                player.play()
    
    
    app = QApplication(sys.argv)
    window = Window()
    window.show()
    window.showLive("pokelawls")
    sys.exit(app.exec_())