Skip to main content

rtsp-stream

Lightweight, thread-based RTSP video stream reader with automatic reconnection — drop the VideoCapture boilerplate and let the library handle feed drops for you.

Install

pip install rtsp-stream

Note: opencv-python is listed as a dependency. If your project already uses opencv-contrib-python or a headless variant, install with --no-deps and manage OpenCV separately.

Quick start

from rtsp_stream import VideoStream

stream = VideoStream("rtsp://camera-ip/stream")
stream.start()

while True:
    frame = stream.read()   # returns None until first frame arrives
    if frame is not None:
        # do something with the numpy array
        pass

stream.stop()

Context manager

with VideoStream("rtsp://camera-ip/stream").start() as stream:
    frame = stream.read()

Configuration

Parameter Type Default Description
rtsp_url str — Any URL accepted by cv2.VideoCapture
reconnect_delay float 1.0 Seconds between reconnection attempts
max_reconnect_attempts int | None None Max consecutive attempts before giving up (None = forever)
reconnect_backoff float 1.0 Multiply delay by this after each failure (e.g. 2.0 = exponential back-off)
max_reconnect_delay float 30.0 Upper bound on back-off delay
on_connect Callable None Called every time a connection succeeds
on_disconnect Callable None Called every time the feed drops
on_reconnect_failure Callable None Called when max_reconnect_attempts is exhausted
cap_backend int cv2.CAP_ANY OpenCV backend flag
cap_buffer_size int | None None Sets CAP_PROP_BUFFERSIZE right after opening (reduces RTSP latency)

Exponential back-off example

stream = VideoStream(
    "rtsp://camera-ip/stream",
    reconnect_delay=1.0,
    reconnect_backoff=2.0,       # 1s → 2s → 4s → 8s …
    max_reconnect_delay=30.0,    # capped at 30s
    max_reconnect_attempts=10,
)
stream.start()

Callbacks

def on_connect():
    print("Camera back online")

def on_disconnect():
    print("Feed dropped — waiting to reconnect")

def on_failure():
    print("Gave up reconnecting")
    # trigger an alert, restart a service, etc.

stream = VideoStream(
    "rtsp://camera-ip/stream",
    on_connect=on_connect,
    on_disconnect=on_disconnect,
    on_reconnect_failure=on_failure,
)
stream.start()

Properties

stream.fps            # float
stream.frame_width    # int
stream.frame_height   # int
stream.is_connected   # bool
stream.reconnect_count  # int — total reconnect attempts so far

Logging

The library uses Python's standard logging module under the rtsp_stream logger. To see reconnect events:

import logging
logging.basicConfig(level=logging.INFO)

Running tests

python3 -m venv .venv
source .venv/bin/activate
pip install -e ".[dev]"
pytest -q

License

MIT

Metadata

Release files for rtsp-stream 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for rtsp-stream 0.1.0
File Size Uploaded
rtsp_stream-0.1.0.tar.gz 6.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for rtsp-stream 0.1.0
File Interpreter ABI Platform
rtsp_stream-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 12.2 kB

Release files / rtsp_stream-0.1.0.tar.gz

Download URL rtsp_stream-0.1.0.tar.gz
Size 6.7 kB
Tags Source
SHA-256 checksum
How to use checksums
0ebed01d14ed8f45e29664f0d8f0c062836bd2d3829aea2d62472474dac59f67
BLAKE2b-256 checksum
How to use checksums
bbb68749cc4ab969bbfd89aeda4fc641755b484e412fb3a2e4c75d05679b2ba5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.7

Release files / rtsp_stream-0.1.0-py3-none-any.whl

Download URL rtsp_stream-0.1.0-py3-none-any.whl
Size 5.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
50ca256e7193929c0a53a52dc0bcf1b69ebe100e9ff0da0170ef08f1b8a88744
BLAKE2b-256 checksum
How to use checksums
d48c2f077b95dfbeea5548ae4e6989ff252ad1df7bd8624f9c89451c0498ee40
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.7

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page