Python scripting missing bindings to frontend events, making it hard to trigger on stream start/stop

TheAstropath

New Member
Hey all,

I'm working on what I had thought would be a simple script in Python for OBS Studio, and am now repeatedly hitting dead ends. Here's what I'm trying to accomplish:

When you start streaming or recording, make a call to a URL to turn on a physical light switch
When you stop streaming or recording, make another call to a different URL to turn off that light switch.

I got all the configuration options set up so the URLs can be defined in the Scripts menu, with buttons to test the URLs (And it does, in fact, turn the light on and off). So far so good.

The first step to proceeding is obviously to get some sort of callback setup to execute my code when the stream (or recording) starts or stops. In the API, there are "obs_frontend_event"s defined that looked great:
-OBS_FRONTEND_EVENT_STREAMING_STARTED
-OBS_FRONTEND_EVENT_STREAMING_STOPPED
-OBS_FRONTEND_EVENT_RECORDING_STARTED
-OBS_FRONTEND_EVENT_RECORDING_STOPPED
I would just need to register a callback using obs_frontend_add_event_callback... Except there isn't a binding for that API call in obspython. OK, next option then.

I saw there was an actual way to get to the signal handler for an output and receive start/stop events for individual outputs (obs_output_get_signal_handler and signal_handler_connect). I got that all rigged up, and everything seemed to be working great - lights went on when I started streaming/recording, lights went off when I stopped. That is, until I switched to a different profile in OBS. At that point, my callbacks weren't getting called. I assume when you switch profiles, the outputs get destroyed and new ones created with the new profile settings.

OK then, I just need to know when you switch profiles so that I can re-register my callbacks, that shouldn't be too bad. Except it looks like the only way to get a notification of a profile change is through the frontend events, which I already discovered couldn't be done in Python.

I even went so far as to try just removing and re-adding the callbacks every 5 seconds using the timer callbacks. For some reason though, despite using signal_handler_disconnect before doing another signal_handler_connect, I was still getting more and more callbacks per start/stop the longer I waited (as though the signal_handler_disconnect wasn't actually removing the callback?).

Is my only option here really to just periodically poll obs_frontend_streaming_active and obs_frontend_recording_active to see if OBS is alive? Is there any particular reason I can't register for frontend events?
 

Marcedo

Member
From what i read lua Frontend callbacks should work. If you already through the point of tryin to poll for stuff, then a lua wrapper just calling your python logic might be argueable - besides doi that completely in lua, but that language just sucks if you ask me.
 

TheAstropath

New Member
Ugh that's annoying. I'm not really a big fan of LUA - I'd love to see the OBS team expand the capabilities of the Python scripting. In the meantime though, I've just gone to a pure polling method with Python. I can't see there being that much load or other issues being generated by polling once a second (or less frequently) to see if you are streaming or recording.
 

OZtwo

New Member
From what I understand you could use Script_save which is called every a button is pressed which then in that function check the status of the stream?
 

TheAstropath

New Member
If script_save gets called on every button press, that could be a good way to check if the profile was changed and if the callbacks need to be readded on the outputs. Thanks for the idea, I'll have to experiment with that.
 

OZtwo

New Member
NP. found it on error the other night when trying to figure out scripting. Still stuck on how to switch senses. :)
 

fenghuyu

New Member
I also encountered this problem in development, and ended up failing several times. Because there is no obs_frontend_add_event_callback method in the current _obspython.pyd file.In addition to the methods you mentioned, there is another solution, which is to use a third-party library[lupa], so that lua can run in python, so that we can get the data we want to monitor through LUA time. However, because it is more troublesome to configure in windows. so I didn't try it. I hope obspython can add these functions in the future.
 

Scratch

New Member
This is half a necrobump, but another half an update. So if anyone's actually still subscribed to this thread they should be notified.

The obs_frontend_add_event_callback method has been added into OBS for release 25, with it hitting release candidate 1 yesterday

it's not in the patchnotes, but it's there ;)
This should make it a lot easier to develop scripts that require it, sidestepping the LUA only methods

It only took two years
 

fenghuyu

New Member
This is half a necrobump, but another half an update. So if anyone's actually still subscribed to this thread they should be notified.

The obs_frontend_add_event_callback method has been added into OBS for release 25, with it hitting release candidate 1 yesterday

it's not in the patchnotes, but it's there ;)
This should make it a lot easier to develop scripts that require it, sidestepping the LUA only methods

It only took two years
Oh my god, finally this feature is available, thank you for your information。
 
Top