All Projects → janakj → py-mjpeg

janakj / py-mjpeg

Licence: MIT license
Python MJPEG streaming utilities

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to py-mjpeg

Jpegrtspcamera
Sample RTSP server streaming MJPEG video from PC camera
Stars: ✭ 25 (-21.87%)
Mutual labels:  mjpeg, video-streaming
Ipcam View
MJPEG video streaming on Android
Stars: ✭ 292 (+812.5%)
Mutual labels:  mjpeg, video-streaming
vscode-extension
Link your code and commits to a video recording and explain your code with video.
Stars: ✭ 67 (+109.38%)
Mutual labels:  video-streaming
ESP32-CAM-MJPEG-Stream-Decoder-and-Control-Library
The library is MJPEG stream decoder based on libcurl and OpenCV, and written in C/C++.
Stars: ✭ 40 (+25%)
Mutual labels:  mjpeg
grumpy-pi-mjpg
Simple HTTP server to provide streams of Raspberry Pi camera using MJPEG codec
Stars: ✭ 17 (-46.87%)
Mutual labels:  mjpeg
Virtual-Room
A virtual room where friends share videos among them in real time directly over the web browser, with synchronized playback and a video chat at the same time.
Stars: ✭ 31 (-3.12%)
Mutual labels:  video-streaming
YetAnotherVideoPlayer
Yet Another Video Player for Andoid
Stars: ✭ 62 (+93.75%)
Mutual labels:  video-streaming
Picam
Elixir library used to capture MJPEG video on a Raspberry Pi using the camera module.
Stars: ✭ 96 (+200%)
Mutual labels:  mjpeg
videostream
Video Streaming site using Laravel and WebTorrent
Stars: ✭ 36 (+12.5%)
Mutual labels:  video-streaming
emiga-stream
Video stream using Express.js , Socket.io
Stars: ✭ 24 (-25%)
Mutual labels:  video-streaming
gilfoyle
Distributed video encoding, hosting and streaming (WIP)
Stars: ✭ 73 (+128.13%)
Mutual labels:  video-streaming
shaka-php
🎞 Shaka PHP is a library that uses Shaka Packager for DASH and HLS packaging and encryption, supporting Common Encryption for Widevine and other DRM Systems.
Stars: ✭ 63 (+96.88%)
Mutual labels:  video-streaming
FPGAmp
720p FPGA Media Player (RISC-V + Motion JPEG + SD + HDMI on an Artix 7)
Stars: ✭ 190 (+493.75%)
Mutual labels:  mjpeg
2nfm
Share your screen and computer's audio via WebRTC!
Stars: ✭ 29 (-9.37%)
Mutual labels:  video-streaming
mini
OpenSource Mini IP camera streamer
Stars: ✭ 64 (+100%)
Mutual labels:  mjpeg
bigscreen-player
Simplified media playback for bigscreen devices
Stars: ✭ 62 (+93.75%)
Mutual labels:  video-streaming
Esp32 Cam Video Recorder
Video Recorder for ESP32-CAM with http server for config and ftp server to download video
Stars: ✭ 169 (+428.13%)
Mutual labels:  mjpeg
emitron-Android
Android version of emitron
Stars: ✭ 53 (+65.63%)
Mutual labels:  video-streaming
FaceNet-IOT
IOT implementation for FaceNet project by David Sandberg https://github.com/davidsandberg/facenet
Stars: ✭ 18 (-43.75%)
Mutual labels:  video-streaming
all-in-one-video-pack.wordpress
A Wordpress Plugin to simplify adding Kaltura to your Blog
Stars: ✭ 19 (-40.62%)
Mutual labels:  video-streaming

MJPEG Streaming Utilities for Python 3.x

This library provides utility functions for working with MJPEG streams. MJPEG is a simple streaming protocol running on top of HTTP that is used by many existing webcams. The library provides a threaded client and a streaming generator for Flask based servers.

The library is only compatible with Python 3.x (tested on Python 3.4 and 3.5). It was tested with mjpg_streamer.

Installation

pip3 install py-mjpeg

Client API

The library provides a simple threaded streaming client in the file mjpeg/client.py. The client is designed to run in a separate background thread to ensure that it can continue reading from the stream while the main thread is blocked. The client automatically reconnects to the server if it gets disconnected.

Here is a simple example:

from mjpeg.client import MJPEGClient

url='http://example.com:8080/?action=stream'

# Create a new client thread
client = MJPEGClient(url)

# Allocate memory buffers for frames
bufs = client.request_buffers(65536, 50)
for b in bufs:
    client.enqueue_buffer(b)
    
# Start the client in a background thread
client.start()

To obtain frame data, the application creates a list of memory buffers via client.request_buffers. Each buffer holds exactly one JPEG frame. The application then requests the buffers to be filled by calling client.enqueue_buffer. Once a buffer is enqueued, the application must no longer touch it.

To received finished frames, the application calls client.dequeue_buffer() repeatedly:

while True:
    buf = client.dequeue_buffer()
    <do some work>
    client.enqueue_buffer(buf)

The call to dequeue_buffer is blocking. Each buffer object provides the following attributes:

  • length: The total number of bytes that can fit into the data portion of the buffer
  • used: The number of bytes occupied by frame data in this buffer
  • timestamp: The timestamp of the first byte within this buffers (obtained via time.time())
  • sequence: Frame's sequence number
  • data: Thea actual frame data

You can use a memory view to obtain frame data from the buffer:

data = memoryview(buf.data)[:buf.used]

When the client runs out of buffers to store frames, it will continue receiving the stream, but any frame data will be discarded. If the connection is disconnected or if the client detects a protocol error, it will try to reconnect the stream automatically. If the client receives a frame that is larger than the destination buffer, the frame will be discarded.

The client can be stopped via its stop() method:

client.stop()

If the client shall be restarted after calling stop() one must create a new instance as threads can only be started once:

# Create a new client thread
client = MJPEGClient(url)

The client provides a method called print_stats which can be used for debugging:

MJPEGClient:
  URL:            : http://example.com:8080/?action=stream
  FPS             : 30
  Buffer overruns : 2
  Reconnects      : 0
  Total frames    : 2984
  Discarded frames: 704
  Buffer queue    : 0

Server API

The file mjpeg/server.py provides a generator for Flask that can be used to generate a MJPEG stream from iterator data.

Here is a simple "echo" example which just sends any frames received from a client back to the client:

from Flask import Flask, Response
from mjpeg.server import MJPEGResponse

def relay():
    while True:
        buf = client.dequeue_buffer()
        yield memoryview(buf.data)[:buf.used]
        client.enqueue_buffer(buf)

@app.route('/')
def stream():
    return MJPEGResponse(relay())

if __name__ == '__main__':
    app = Flask(__name__)
    app.run(host='0.0.0.0', port=8080)
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].