Skip to main content
Open In Colab

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

pip install videodb

Connect to VideoDB

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

# 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,
    )

Step 2: Create Shared Indexes on All Cameras

Define one analysis prompt and create indexes on all streams:
analysis_prompt = """Analyze this basketball game footage and describe:
1. Player positions and movements on the court
2. Ball location and which team has possession
3. Any significant events (baskets scored, fouls, free throws, timeouts)
4. Defensive and offensive plays being executed
5. Crowd reactions or unusual activities"""

# Create scene indexes for all cameras
scene_indexes = {}
for cam_name, rtstream in rtstreams.items():
    scene_indexes[cam_name] = rtstream.index_visuals(
        batch_config={
            "type": "time",
            "value": 15,
            "frame_count": 1,
        },
        prompt=analysis_prompt,
        name=f"Basketball_Analytics_{cam_name.upper()}",
    )

Step 3: Define Shared Events

Create three events that apply across all camera angles:
# Basket Scored
basket_event_id = conn.create_event(
    event_prompt="Detect when a basket is scored - ball through hoop, celebrations, score change.",
    label="basket_scored",
)

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

# Timeout Called
timeout_event_id = conn.create_event(
    event_prompt="Detect when a timeout is called - players huddle or referee signals.",
    label="timeout_called",
)

Step 4: Attach Alerts to All Cameras

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.create_alert(basket_event_id, callback_url=webhook_url)
    scene_index.create_alert(foul_event_id, callback_url=webhook_url)
    scene_index.create_alert(timeout_event_id, callback_url=webhook_url)
Now you have 9 total alerts (3 events × 3 cameras).

Alert Example

When a basket is scored, each camera sends an alert:
{
  "event_id": "event-basket-scored",
  "label": "basket_scored",
  "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 triggers on all 3 cameras, giving you synchronized multi-angle evidence.

Synchronized Playback

Generate synchronized clips from all cameras for the same moment:
# When you receive a basket 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():
    stream_url = rtstream.generate_stream(
        start=epoch_time - time_window,
        end=epoch_time + time_window
    )
    synchronized_streams[cam_name] = stream_url
Now you have 3 synchronized video clips from different angles for the same moment.

Advanced: Webhook Integration with ngrok

For real-time event handling, set up ngrok tunneling:
# 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

Explore the Full Notebook

Open the complete implementation with webhook setup, ngrok tunneling, and data processing.