Skip to content

Instantly share code, notes, and snippets.

@Yogu
Last active July 10, 2023 19:24
Show Gist options
  • Save Yogu/176fbfe5d5304c3899b011a0c9ebe5fd to your computer and use it in GitHub Desktop.
Save Yogu/176fbfe5d5304c3899b011a0c9ebe5fd to your computer and use it in GitHub Desktop.
// prints how long in the past the current "end" of the stream is
// this is a lower bound on latency that's viewer-independent
// put in the m3u of a stream
// e.g. just open network tab, refresh, then filter for m3u
// don't use the meta one (the first) but the one that's called repeatedly
const playlistUrl = "";
async function test() {
const minNow = new Date().getTime();
const res = await fetch(playlistUrl, {
headers: {
"accept": "application/x-mpegURL, application/vnd.apple.mpegurl, application/json, text/plain"
}
});
const text = await res.text();
const dateLine = text.split('\n').filter(l => l.includes('EXT-X-PROGRAM-DATE-TIME:')).at(-1);
const lengthLine = text.split('\n').filter(l => l.includes('EXTINF:')).at(-1)
const maxNow = new Date().getTime();
const date = new Date(dateLine.substring(dateLine.indexOf(':') + 1));
const pos = lengthLine.indexOf(':');
const length = Number(lengthLine.substring(pos + 1, lengthLine.indexOf(','))) * 1000;
const minEndAge = minNow - date.getTime() - length;
const maxEndAge = maxNow - date.getTime() - length;
const timestamp = new Date().toISOString();
return `${timestamp}: stream end is between ${(minEndAge/1000).toFixed(3)} and ${(maxEndAge/1000).toFixed(3)} s in the past`;
}
// run this a few times:
for (let i = 0; i < 200; i++) { setTimeout(() => { test().then(l => console.log(l))}, i * 20)}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment