HLS stands for HTTP Live Streaming. It is an HTTP-based adaptive bitrate streaming communications protocol developed by Apple. It allows content to be delivered over HTTP-based CDNs using extended M3U playlists. To learn more about HLS, consider visting Wikipedia.
This is an automated example implementation of HLS in NodeJS using ffmpeg.
Express server accepts the video and save it temporarily on the server. Few checks are made like checking the video format and mime-types. Garbage files are truncated.
Server triggers a shell script which handles the video for different renditions by keeping the aspect ratio of the video. The various renditions are:
- 640x360 resolution with 800k bitrate and 96k audio-birate --> 360p
- 842x480 resolution with 1400k bitrate and 128k audio-birate --> 480p
- 1280x720 resolution with 2800k bitrate and 128k audio-birate --> 720p
- 1920x1080 resolution with 5000k bitrate and 192k audio-birate --> 1080p
Though, these are pretty basic renditions. The renditions are static for now and could be dynamic based on initial video quality.
The video is divided into 4 seconds segments intervals which will be loaded for each buffering units and the durations must be calculated on mean size of videos being uploaded because it cannot be too small or too large. Too short segment size will lead to too many segments to manage and hence larger buffer lookups. Too large segment size will fail for the smaller videos as they will always be loaded with single segment. The standard option is to have very small segment size for smaller videos. Whereas, the segment size of 4-6 seconds fits for all video types.
The bitrate ratio defines the maximum accepted birate fluctuations which is kept to 1.07.
Maximum buffer size between bitrate conformance checks is set to 1.5 by default.
Using ffmpeg to trigger shell command to generate HLS VOD streams for the incoming video. Consider visiting ffmpeg webiste for details about the CMD tools and API.
The logs for shell command are handled via spawn
and logged via basic stdout
as of now. For production, we can use netcat to communicate messages/status via TCP/UDP connection. We can utilize sockets
connects on shell to let applications communicate for logs and statuses so that errors/success messages could be tracked.
Following is the link to netcat cheatsheet: netcat cheatsheet
The script will generate the sequence for video with the following formats.
Here, .qt
file the uploaded file and rest of the file are different renditions generated for streaming over HTTP. Now, we do not need original file. We just have to upload the Renditions alon with .m3u8
playlist files.
Now in the last step, We will upload all generated renditions to S3. We don't have to upload original file but we can for the backup purpose. Now, the single file playlist.m3u8
will be sufficient to stream the video on any supporting device that support HLS streaming. The most common example are VLC media Player, WMV player, Android player, HTML5 Web player etc.
- Messaging between shell and backend via
netcat
UDP connections. - Alerts and Upload status management.
- Dynamic Renditions based on original video resolutions.
- Dynamic segment size, bitrateratio and buffer ratio.
- Advanced logging and log tracking.
- Advanced error handling and fault taulrance.
- Handling for production scaling with load balance test.
Reach out ot me at sharma02gaurav@gmail.com
PRs are welcome.