Skip to main content

RTSP/RTMP client and server for Python.

Project description

RTSP

CI codecov PyPI version Downloads Downloads License: MIT

        /((((((\\\\
=======((((((((((\\\\\
     ((           \\\\\\\
     ( (*    _/      \\\\\\\
       \    /  \      \\\\\\________________
        |  |   |      </    __             ((\\\\
        o_|   /        ____/ / _______       \ \\\\    \\\\\\\
             |  ._    / __/ __(_-</ _ \       \ \\\\\\\\\\\\\\\\
             | /     /_/  \__/___/ .__/       /    \\\\\\\     \\
     .______/\/     /           /_/           /         \\\
    / __.____/    _/         ________(       /\
   / / / ________/`---------'         \     /  \_
  / /  \ \                             \   \ \_  \
 ( <    \ \                             >  /    \ \
  \/      \\_                          / /       > )
           \_|                        / /       / /
                                    _//       _//
                                   /_|       /_|

A Pythonic RTSP client and microserver for video and computer vision work, from rapid prototyping to moderate-weight production. Pull frames from any RTSP stream or local capture device as Pillow images, preview streams in a window, or serve a frame buffer over RTSP with a single context manager. For multi-client distribution, pair it with a MediaMTX relay. For performance-intensive pipelines requiring high-throughput encoding or minimal latency, use OpenCV, GStreamer, or FFmpeg directly.

Features

  • read most-recent RTSP frame as Pillow Image on demand
  • preview stream in a tkinter window. 'q' or ESC to quit.
  • URI shortcuts for rapid prototyping
    • integers (or numeric strings) open a local capture device, e.g. rtsp.Client(0)
    • bare host strings default to rtsp://, e.g. rtsp.Client('192.168.1.1/stream')
    • Raspberry Pi camera: enable the V4L2 driver (dtoverlay=imx219 or similar in /boot/config.txt) and use rtsp.Client(0)
  • lightweight RTSP server
  • rtmp:// and rtmps:// URIs work with Client and Source using the same API as RTSP
  • enumerate local capture devices with rtsp.list_devices()

Examples

Client Use

Use RTSP access credentials in your connection string e.g.

RTSP_URL = f"rtsp://{USERNAME}:{PASSWORD}@192.168.1.221:554/11"

One-off Retrieval

import rtsp
client = rtsp.Client(rtsp_server_uri = 'rtsp://...', verbose=True)
client.read().show()
client.close()

Stream Preview

import rtsp
with rtsp.Client(0) as client: # previews USB webcam 0
    client.preview()

Continuous Retrieval

import rtsp

with rtsp.Client(rtsp_server_uri = 'rtsp://...') as client:
    _image = client.read()

    while True:
        process_image(_image)
        _image = client.read(raw=True)

Resize Retrieval Image

import rtsp

RTSP_URL = "rtsp://..."
client = rtsp.Client(rtsp_server_uri = RTSP_URL)

width = 640
height = 480

client.read().resize([width, height]).show()
client.close()

Rotate Retrieval Image

import rtsp

RTSP_URL = "rtsp://..."
client = rtsp.Client(rtsp_server_uri = RTSP_URL)

client.read().resize([client.read().size[0], client.read().size[0]]).rotate(90).resize([client.read().size[1], client.read().size[0]]).show()
client.close()

Save Retrieval Image (With the TimeStamp Format and Set Number of Save Image)

import rtsp
import datetime

RTSP_URL = "rtsp://..."
IMAGE_COUNT = 10

client = rtsp.Client(rtsp_server_uri = RTSP_URL)
while client.isOpened() and IMAGE_COUNT > 0:
    client.read().save("./"+ str(datetime.datetime.now()) +".jpg")
    IMAGE_COUNT = IMAGE_COUNT - 1
client.close()

Source Use

Serve frames locally and preview them. source.client_uri gives the address to connect to, and verbose=True logs the URI and fps on startup:

import rtsp

frames = ['frame1.jpg', 'frame2.jpg', 'frame3.jpg']

with rtsp.Source('rtsp://0.0.0.0:8554/live', fps=1,
                 frame_buffer=frames, verbose=True) as source:
    with rtsp.Client(source.client_uri) as client:
        client.preview()   # blocks; press q or ESC to quit

Serve without a viewer. serve_forever() blocks and loops the buffer indefinitely until Ctrl-C:

import rtsp

frames = ['frame1.jpg', 'frame2.jpg', 'frame3.jpg']

with rtsp.Source('rtsp://0.0.0.0:8554/live', frame_buffer=frames) as source:
    source.serve_forever()   # blocks; Ctrl-C to stop

Multi-client stream via MediaMTX relay. Also, frames can be added incrementally with put():

# Terminal: ./mediamtx          (listens on :8554 by default)

import rtsp

# serve=False pushes to the running MediaMTX relay.
# Any number of Client() instances can then read from the same URI.
with rtsp.Source('rtsp://localhost:8554/live', serve=False) as source:
    for frame in incoming_frames():
        source.put(frame)

# Elsewhere, any number of concurrent readers:
with rtsp.Client('rtsp://localhost:8554/live') as client:
    client.preview()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rtsp-2.0.2.tar.gz (57.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rtsp-2.0.2-py3-none-any.whl (25.2 kB view details)

Uploaded Python 3

File details

Details for the file rtsp-2.0.2.tar.gz.

File metadata

  • Download URL: rtsp-2.0.2.tar.gz
  • Upload date:
  • Size: 57.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for rtsp-2.0.2.tar.gz
Algorithm Hash digest
SHA256 37530af9a316fa82756af26793a818df8960c2800688b8e459aeb6e0f8e0daab
MD5 873304fb29e3c2b4b038bedb1e900aa2
BLAKE2b-256 c84b9dfbf32bb387833d8d09288ce9e9ccdb06376595786f7b5ddf3434eb9728

See more details on using hashes here.

Provenance

The following attestation bundles were made for rtsp-2.0.2.tar.gz:

Publisher: publish.yml on dactylroot/rtsp

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rtsp-2.0.2-py3-none-any.whl.

File metadata

  • Download URL: rtsp-2.0.2-py3-none-any.whl
  • Upload date:
  • Size: 25.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for rtsp-2.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 de2d2273408a4b7f4d2b057416e1c8160260506d09cbc829449a9c4b441734dc
MD5 a54bd3d4d1b5c38c820c2b347f12cc66
BLAKE2b-256 bd3126b7a4bb07eb5243a018883bdf4d181d7e9b6ec015214d956a6f40ad0031

See more details on using hashes here.

Provenance

The following attestation bundles were made for rtsp-2.0.2-py3-none-any.whl:

Publisher: publish.yml on dactylroot/rtsp

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page