Python script & LibVLC: Music inside OBS with current title and position

Suk0m8u

New Member
Hello, dear community.

Currently i need inside OBS an simple text field with the current playing title and current position of my background music from YouTube.
So i decided to write an simple test script and that works:

Python:
#!/usr/bin/env python3

from time import sleep

from datetime import timedelta

from random import shuffle

from vlc import Instance as vlcInstance, MediaList, MediaListPlayer, EventType, State


class MusicPlayer:

    def __init__(self, mrls):

        self.prepare()

        self.load(mrls)


    def prepare(self):

        self.player = MediaListPlayer(vlcInstance("--no-ts-trust-pcr", "--ts-seek-percent", "--no-video", "-q"))

        eventManager = self.player.event_manager()

        eventManager.event_attach(EventType.MediaListPlayerNextItemSet, self.updateTitle)


    def load(self, mrls):

        shuffle(mrls)

        self.player.set_media_list(MediaList(mrls))


    def play(self):

        self.player.play()

        while self.player.get_state() != State.Ended:

            self.updatePosition()

            sleep(0.5)


    def updateTitle(self, event):

        media = self.player.get_media_player().get_media()

        if media.get_meta(1) != None:

            print(media.get_meta(0))


    def updatePosition(self):

        currentPosition = str(timedelta(seconds=self.player.get_media_player().get_time() / 1000, microseconds=0)).split(".")[0]

        print(currentPosition)


mrls = [

    "https://www.youtube.com/watch?v=WEcPAs9gC70",

    "https://www.youtube.com/watch?v=EJTZms5PFg8"]


musicPlayer = MusicPlayer(mrls)

musicPlayer.play()


Okay, now i try to rewrite it step by step for usage inside OBS but even if i uncomment line 16, then OBS closes itself with the message "Segmentation fault".
So what's my mistake?


Python:
import obspython as obs
from time import sleep
from datetime import timedelta
from random import shuffle
from vlc import Instance as vlcInstance, MediaList, MediaListPlayer, EventType, State
import obspython as obs

class MusicPlayer:
    def __init__(self, mrls):
        print("NEW")
        self.prepare()
        #self.load(mrls)

    def prepare(self):
        print("test")
        self.player = MediaListPlayer(vlcInstance("--no-ts-trust-pcr", "--ts-seek-percent", "--no-video", "-q"))
        #eventManager = self.player.event_manager()
        #eventManager.event_attach(EventType.MediaListPlayerNextItemSet, self.updateTitle)

    def load(self, mrls):
        shuffle(mrls)
        self.player.set_media_list(MediaList(mrls))

    def play(self):
        self.player.play()
        while self.player.get_state() != State.Ended:
            self.updatePosition()
            sleep(0.5)

    def updateTitle(self, event):
        media = self.player.get_media_player().get_media()
        if media.get_meta(1) != None:
            print(media.get_meta(0))

    def updatePosition(self):
        currentPosition = str(timedelta(seconds=self.player.get_media_player().get_time() / 1000, microseconds=0)).split(".")[0]
        print(currentPosition)

    def test(self):
        print("OK")

mrls = [
    "https://www.youtube.com/watch?v=WEcPAs9gC70",
    "https://www.youtube.com/watch?v=EJTZms5PFg8"]

musicPlayer = MusicPlayer(mrls)

def add_pressed(props, prop):
    musicPlayer.test()

# Script description
def script_description():
    return "Simple music player"

# Configuration dialog
def script_properties():
    ui = obs.obs_properties_create()
    obs.obs_properties_add_button(ui, "button", "Change text source", add_pressed)
    obs.obs_properties_add_text(ui, "playlist", "Playlist", obs.OBS_TEXT_MULTILINE)
    return ui

Thanks, very much for your help!
 

Suk0m8u

New Member
Okay, on Windows 10 + Python 3.6.8 it works but not with Python 3.9.
On Linux it doesn't work with Python 3.9.3 but there is no 'Python settings' tab.
So how can i switch the Python version for OBS on Linux?
 

Suk0m8u

New Member
Okay, sometimes i am just stupid.
Also don't need threads for my case.
So this topic is solved for me.
How can i close it?
 
Top