Skip to content

Instantly share code, notes, and snippets.

@m1k1o
Last active May 19, 2021 18:48
Show Gist options
  • Save m1k1o/fab8ae2b16e4fededa14363c76c5b3ef to your computer and use it in GitHub Desktop.
Save m1k1o/fab8ae2b16e4fededa14363c76c5b3ef to your computer and use it in GitHub Desktop.
Simple VOD HLS Loop in PHP
<?php
header("Content-Type: application/x-mpegURL");
header("Content-Disposition: attachment; filename=index.m3u8");
$target_duration = 10;
$segments_total = 93;
$segments_live = 5;
$segment_path = "%03d.ts";
$start_offset = 0; // starts with 000.ts
$start_time = strtotime("19 May 2021");
$media_sequence = intval((time() - $start_time) / $target_duration);
echo <<<M3U8
#EXTM3U
#EXT-X-VERSION:3
#EXT-X-TARGETDURATION:${target_duration}
#EXT-X-MEDIA-SEQUENCE:${media_sequence}
M3U8;
for ($i=0;$i<$segments_live;$i++) {
$segment_id = ($media_sequence + $i) % $segments_total;
if ($segment_id == 0) echo "\n", "#EXT-X-DISCONTINUITY";
echo "\n", "#EXTINF:${target_duration},";
echo "\n", sprintf($segment_path, $segment_id + $start_offset);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment