Skip to content

Instantly share code, notes, and snippets.

@birme
Created November 1, 2021 09:31
Show Gist options
  • Save birme/67be1829eb370fb7badcad510eb70c78 to your computer and use it in GitHub Desktop.
Save birme/67be1829eb370fb7badcad510eb70c78 to your computer and use it in GitHub Desktop.
An endpoint that on-the-fly repeats an HLS VOD
// $ node hls-repeat.js
// Example (2 reps): http://localhost:8000/slate-consuo2.mp4/master.m3u8?r=2
// where r is how many times the VOD to be repeated
const { HLSProxy } = require("@eyevinn/hls-proxy");
const HLSRepeatVod = require("@eyevinn/hls-repeat");
const proxy = new HLSProxy({
originHandler: async () => {
// Origin where the VOD is found
return "https://maitv-vod.lab.eyevinn.technology";
},
masterManifestHandler: async (request, baseUrl, m3u) => {
const repeats = request.raw.query["r"] || 2;
m3u.items.StreamItem.map(item => {
const params = require("querystring").stringify({
bw: item.get("bandwidth"),
r: repeats,
src: request.raw.url
});
item.set("uri", item.get("uri") + "?" + params);
});
return m3u.toString();
},
mediaManifestHandler: async (request, baseUrl, m3u) => {
const sourceUrl = new URL(request.raw.query["src"], baseUrl);
const hlsVod = new HLSRepeatVod(sourceUrl.href, request.raw.query["r"]);
await hlsVod.load();
return hlsVod.getMediaManifest(request.raw.query["bw"]);
},
segmentRedirectHandler: async (request, baseUrl) => {
return (new URL(request.raw.url, baseUrl)).href;
}
});
proxy.listen(8000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment