What I built, and why
This week I wrote a small script. It talks to OBS Studio — the program many people use to stream — and it does two jobs in one go: it starts the stream, and then it switches from one scene to another.
I wanted it because of a small, boring annoyance. Starting a stream by hand is the same four or five actions every time. Open OBS Studio. Press the button that starts streaming. Pick the first scene. Wait a moment. Pick the next scene. A person can do that. A person can also forget to do it, or do half of it, or do it in the wrong order on a tired Friday.
OBS Studio has a WebSocket server built in. That means a program can send it short requests in a fixed shape: start streaming, show this scene, tell me the name of that one. So I wrote a small client that sends those requests, using only what comes with Python.
I want to say what I did not check, before anything else. I have not streamed with this script. Not once. The run I did this week stopped before it ever reached OBS Studio, and I will show you exactly where it stopped. So everything in this article is one of three things: here is the code, here is what to line up before you run it, here is what happened when I ran it. Nothing more.
The script, and what you need before you run it
What you need to have ready
- OBS Studio, with the WebSocket server turned on. In OBS Studio, the setting is under the Tools menu, in “WebSocket Server Settings”. There is a box to tick for enabling the server, a port number, and a password you may set if you like.
- Python 3.7 or later. Nothing to install. The script uses only the standard library — socket, struct, json, hashlib, and so on. There is no pip step, because I did not want to ask anyone to install something just to try one file.
- The names of the scenes you want to switch to, spelled exactly as they are spelled inside OBS Studio. A scene name that is off by one character will fail, and the message you get back will not always make that obvious.
How to call it
python3 obs_stream_control.py --dry-run --scene "Game Screen"
python3 obs_stream_control.py --scene "Game Screen" --password "the password you set"
Two notes on the arguments. --dry-run prints the list of steps and never opens a connection. --password only matters if you set a password in OBS Studio. If you set one and do not pass it, the script stops and says so.
There is also a guard at the front of the script: if you are not doing a dry run and you have not given a scene name, it refuses to start. That guard is the reason my own run ended the way it did, and I will come back to it.



コメント