Lightweight, thread-based RTSP video stream reader with automatic reconnection.
Project description
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-pythonis listed as a dependency. If your project already usesopencv-contrib-pythonor a headless variant, install with--no-depsand 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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file rtsp_stream-0.1.0.tar.gz.
File metadata
- Download URL: rtsp_stream-0.1.0.tar.gz
- Upload date:
- Size: 6.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0ebed01d14ed8f45e29664f0d8f0c062836bd2d3829aea2d62472474dac59f67
|
|
| MD5 |
6320eeeab5cedebf2e235875093c9333
|
|
| BLAKE2b-256 |
bbb68749cc4ab969bbfd89aeda4fc641755b484e412fb3a2e4c75d05679b2ba5
|
File details
Details for the file rtsp_stream-0.1.0-py3-none-any.whl.
File metadata
- Download URL: rtsp_stream-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
50ca256e7193929c0a53a52dc0bcf1b69ebe100e9ff0da0170ef08f1b8a88744
|
|
| MD5 |
0bc805bbf91141c09595186e50a0447b
|
|
| BLAKE2b-256 |
d48c2f077b95dfbeea5548ae4e6989ff252ad1df7bd8624f9c89451c0498ee40
|