> ## Documentation Index
> Fetch the complete documentation index at: https://docs.videodb.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Multi-Camera Basketball Analytics

> Real-time 3-camera basketball monitoring with synchronized multi-angle highlight detection

<a href="https://colab.research.google.com/github/video-db/videodb-cookbook/blob/main/real_time_streaming/multicam/Multicam_Basketball_Analysis.ipynb" target="_blank">
  <img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab" noZoom />
</a>

## The Challenge

Modern sports analysis faces growing challenges in providing real-time insights. From fast-paced plays to subtle player movements, monitoring every aspect of the game is tough.

Traditional broadcasters use multiple camera angles but lack AI intelligence to automatically detect key moments and generate synchronized multi-angle highlights in real-time.

What if AI could monitor all feeds, detect key plays instantly, and generate highlights automatically?

## What You'll Build

VideoDB RTStream brings AI-powered intelligence to multi-camera sports systems. In this guide, you'll build a system that:

* Connects 3 synchronized camera feeds (main court + two baskets)
* Detects key basketball events in real-time
* Sends alerts for every highlight across all angles
* Generates synchronized multi-angle video clips for playback

## Multi-Camera Setup

```
🏀 Basketball Arena - 3 Camera System

├── CAM 1: Main Court Field (Wide Angle)
│   └── rtsp://samples.rts.videodb.io:8554/bb-cam1
│
├── CAM 2: North Basket Area Field (Close Up)
│   └── rtsp://samples.rts.videodb.io:8554/bb-cam2
│
└── CAM 3: South Basket Area Field (Close Up)
    └── rtsp://samples.rts.videodb.io:8554/bb-cam3
```

## Setup

### Install Dependencies

```bash theme={null}
pip install videodb
```

### Connect to VideoDB

```python theme={null}
import videodb

api_key = "your_api_key"
conn = videodb.connect(api_key=api_key)
coll = conn.get_collection()
```

## Implementation

### Step 1: Connect All Three Camera Feeds

```python theme={null}
# Camera URLs
cameras = {
    "main_court": "rtsp://samples.rts.videodb.io:8554/bb-cam1",
    "north_basket": "rtsp://samples.rts.videodb.io:8554/bb-cam2",
    "south_basket": "rtsp://samples.rts.videodb.io:8554/bb-cam3"
}

# Connect all streams
rtstreams = {}
for cam_name, rtsp_url in cameras.items():
    rtstreams[cam_name] = coll.connect_rtstream(
        name=f"Basketball {cam_name.replace('_', ' ').title()}",
        url=rtsp_url,
        store=True,
    )
```

### Step 2: Create Shared Indexes on All Cameras

Define one analysis prompt and create indexes on all streams:

```python theme={null}
analysis_prompt = """Analyze this basketball game footage and describe:
1. Slam dunks, three-point shots, and scoring plays
2. Fast breaks and quick transition plays
3. Fouls, physical contact, or player collisions
Be specific about player positions and court location."""

scene_understandings = {}
for cam_name, rtstream in rtstreams.items():
    understanding = rtstream.understand(
        segmentation={"type": "time", "window": "10s"},
        analyzers=[{
            "type": "vlm",
            "name": "scene",
            "sampling": {"frame_count": 1},
            "config": {"prompt": analysis_prompt},
        }],
        store=True,
    )
    scene_understandings[cam_name] = {
        "understanding": understanding,
        "output": understanding.outputs.get("scene"),
    }

scene_indexes = {}
for cam_name, rtstream in rtstreams.items():
    scene = scene_understandings[cam_name]
    index = rtstream.index(
        source=scene["output"],
        name=f"Basketball_{cam_name.upper()}",
        use_for=["semantic"],
    )
    scene_indexes[cam_name] = {
        "understanding": scene["understanding"],
        "output": scene["output"],
        "index": index,
        "index_id": index.id,
    }
```

### Step 3: Define Four Shared Events

Create four events that apply across all camera angles:

