Coder Social home page Coder Social logo

python-hls-stream's Introduction

python-hls-stream

A minimal proof-of-concept to demonstrate real-time (seekable) HLS streaming from Python with dynamic timeline marker support.


Front-end of HLS streaming demo. Click to view video.

The video linked above shows a real-time HTTP Live Streaming (HLS) generated in Python using ffmpeg. Ocassionally, an event (mint squares) is emitted that leads to markers being added to the HTML5 client table and the timeline. Depending on the HLS configuration you can leave the live edge and seek backwards in time.

Architecture

The system combines multiple processes to generated the desired result

  • hlsstream.sync (Python) key/value cache for inter-process communication based on multiprocessing.SyncManager.
  • hlsstream.stream (Python) generates the checkerboard images and encodes them as HLS stream using ffmpeg. Additionally, events (mint squares) are randomly emitted and stored in the cache as time/text dict.
  • hlsstream.api (Python) a web-API that exposes the HLS stream plus a marker query API using fastAPI. Additionally it serves index.html that contains an embedded video-player along with business logic for handling markers.
  • frontend (HTML5) client frontend using video-js.

Limitations

Keep in mind, this is a proof-of-concept and thus expect glitches and other issues.

  • hlsstream.sync For production you should switch this for redis or memcached.
  • hlsstream.stream The event detections (mint-square) is not based on computer-vision but based on checkerboard generator knowledge. In reality, you will have separate detection services that read images and emit marker events.
  • hlsstream.stream HLS stream encoder expects rawvideo (images) input. Except for setting a target FPS, I did not find a way to provide a PTS per frame. Hence, ffmpeg assumes 1/FPS between two frames, even if reality the FPS varies. To resolve, you should keep track of generator timestamps vs target timestamps and if an event needs to be generated, convert from generator timestamp to target timestamp. The demo currently employs a busy-waiting strategy to keep timestamps closest to target fps. This leads to high CPU usage.
  • hlsstream.api currently configures CORS very carelessly. In production you will need to restrict it accordingly.
  • frontend attempts to determine the endpoints of the timeline of the video-js player. The current method seems to work when live or when not tracking the live stream, but might fail when hlsstream.stream is terminated. A refresh should fix things.

Clocks

The system involves several clocks that need to be synchronized.

  • GEN: Generator clock in [sec]. This clock is usually built into capturing devices such as cameras.

  • HLS: HLS stream clock in [sec]. Frames from GEN are encoded for HLS. When HLS assumes a fixed encoding frame-rate, you need to keep track of GEN and HLS timestamps. While your system internal process will use GEN timestamps, the API should report HLS timestamps.

  • CLIENT: HMTL video-js clock in [sec]. When the client connects to the HLS stream, the client clock is reset. To seek the video correctly, we need to convert markers from HLS <-> CLIENT. If we assume this transformation takes only an offset and if we assume that the segment duration is constant, we can compute the offset as

     offset = HLS-sequence * HLS-duration
    

Usage

Python 3.9 is required. This should work on linux/windows.

$ pip -m venv --upgrade-deps .venv
$ source .venv/bin/activate
(.venv) $ pip install pip-tools
(.venv) $ pip-sync requirements.txt dev-requirements.txt
(.venv) $ python -m hlsstream

Point your browser to http://127.0.0.1:5000. Same commands, except for how to activate the venv, apply to Windows.

python-hls-stream's People

Contributors

cheind avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.