mirror of
https://github.com/onyx-and-iris/obsws-python.git
synced 2026-04-05 16:39:10 +00:00
54 lines
1.6 KiB
Python
54 lines
1.6 KiB
Python
import os
|
|
from threading import Event
|
|
|
|
import obsws_python as obs
|
|
|
|
|
|
class Observer:
|
|
def __init__(self, host, port, password, stop_event):
|
|
self._client = obs.EventClient(host=host, port=port, password=password)
|
|
self._stop_event = stop_event
|
|
self._client.callback.register(
|
|
[
|
|
self.on_current_program_scene_changed,
|
|
self.on_scene_created,
|
|
self.on_input_mute_state_changed,
|
|
self.on_exit_started,
|
|
]
|
|
)
|
|
print(f"Registered events: {self._client.callback.get()}")
|
|
|
|
def __enter__(self):
|
|
return self
|
|
|
|
def __exit__(self, exc_type, exc_value, exc_traceback):
|
|
self._client.disconnect()
|
|
|
|
def on_current_program_scene_changed(self, data):
|
|
"""The current program scene has changed."""
|
|
print(f"Switched to scene {data.scene_name}")
|
|
|
|
def on_scene_created(self, data):
|
|
"""A new scene has been created."""
|
|
print(f"scene {data.scene_name} has been created")
|
|
|
|
def on_input_mute_state_changed(self, data):
|
|
"""An input's mute state has changed."""
|
|
print(f"{data.input_name} mute toggled")
|
|
|
|
def on_exit_started(self, _):
|
|
"""OBS has begun the shutdown process."""
|
|
print("OBS closing!")
|
|
self._stop_event.set()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
host = os.getenv("OBSWS_HOST", "localhost")
|
|
port = int(os.getenv("OBSWS_PORT", 4455))
|
|
password = os.getenv("OBSWS_PASSWORD", "")
|
|
|
|
stop_event = Event()
|
|
|
|
with Observer(host, port, password, stop_event) as observer:
|
|
stop_event.wait()
|