obs_source_update doesn't allow other functions to work

iszotic

New Member
Hi, The script is meant to load a playlist onto a VLC source, all ok and dandy, but then I want to continue playing the current video where it has left after the source is updated with a new playlist starting with the current video playing, after the conditional
Python:
if updated and first_playing and first_master
is where obs_source_media_set_time should be called but instead I have to do a workaround and change the time of the source in a second call of update_video storing the time of the media in the first call in the change_progress variable once the playlist is loaded.

I have tried getting/releasing the source a second time in the update_video with the function obs_source_media_set_time, or stalling the script with time.sleep after updating the source, but none of these worked.

Python:
import obspython as obs
import time
import os
import json
import datetime

stream_folder = ""
source_name = ""
change_progress = 0

def script_description():
    return 'controls media'

def script_update(settings):
    global stream_folder
    global source_name
    global change_progress

    stream_folder = obs.obs_data_get_string(settings, "stream_folder")
    source_name = obs.obs_data_get_string(settings, "source")
    if stream_folder != '' and source_name != "":
        obs.timer_add(update_video, 100)
            
def update_video():
    global stream_folder
    global source_name
    global change_progress

    playlist_path = os.path.join(stream_folder, 'playlist.txt')
    info_path = os.path.join(stream_folder, 'info.txt')
    with open(info_path, 'r') as f:
        raw = f.read()
        try:
            stream_info = json.loads(raw)
            title = stream_info['title']
        except:
            title = ''

    if os.path.exists(playlist_path):
        with open(playlist_path, 'r') as f:
            tmp = json.load(f)
        external_playlist = tmp['playlist']
        first_master = tmp['first_master']
        os.remove(playlist_path)
    else:
        external_playlist = []
        first_master = False

    source = obs.obs_get_source_by_name(source_name)
    updated = False
    first_playing = False
    progress = obs.obs_source_media_get_time(source)
    if change_progress > 0: #workaround because source update 'locks' the media
        obs.obs_source_media_set_time(source, change_progress)
        change_progress = 0

    if not source is None and len(external_playlist) > 0:

        settings = obs.obs_source_get_settings(source)
        playlist = obs.obs_data_get_array(settings, 'playlist')

        for i, media_path in enumerate(external_playlist):
            item = obs.obs_data_array_item(playlist, i)
            vlc_media_path = obs.obs_data_get_string(item, 'value')
            if media_path != vlc_media_path:
                obs.obs_data_set_string(item, 'value', media_path)
                obs.obs_data_set_array(settings, 'playlist', playlist)
                updated = True
            elif media_path == vlc_media_path and i == 0 and progress > 0:
                first_playing = True
            obs.obs_data_release(item)
        obs.obs_data_array_release(playlist)
        obs.obs_data_release(settings)
    if updated:
        obs.obs_source_update(source, settings)
    obs.obs_source_release(source)
    if updated and first_playing and first_master:
        change_progress = progress

def refresh_pressed(props, prop):
    update_video()

def script_properties():
    props = obs.obs_properties_create()
    obs.obs_properties_add_text(props, "stream_folder", "Stream folder", obs.OBS_TEXT_DEFAULT)
    p = obs.obs_properties_add_list(props, "source", "Media Source",
                obs.OBS_COMBO_TYPE_EDITABLE, obs.OBS_COMBO_FORMAT_STRING)
    sources = obs.obs_enum_sources()
    if sources is not None:
        for source in sources:
            source_id = obs.obs_source_get_unversioned_id(source)
            if 'vlc_source' in source_id:
                name = obs.obs_source_get_name(source)
                obs.obs_property_list_add_string(p, name, name)
        obs.source_list_release(sources)
    obs.obs_properties_add_button(props, "button", "Refresh", refresh_pressed)
    return props
 
Top