```python theme={null}
# Slam Dunk
slam_dunk_event_id = conn.create_event(
    event_prompt="Detect a slam dunk: a player forcefully puts the ball through the hoop.",
    label="slam_dunk",
)

# Three-Pointer
three_pointer_event_id = conn.create_event(
    event_prompt="Detect a successful or attempted three-point shot from beyond the arc.",
    label="three_pointer",
)

# Foul
foul_event_id = conn.create_event(
    event_prompt="Detect fouls, aggressive behavior, or rule violations.",
    label="foul",
)

# Fast Break
fast_break_event_id = conn.create_event(
    event_prompt="Detect a fast break or quick transition toward the basket.",
    label="fast_break",
)
```

### Step 4: Attach Alerts to All Cameras

```python theme={null}
webhook_url = "https://your-webhook-url.com"

# Create alerts for all events on all cameras
for cam_name, scene_index in scene_indexes.items():
    scene_index["index"].create_alert(slam_dunk_event_id, callback_url=webhook_url)
    scene_index["index"].create_alert(three_pointer_event_id, callback_url=webhook_url)
    scene_index["index"].create_alert(foul_event_id, callback_url=webhook_url)
    scene_index["index"].create_alert(fast_break_event_id, callback_url=webhook_url)
```

Now you have 12 total alerts (4 events × 3 cameras).

## Alert Example

An illustrative alert envelope for a detected play can look like this; inspect the live payload before depending on optional fields:

```json theme={null}
{
  "event_id": "event-slam-dunk",
  "label": "slam_dunk",
  "confidence": 0.95,
  "explanation": "The ball is directly above the rim and net, descending through the hoop, indicating a basket is being scored.",
  "timestamp": "2025-09-10T11:21:16.614553+00:00",
  "start_time": "2025-09-10T16:50:45.698108+05:30",
  "end_time": "2025-09-10T16:51:00.698108+05:30",
  "stream_url": "https://rt.stream.videodb.io/manifests/rts-019711db-1086-7750-ba79-8f47a4fed603/1757503245000000-1757503261000000.m3u8"
}
```

The same event can trigger on all 3 cameras. Multi-angle replay assumes the camera clocks share a time basis; align clocks before treating same-interval clips as synchronized evidence.

## Synchronized Playback

Generate synchronized clips from all cameras for the same moment:

```python theme={null}
# When you receive a play alert with timestamp, extract it
alert_timestamp = "2025-09-10T16:50:45.698108+05:30"
import datetime
epoch_time = int(datetime.datetime.fromisoformat(alert_timestamp.replace("Z", "+00:00")).timestamp())

# Generate streams from all cameras for same time window (±5 seconds)
time_window = 5
synchronized_streams = {}

for cam_name, rtstream in rtstreams.items():
    rtstream.generate_stream(
        start=epoch_time - time_window,
        end=epoch_time + time_window
    )
    synchronized_streams[cam_name] = rtstream.stream_url
```

Now you have three clips requested for the same time window from different angles.

## Advanced: Webhook Integration with ngrok

For real-time event handling, set up ngrok tunneling:

```bash theme={null}
# Terminal: Start ngrok tunnel
ngrok http 5000
# Get public URL: https://xxxx-xxxx-xxxx.ngrok.io
```

Then use this public webhook URL when creating alerts for instant event notifications.

## The Result

This demo shows how AI turns raw game feeds into actionable sports intelligence, enabling:

* **Real-time game analysis** across multiple angles
* **Instant highlight generation** with synchronized playback
* **Multi-angle replay** for coaches and analysts
* **Automated event detection** across multiple viewpoints
* **Forensic evidence** for rule reviews and disputes

<Card icon="notebook" href="https://colab.research.google.com/github/video-db/videodb-cookbook/blob/main/real_time_streaming/multicam/Multicam_Basketball_Analysis.ipynb" title="Explore the Full Notebook">
  Open the complete implementation with webhook setup, ngrok tunneling, and data processing.
</Card>

## Related Tutorials

<CardGroup cols={2}>
  <Card icon="trophy" href="/examples-and-tutorials/live-intelligence/cricket-match-monitoring" title="Cricket Highlights Detection">
    Auto-detect sports highlights in real-time
  </Card>

  <Card icon="cctv" href="/examples-and-tutorials/live-intelligence/multicam-public-surveillance" title="Multi-Camera Public Surveillance">
    Enterprise-scale multi-camera monitoring system
  </Card>
</CardGroup>


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.