Search code examples
http-live-streamingm3u8

How to include same segment into m3u8 playlist multiple times?


I have several .ts files, generated by ffmpeg from live stream. All of them have the same length (well, approximately). Ideally, they are being generated constantly, with names representing the datetime when they have been recorded (for example, "2019-04-11_10-51-40.ts"). But it may happen so, that for any technical reasons recording was being stopped for a while and files have not been generated.

Now, I have a task to create a playlist of these files for a certain datetime range - and if there are no files for part of this range, I need to show just a black screen. I have a black video for this purpose with the same length as other files. So, I'm trying to manually create an .m3u8 file and insert this black video in all gaps between normal videos that I have. For example:

#EXTM3U
#EXT-X-VERSION:3
#EXT-X-TARGETDURATION:34
#EXT-X-PLAYLIST-TYPE:VOD
#EXTINF:30.07,
http://example.com/black_video.ts
#EXTINF:30.07,
http://example.com/black_video.ts
#EXTINF:33.33,
http://example.com/2019-04-11_10-51-40.ts
#EXTINF:33.33,
http://example.com/2019-04-11_10-52-15.ts
#EXTINF:25.00,
http://example.com/2019-04-11_10-52-48.ts
#EXT-X-ENDLIST

The problem is that when I'm trying to play this playlist, it does not play correctly: depending on player either only one black part is being played no matter how much of them there are in a sequence (VLC), or player is getting stuck after first black video ("Play HLS M3u8" extension for Chrome or player on our own service, based on hls.js).

I also tried to use #EXT-X-DISCONTINUITY; in this case all videos are being played, but progress bar drops to the very beginning after each #EXT-X-DISCONTINUITY appearance, which is also an undesired behaviour. Example:

#EXTM3U
#EXT-X-VERSION:3
#EXT-X-TARGETDURATION:34
#EXT-X-PLAYLIST-TYPE:VOD
#EXTINF:30.07,
http://example.com/black_video.ts
#EXT-X-DISCONTINUITY
#EXTINF:30.07,
http://example.com/black_video.ts
#EXTINF:33.33,
http://example.com/2019-04-11_10-51-40.ts
#EXTINF:33.33,
http://example.com/2019-04-11_10-52-15.ts
#EXTINF:25.00,
http://example.com/2019-04-11_10-52-48.ts
#EXT-X-ENDLIST

As for black videos themselves, I tried several options: each link was to the same file; each link was to a different symlink to the same file; each link was to a separately generated black video - none of them worked.

As of now the only way I found is to collect all normal videos for the time range, fill the gaps with black video files and then combine them into one file with ffmpeg -f concat and split the concatenated file back to parts. But this approach works way longer than manual m3u8 file creation, so I would like to avoid it.

So, what could be the reason of manually formed playlist not working correctly and how could I fix that? May it be the problem with playlist itself, or playlist is technically fine but players I used possibly don't fully implement the specification?

TLDR: I am creating m3u8 playlist manually and I want to insert the same video multiple times into this playlist. Unfortunately, such playlist is not being played correctly: depending on player either this inserted video is being played only once no matter how many times has it been included, or playing is getting stuck after the first inserted video part. May it be that there is something wrong with playlist and how can this be fixed?


Solution

  • Add a #EXT-X-DISCONTINUITY tag before and after black_video.ts file and change the #EXT-X-VERSION tag to 5. That should fix the issue. This solution worked for me on VLC player.