Possible to set the stream key / change OBS settings programmatically, via a plugin or script?

markusd1984

New Member
I'm looking for a way to update the stream key for scheduled events without having to go through settings, as each uses a unique key

happy to look into creating something if somebody can point me in the right direction? Should this be possible?
Is there any documentation on how to change OBS settings via a plugin / script?
 
Couple things to look at:
1) The streaming info and key are stored with the Profile. We use different keys for Sunday, Saturday, and funerals, so we have three profiles, each with the appropriate key (which we reuse each Sunday, etc.). Desktop shortcut to OBS for each profile, using the --profile commandline switch. User starts OBS with the appropriate profile.

2) At least for YouTube (the only streaming I have used), the key is stored in C:\Users\yourusername\AppData\Roaming\obs-studio\basic\profiles\SATURDAY\service.json (SATURDAY profile as an example). It's a json file, and the key is called "key". Your could hand-edit the file, or write a Python (or other language, but Python makes json easy) script to update the file before you run OBS.

3) While OBS is running, a Lua or Python script can READ the current key by calling obs_frontend_get_streaming_service, then obs_service_get_key. But I don't know of a function to SET the key. You can, however, change the profile in Lua by calling obs_frontend_set_current_profile. Wait a second or two after the call, since changing profile can take a while to set all the options.
 

markusd1984

New Member
Thanks John for sharing your setup and know-how. Profiles will work indeed if one uses different accounts for different purposes but without unique event keys in each account. We are utilising restream.io to stream to YT & FB simultaneously and more importantly to schedule the events in advance to promote it and so people can subscribe to notifications to the events, hence unique stream keys for each event.

Updating the .json seems an option / using pyhon and websocket plugin another way.

I tried using lua to provide a dock with an input field, but I ran into a problem that the plugin is not recognised by OBS:
Skipping module '../../obs-plugins/64bit/myplugin.dll', not an OBS plugin

any idea if this code is outdated or am missing something?

Python:
obs = obslua



-- Define a function that updates the stream key from the input field

function update_stream_key(props, p)

    local new_stream_key = obs.obs_properties_get_string(props.stream_key)

    local settings = obs.obs_data_create()

    obs.obs_data_set_string(settings, "key", new_stream_key)

    obs.obs_frontend_update_streaming_settings(settings)

    obs.obs_data_release(settings)

end



-- Define the properties for the input field and button

function myplugin_properties(props)

    local p = obs.obs_properties_create()

    obs.obs_properties_add_text(p, "stream_key", "Stream Key", obs.OBS_TEXT_DEFAULT)

    obs.obs_properties_add_button(p, "update_button", "Update Stream Key", update_stream_key)



    return p

end



-- Register the properties with OBS

obs.obs_properties_add_extern("myplugin", "My Plugin", myplugin_properties)

alternatively I tried using a dock but same issue
Python:
obs = obslua

-- Define a function that updates the stream key from the input field
function update_stream_key(props, p)
    local new_stream_key = obs.obs_properties_get_string(props.stream_key)
    local settings = obs.obs_data_create()
    obs.obs_data_set_string(settings, "key", new_stream_key)
    obs.obs_frontend_update_streaming_settings(settings)
    obs.obs_data_release(settings)
end

-- Define the properties for the input field and button
function myplugin_properties(props)
    local p = obs.obs_properties_create()

    -- Add a group to the properties
    local group = obs.obs_properties_add_group(p, "stream_key_group", "Stream Key", obs.OBS_GROUP_NORMAL)

    -- Add the input field to the group
    obs.obs_properties_add_text(group, "stream_key", "Stream Key", obs.OBS_TEXT_DEFAULT)

    -- Add the update button to the group
    obs.obs_properties_add_button(group, "update_button", "Update Stream Key", update_stream_key)

    return p
end

-- Define the dock widget
function myplugin_dock_widget(widget, container)
    local props = obs.obs_properties_create()
    obs.obs_properties_add_extern(props, "myplugin", "My Plugin")

    local p = obs.obs_properties_create()
    obs.obs_properties_add_list(p, "plugins", "Plugins", obs.OBS_COMBO_TYPE_LIST, obs.OBS_COMBO_FORMAT_STRING)

    -- Set the properties for the dock widget
    obs.obs_dock_widget_set_properties(widget, props)
    obs.obs_dock_widget_set_title(widget, "My Plugin")
    obs.obs_dock_widget_set_icon(widget, "icon.png")

    obs.obs_properties_release(props)
    obs.obs_properties_release(p)
end

-- Register the properties and dock widget with OBS
obs.obs_properties_add_extern("myplugin", "My Plugin", myplugin_properties)
obs.obs_frontend_add_dock_widget("myplugin", "My Plugin", myplugin_dock_widget)


FYI the plugin.json
JSON:
{
    "id": "com.example.myplugin",
    "name": "My Plugin",
    "description": "Provides an input field for a stream key and a button that updates the OBS stream key from the input field.",
    "author": "Your Name",
    "version": "1.0",
    "minimumOBS": "27.0.0",
    "tags": [ "stream key", "update" ],
    "url": "https://example.com/myplugin",
    "authorUrl": "https://example.com"
}

and en-US.ini
Code:
[Plugin]
NAME = My Plugin
DESCRIPTION = Provides an input field for a stream key and a button that updates the OBS stream key from the input field.
 
I have done browser docks in JavaScript to control PTZ cameras, and control OBS with Websocket. But as far as I know, GUI for Lua is restricted to the script dialog.

Do you need to update the key while OBS is running? If the keys are generated in advance you wouldn't need to couple your updater to OBS. Or make a command file or PowerShell script that updates the key used by a profile, and then runs OBS.

I don't know restream.io, but YouTube keys can be reused: each scheduled stream gets a unique URL that you can distribute. The only restriction is that you can only have one pending scheduled stream on each key. So reuse works if you can tie the key to a day, or the type of event, and if you don't need to schedule too far in advance.
 
Top