keepcalculator

06 Numbers and Powers

Log OBS Stream Status to File 24/7

Card No. 06·88807 · Updated · Standard formula, shown below

This guide shows you how to automatically log your OBS Studio stream status (live, offline, reconnecting) to a text file during a 24/7 broadcast. You’ll set up a simple script that checks OBS’s status every few seconds and appends a timestamped line to a log file, giving you a permanent record of every drop, reconnect, and uptime stretch. You’ll need OBS Studio running, a basic text editor, and either Windows PowerShell or a Linux/macOS terminal.

What You’ll Need

  • OBS Studio (version 28 or newer recommended) installed and configured for your stream.
  • OBS WebSocket enabled: In OBS, go to Tools > WebSocket Server Settings, check Enable WebSocket server, note the port (default 4455), and set a password if you want.
  • A script editor (Notepad, VS Code, nano) to create the logging script.
  • For Windows: PowerShell 5.1 or later (comes with Windows 10/11). For Linux/macOS: Python 3 or bash.

Step-by-Step

  1. Enable OBS WebSocket: Open OBS Studio, go to Tools > WebSocket Server Settings. Check Enable WebSocket server. Set a password (e.g., mypassword) and note the port (default 4455). Click OK.
  2. Get your OBS WebSocket URL: The WebSocket address is usually ws://127.0.0.1:4455 (if OBS is on the same computer). If OBS is on another machine, use its IP address.
  3. Create the logging script: Open a text editor and create a new file. Below are two scripts – one for Windows PowerShell and one for Python (cross-platform). Choose the one that fits your system.
  4. For Windows – PowerShell script : Copy and paste the following into your file, save as obs-logger.ps1 : # obs-logger.ps1 $wsUrl = “ws://127.0.0.1:4455” $password = “mypassword” $logFile = “C:\obs-stream-status.log” # Connect to OBS WebSocket $ws = New-Object System.Net.WebSockets.ClientWebSocket $ws.ConnectAsync([Uri]$wsUrl, [Threading.CancellationToken]::None).Wait() # Authenticate (simplified – you may need to handle auth challenge) # For simplicity, this example assumes no auth or uses a token. In practice, you’ll need to implement the auth handshake. while ($true) { # Send a request to get stream status $bytes = [System.Text.Encoding]::UTF8.GetBytes($request) $ws.SendAsync([ArraySegment[byte]]$bytes, [System.Net.WebSockets.WebSocketMessageType]::Text, $true, [Threading.CancellationToken]::None).Wait() # Receive response $buffer = New-Object byte[] 4096 $result = $ws.ReceiveAsync([ArraySegment[byte]]$buffer, [Threading.CancellationToken]::None).Result $response = [System.Text.Encoding]::UTF8.GetString($buffer, 0, $result.Count) # Parse JSON (requires ConvertFrom-Json) $json = $response | ConvertFrom-Json $streaming = $json.d.responseData.streaming $status = if ($streaming) { “LIVE” } else { “OFFLINE” } $timestamp = Get-Date -Format “yyyy-MM-dd HH:mm:ss” Add-Content -Path $logFile -Value “$timestamp – $status” Start-Sleep -Seconds 5 } Note: The above is a simplified example. For production, you must implement the OBS WebSocket authentication handshake (op 1) and error handling.
  5. For Python (cross-platform): Install the obs-websocket-py library: pip install obs-websocket-py. Then create obs_logger.py with:
    import obswebsocket, obswebsocket.requests, time, datetime
    
    host = "localhost"
    port = 4455
    password = "mypassword"
    log_file = "obs_stream_status.log"
    
    ws = obswebsocket.obsws(host, port, password)
    ws.connect()
    
    try:
        while True:
            status = ws.call(obswebsocket.requests.GetStreamStatus())
            streaming = status.getStreaming()
            state = "LIVE" if streaming else "OFFLINE"
            timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
            with open(log_file, "a") as f:
                f.write(f"{timestamp} - {state}\n")
            time.sleep(5)
    except KeyboardInterrupt:
        pass
    finally:
        ws.disconnect()
        

    Save the file.

  6. Run the script: For PowerShell, open PowerShell as administrator (if needed), navigate to the script folder, and run .\.\obs-logger.ps1. For Python, run python obs_logger.py. Leave the terminal window open.
  7. Verify logging: Start your stream in OBS. After a few seconds, check the log file (e.g., obs_stream_status.log). You should see lines like 2025-01-01 12:00:00 - LIVE. Stop the stream and you’ll see OFFLINE entries.
  8. Automate at startup (optional): To run the script automatically when your computer boots, create a scheduled task (Windows) or a systemd service (Linux) that runs the script. This ensures logging runs 24/7 even if you restart the PC.

