python-hls-stream
python-hls-stream copied to clipboard
Minimal HLS streaming demo with dynamic marker support in Python
python-hls-stream
A minimal proof-of-concept to demonstrate real-time (seekable) HLS streaming from Python with dynamic timeline marker support.
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 onmultiprocessing.SyncManager.hlsstream.stream(Python) generates the checkerboard images and encodes them as HLS stream usingffmpeg. 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 usingfastAPI. Additionally it servesindex.htmlthat contains an embedded video-player along with business logic for handling markers.frontend(HTML5) client frontend usingvideo-js.
Limitations
Keep in mind, this is a proof-of-concept and thus expect glitches and other issues.
hlsstream.syncFor production you should switch this forredisormemcached.hlsstream.streamThe 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.streamHLS stream encoder expectsrawvideo(images) input. Except for setting a target FPS, I did not find a way to provide a PTS per frame. Hence,ffmpegassumes1/FPSbetween 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.apicurrently configures CORS very carelessly. In production you will need to restrict it accordingly.frontendattempts to determine the endpoints of the timeline of thevideo-jsplayer. The current method seems to work when live or when not tracking the live stream, but might fail whenhlsstream.streamis 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 fromGENare encoded forHLS. WhenHLSassumes a fixed encoding frame-rate, you need to keep track ofGENandHLStimestamps. While your system internal process will useGENtimestamps, the API should reportHLStimestamps. -
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 fromHLS <-> 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 asoffset = 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.
