How to loop TS file in m3u8 playlist

Aleksandar picture Aleksandar · Jan 25, 2016 · Viewed 19.2k times · Source

I want to create m3u8 playlist, like this:

#EXTINF:-1,LIVE STREAM
http://example.com/live01.ts

When this file ended I want repeat in loop. After live01.ts ended, need to start again and do this infinity in loop.

Is this possible?

Answer

MithrilTuxedo picture MithrilTuxedo · Jan 27, 2016

It's not possible with a static playlist like that.

First, you'll have to have a #EXT-X-DISCONTINUITY tag before the #EXTINF tag so the player knows the timestamps on the video/audio frames aren't going to continue in order.

Second, and more importantly, you'll need a #EXT-X-MEDIA-SEQUENCE:<number> and/or #EXT-X-DISCONTINUITY-SEQUENCE:<number> at the head of the file that increments at the same rate as the duration of that chunk of video. The player is going to be re-requesting the playlist (until it sees #EXT-X-ENDLIST) and without incrementing those values it will continue to assume that http://example.com/live01.ts is the first chunk in the sequence.

If you're serving the M3U8 file directly and you have a sleep command on your system that supports milliseconds, I'd suggest something like this:

file="/path/to/file.m3u8"
duration="1.337"

make_playlist() {
  echo "#EXTM3U"
  echo "#EXT-X-MEDIA-SEQUENCE:${1}"
  echo "#EXT-X-DISCONTINUITY-SEQUENCE:${1}"
  echo "#EXT-X-DISCONTINUITY"
  echo "#EXTINF:${duration},"
  echo "http://example.com/live01.ts"
  echo
}

for ((x=0;;x++)); do
  make_playlist "${x}" > "${file}"
  sleep "${duration}"
done