https://github.com/cheind/python-hls-stream

Minimal HLS streaming demo with dynamic marker support in Python

https://github.com/cheind/python-hls-stream

Science Score: 26.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
  • Academic publication links
  • Academic email domains
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (11.1%) to scientific vocabulary

Keywords

fastapi ffmpeg hls hls-l html5 python video-js
Last synced: 5 months ago · JSON representation

Repository

Minimal HLS streaming demo with dynamic marker support in Python

Basic Info
  • Host: GitHub
  • Owner: cheind
  • License: mit
  • Language: Python
  • Default Branch: main
  • Homepage:
  • Size: 3.57 MB
Statistics
  • Stars: 26
  • Watchers: 2
  • Forks: 0
  • Open Issues: 1
  • Releases: 0
Topics
fastapi ffmpeg hls hls-l html5 python video-js
Created almost 4 years ago · Last pushed about 2 years ago
Metadata Files
Readme License

README.md

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. bash $ 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.

Owner

  • Name: Christoph Heindl
  • Login: cheind
  • Kind: user
  • Location: Austrian area

I am a computer scientist working at the interface of perception, robotics and deep learning.

GitHub Events

Total
  • Issues event: 1
  • Watch event: 3
Last Year
  • Issues event: 1
  • Watch event: 3

Issues and Pull Requests

Last synced: 10 months ago

All Time
  • Total issues: 1
  • Total pull requests: 0
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Total issue authors: 1
  • Total pull request authors: 0
  • Average comments per issue: 0.0
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 1
  • Pull requests: 0
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Issue authors: 1
  • Pull request authors: 0
  • Average comments per issue: 0.0
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
  • Siziff (1)
Pull Request Authors
Top Labels
Issue Labels
Pull Request Labels

Dependencies

dev-requirements.in pypi
  • black * development
  • flake8 * development
  • matplotlib * development
  • pytest * development
dev-requirements.txt pypi
  • atomicwrites ==1.4.0 development
  • attrs ==21.4.0 development
  • black ==22.3.0 development
  • click ==8.1.3 development
  • colorama ==0.4.4 development
  • cycler ==0.11.0 development
  • flake8 ==4.0.1 development
  • fonttools ==4.33.3 development
  • iniconfig ==1.1.1 development
  • kiwisolver ==1.4.2 development
  • matplotlib ==3.5.2 development
  • mccabe ==0.6.1 development
  • mypy-extensions ==0.4.3 development
  • numpy ==1.22.3 development
  • packaging ==21.3 development
  • pathspec ==0.9.0 development
  • pillow ==9.1.0 development
  • platformdirs ==2.5.2 development
  • pluggy ==1.0.0 development
  • py ==1.11.0 development
  • pycodestyle ==2.8.0 development
  • pyflakes ==2.4.0 development
  • pyparsing ==3.0.8 development
  • pytest ==7.1.2 development
  • python-dateutil ==2.8.2 development
  • six ==1.16.0 development
  • tomli ==2.0.1 development
  • typing-extensions ==4.2.0 development
requirements.in pypi
  • fastapi *
  • ffmpeg-python *
  • numpy *
  • schedule *
  • uvicorn *
requirements.txt pypi
  • anyio ==3.5.0
  • asgiref ==3.5.1
  • click ==8.1.3
  • colorama ==0.4.4
  • fastapi ==0.76.0
  • ffmpeg-python ==0.2.0
  • future ==0.18.2
  • h11 ==0.13.0
  • idna ==3.3
  • numpy ==1.22.3
  • pydantic ==1.9.0
  • schedule ==1.1.0
  • sniffio ==1.2.0
  • starlette ==0.18.0
  • typing-extensions ==4.2.0
  • uvicorn ==0.17.6