Common Mistakes

  • Wrong WebSocket port or password: Double-check the port and password in OBS’s WebSocket settings. A mismatch will cause connection errors.
  • Not enabling WebSocket: If the WebSocket server is off, the script cannot connect. Ensure it’s enabled.
  • Using the wrong script for your OS: PowerShell scripts won’t run on Linux without PowerShell installed; Python scripts are more portable.
  • Log file not writable: If the log file path is in a protected directory (like Program Files), the script may fail. Use a user-writable folder like C:\Users\YourName\ or ~/.
  • Script stops after a disconnect: If OBS restarts or the WebSocket drops, the script may crash. Add reconnect logic (not shown in the simplified examples) to make it robust.

Tips

  • Log more than just status: You can also log the stream duration, bitrate, or dropped frames by extending the script to request GetStreamStatus returns more data.
  • Use a timestamp format that’s easy to sort: ISO 8601 (YYYY-MM-DD HH:MM:SS) sorts lexicographically and is human-readable.
  • Rotate the log file: For 24/7 logging, the file can grow large. Use a script to split logs daily (e.g., obs-status-2025-01-01.log).
  • Run as a background service: Instead of a visible terminal, use nohup (Linux) or a Windows service wrapper to run the script silently.
  • Test the script before going live: Start the script, start and stop a test stream, and verify the log entries appear correctly.

Troubleshooting

  • Script fails to connect: Check that OBS is running and WebSocket is enabled. Test the connection using a WebSocket client (e.g., websocat) or the OBS WebSocket test page. Ensure the port is open (if connecting remotely).
  • Authentication errors: The Python script handles auth automatically. For PowerShell, you must implement the auth handshake (op 1 with challenge). If you get an auth error, verify the password and that you’re using the correct protocol version (OBS WebSocket v5).
  • No log entries: Check the script’s console output for errors. Ensure the log file path is correct and writable. Run the script with elevated permissions if needed.
  • Script crashes after OBS restarts: Add a retry loop that attempts to reconnect every few seconds. The Python library may throw an exception; catch it and retry.
  • Log entries are all OFFLINE even when streaming: The script might be reading the wrong field. In OBS WebSocket v5, the response field is outputActive for the streaming output. Update the script to use outputActive instead of streaming.

Frequently Asked Questions

Can I log other stream metrics like bitrate or dropped frames?

Yes, the GetStreamStatus request returns fields like outputBytes, outputSkippedFrames, and outputTotalFrames. You can append these to your log line.

Will this work if OBS is on a different computer?

Yes, as long as the WebSocket server is accessible over the network. Use the correct IP address and ensure the port is open in the firewall.

How do I stop the logging script?

For a terminal script, press Ctrl+C. For a background service, stop the service or kill the process.

Can I log to a database instead of a file?

Yes, modify the script to insert into a SQLite or MySQL database. This allows easier querying of uptime and outages.

Does this work with OBS 27 or older?

OBS WebSocket was integrated into OBS 28. For older versions, you need to install the obs-websocket plugin separately and use protocol v4, which has different request/response formats.

What if my stream goes offline due to internet outage?

The script logs the status as reported by OBS. If OBS itself loses connection to the streaming service, it may show ‘Reconnecting’ or ‘Offline’. You can log the outputReconnecting field to capture that state.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →