How to add new video or audio segments in an HLS playlist?

I am trying to create a very long stream with short videos added at a particular time interval. I know it is possible with the HLS format, what kind of changes are needed in the playlist to add new video segments?

1 Like

HLS uses the EXT-X-ENDLIST tag to mark the end of the playlist. So after receiving the EXT-X-ENDLIST tag player stops playing the stream, streams without EXT-X-ENDLIST tag are considered to be live streams.

To add new segments to playlist, you need to remove EXT-X-ENDLIST tag first, and add #EXT-X-DISCONTINUITY tag for discontinuing current playback and adding segments for new video streams. In the end, you can append the same #EXT-X-DISCONTINUITY tag to mark your playlist complete.

Here is the detailed tutorial to follow:

  1. Typical HLS Playlist Format
    Typically, an HLS playlist is divided into two categories:
  • Master Playlist (master.m3u8): This file includes details on the various stream variations or quality levels.

  • Media Playlist (stream.m3u8): A collection of video segments together with their corresponding lengths can be found in this file.

  1. How to Add New Video Clips
    Arrange the Video Clips and make sure the brief videos you have are encoded into HLS video segments (.ts files) that are compatible. Every video section must have a fixed length.

  2. Make changes to the stream.m3u8 media playlist
    Use any text editor of your choice to open the stream.m3u8 file that is already there and add the newly produced video parts to the playlist’s conclusion. Try to adjust each segment’s duration correspondingly.

A sample entry from a media playlist:

#EXTINF:10.000,
segment_001.ts
#EXTINF:10.000,
segment_002.ts
#EXTINF:10.000,
segment_003.ts

  1. New Master Playlist Creation (master.m3u8):

Update the Master Playlist with the new Media Playlist if you have numerous variations or quality. A distinct Media Playlist is linked to each variation in the Master Playlist.

#EXT-X-STREAM-INF:BANDWIDTH=800000,RESOLUTION=640x360
low/playlist.m3u8
#EXT-X-STREAM-INF:BANDWIDTH=1400000,RESOLUTION=842x480
mid/playlist.m3u8
#EXT-X-STREAM-INF:BANDWIDTH=2800000,RESOLUTION=1280x720
high/playlist.m3u8

Your HLS stream should be able to play continuously after making these adjustments, with new video segments inserted at the proper intervals. To guarantee flawless playback, make sure you try the updated playlists.

Got it! Thanks. But why does FFMpeg HLS video transcode and generate partial playlists?
Like in my case, whenever I try to transfer the basic mp4 video into the HLS video via FFMpeg using the following command:
ffmpeg -i SampleVideo_1280x720_10mb.mp4 -codec:v libx264 -codec:a aac -strict experimental -start_number 1 out.m3u8
No doubt it manages perfectly and generates the .ts segment files, It does manage to generate all of the .ts segment files, but the end outcome is any previous segments are omitted from the m3u8 playlist file, which only lists the final four segment files!
Any suggestions on that?

Basically, The playlist defaults to 5 entries with a segment duration of 2 seconds, according the ffmpeg documentation. This likely explains why the playlists only have a few items, as you can see. Try making the playlist’s length (-hls_list_size) equal to 0, which will include every segment. Moreover, Apple advises 10 seconds for section length. The -hls time option allows you to specify the segment’s duration.

The hls_list_size flag worked wonders to fix that. I noticed that in the documentation but chose to ignore it because I believed they would simply apply the (in my opinion) logical default that would contain all of the files. Thanks!

Or maybe this helps you to understand the basic structure of how the command looks like and I hope it may resolve your problem so try:
ffmpeg -i SampleVideo_1280x720_10mb.mp4 -c:v libx264 -c:a aac -strict -2 -start_number 1 -hls_list_size 0 out.m3u8