Skip to main content

Simple MJPEG streamer for Python

Project description

mjpeg-streamer

Overview

The mjpeg-streamer package provides a simple, flexible and efficient way to stream MJPEG video from OpenCV-compatible sources over HTTP. It provides a flexible interface that allows users to specify the video source and configure various parameters such as image size, quality, and FPS.

Installation

You can install the package via pip:

pip install mjpeg-streamer

Usage

Here's a simple example that shows how to use the MJPEG Server package to stream video from a webcam:

import cv2
from mjpeg_streamer import MjpegServer, Stream

cap = cv2.VideoCapture(0)

stream = Stream("my_camera", size=(640, 480), quality=50, fps=30)

server = MjpegServer("localhost", 8080)
server.add_stream(stream)
server.start()

while True:
    _, frame = cap.read()
    cv2.imshow(stream.name, frame)
    if cv2.waitKey(1) == ord("q"):
        break

    stream.set_frame(frame)

server.stop()
cap.release()
cv2.destroyAllWindows()

This example starts the MJPEG server on localhost:8080 and streams video from the webcam with the index of 0. The video is resized to 640x480 pixels, compressed with JPEG quality of 50, and streamed at 30 FPS.

To view the video stream, you can open a web browser and navigate to http://localhost:8080/my_camera.

Don't forget to check out the tests directory for more examples.

Class Reference

Stream

A class that represents a single video stream. A stream consists of a sequence of frames that can be set and retrieved using the set_frame and _get_frame methods.

Constructor:

Stream(name: str, size: Optional[Tuple[int, int]] = None, quality: int = 50, fps: int = 24)

Creates a new Stream instance with the given unique name, image size, JPEG quality (1-100), and FPS.

Methods:

  • set_frame

    set_frame(frame: np.ndarray)
    

    Sets the current frame to the given Numpy array (OpenCV frame).


  • get_bandwidth

    get_bandwidth()
    

    Returns the bandwidth of the stream in bytes per second.


  • _get_frame (private)

    _get_frame()
    

    Returns the current frame as a Numpy array.

MjpegServer

A class that represents an MJPEG server. The server can serve multiple video streams, each identified by a unique name.

Constructor:

MjpegServer(host: str = "localhost", port: int = 8080)

Creates a new MjpegServer instance with the given host and port.

Methods:

  • add_stream

    add_stream(stream: Stream)
    

    Adds a new video stream to the server.


  • start

    start()
    

    Starts the server in a separate thread.


  • stop

    stop()
    

    Stops the server.

Contributing

Contributions are welcome! Please open an issue or submit a pull request.

License

This project is licensed under the AGPL-3.0 License - see the LICENSE file for details.

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

mjpeg-streamer-2023.10.14.tar.gz (16.6 kB view details)

Uploaded Source

File details

Details for the file mjpeg-streamer-2023.10.14.tar.gz.

File metadata

  • Download URL: mjpeg-streamer-2023.10.14.tar.gz
  • Upload date:
  • Size: 16.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.5

File hashes

Hashes for mjpeg-streamer-2023.10.14.tar.gz
Algorithm Hash digest
SHA256 9b099bcce5594d4ae9b52797e280c3d4753e09df7db3f25e4cf5dfbdc1c03763
MD5 f19598265461c237a000abc7c0b53da7
BLAKE2b-256 1bd6743146a984366a97c0bc43e4b87eee6493f78b0fe69916e89b7e7cc5c014

See more details on using hashes here.

Supported by

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