# OBS Browser Source Manager

This guide demonstrates how to programmatically control which website is displayed in an OBS Browser Source. This is useful for dynamic overlays, rotating dashboards, or remote-controlled displays.

**Modules Used:**
*   `obsws-python`: To communicate with OBS via WebSocket.
*   [[programming/python/modules/argparse-module|argparse]]: To handle command-line arguments.
*   `time`: For delays (if cycling URLs).

## Prerequisites

1.  **OBS Studio** installed.
2.  **WebSocket Server** enabled in OBS (Tools -> WebSocket Server Settings).
3.  A **Browser Source** created in your current scene (e.g., named "Browser").

## Installation

```bash
pip install obsws-python
```

## The Code

Save this as `obs_browser.py`.

```python
import obsws_python as obs
import argparse
import time
import sys

# Default Config (Can be overridden by CLI args)
HOST = 'localhost'
PORT = 4455
PASSWORD = 'your_password_here'

def get_client(password):
    try:
        return obs.ReqClient(host=HOST, port=PORT, password=password)
    except Exception as e:
        print(f"Connection Error: {e}")
        sys.exit(1)

def set_source_url(client, source_name, url):
    try:
        # 'url' is the standard setting key for Browser Sources
        # overlay=True merges with existing settings rather than replacing all
        client.set_input_settings(name=source_name, settings={'url': url}, overlay=True)
        print(f"Updated '{source_name}' to: {url}")
    except Exception as e:
        print(f"Failed to update source: {e}")

def cycle_urls(client, source_name, urls, interval):
    print(f"Cycling {len(urls)} URLs every {interval} seconds. Press Ctrl+C to stop.")
    try:
        while True:
            for url in urls:
                set_source_url(client, source_name, url)
                time.sleep(interval)
    except KeyboardInterrupt:
        print("\nStopped.")

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="OBS Browser Source Manager")
    parser.add_argument("source", help="Name of the Browser Source in OBS")
    parser.add_argument("urls", nargs='+', help="URL(s) to display")
    parser.add_argument("--password", default=PASSWORD, help="OBS WebSocket Password")
    parser.add_argument("--cycle", type=int, help="Cycle through URLs with this interval (seconds)")
    
    args = parser.parse_args()
    
    client = get_client(args.password)
    
    if args.cycle:
        if len(args.urls) < 2:
            print("Error: Provide at least two URLs to cycle.")
        else:
            cycle_urls(client, args.source, args.urls, args.cycle)
    else:
        # Just set the first URL
        set_source_url(client, args.source, args.urls[0])
```

## Usage

```bash
# Set a single URL
python obs_browser.py "Browser" https://google.com

# Cycle through multiple dashboards every 10 seconds
python obs_browser.py "Browser" https://dashboard1.com https://dashboard2.com --cycle 10
```

[[programming/python/python]]