How do HLS-live-stream incoming batches of individual frames, "appending" to an m3u8 playlist in real-time, with ffmpeg?

Creating a real-time HLS (HTTP Live Streaming) live stream by continuously appending individual frames to an m3u8 playlist using FFmpeg can be a complex process. Here’s a detailed guide on how to achieve this:

  1. Install FFmpeg

First you need to download FFmpeg on your system use the following the below guides:

  1. Get ready for your video source
    Be prepared with a video source that offers separate frames. This can be either a video source or a series of picture files (such as PNG or JPEG).

  2. Create the Initial m3u8 Playlist

Create a first m3u8 playlist that contains a link to your first video segment.

For example:

#EXTM3U
#EXT-X-TARGETDURATION:10
#EXT-X-VERSION:3
#EXT-X-MEDIA-SEQUENCE:0
#EXTINF:10.000,
segment_0.ts

Save this as playlist.m3u8.

  1. Start the FFmpeg real-time segmentation process

Run FFmpeg to split the video stream into manageable chunks and update the playlist continually. Change input.mp4 to the name of your video source.

ffmpeg -i input.mp4 -c:v h264 -hls_time 10 -hls_list_size 5 -hls_wrap 10 -start_number 0 playlist.m3u8
  • -c:v h264: Set the video codec to H.264. Adjust as needed.
  • -hls_time 10: Define the segment duration (in seconds).
  • -hls_list_size 5: Limit the number of segments in the playlist (adjust as needed).
  • -hls_wrap 10: Specify the maximum number of segment files to retain before overwriting (should be larger than -hls_list_size).
  • -start_number 0: Set the initial segment number.
  1. Keep Adding New Frames

As additional frames become available, manually modify the playlist playlist.m3u8 file with new entries for the segments, then use FFmpeg to convert them into video segments (e.g., segment_1.ts, segment_2.ts).

  1. Stream the HLS protocol

Deliver the HLS stream to viewers via a web server or streaming software. Make that the segment segment.m3u8 and playlist playlist.m3u8 files can be accessed through HTTP.

The m3u8 playlist will be updated often when new segments are added, simulating a real-time HLS live stream.

Do let me know if you need more help regarding this.

2 Likes