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?

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, add #EXT-X-DISCONTINUITY tag for discontinuing current playback and adding segments for new videos stream. In the end, you can append the same #EXT-X-DISCONTINUITY tag to mark your playlist complete.

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