UNPKG

m3u8stream

Version:

Reads segments from a m3u8 or dash playlist into a consumable stream.

84 lines • 3.02 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isSet = void 0; exports.selectVariant = selectVariant; exports.selectMedia = selectMedia; const isSet = (value) => typeof value !== 'undefined'; exports.isSet = isSet; const highestBandwidth = (list) => [...list].sort((a, b) => (b.bandwidth || 0) - (a.bandwidth || 0))[0]; function selectVariant(variants, filter) { var _a; if (!variants.length) { return undefined; } if (filter === undefined) { return ((_a = variants.find((variant) => variant.contentType === 'video')) !== null && _a !== void 0 ? _a : variants[0]); } if (typeof filter === 'function') { return variants.find(filter); } const filterStr = filter.toString(); // Quality keywords if (/^(highest|best)$/i.test(filterStr)) { return highestBandwidth(variants); } if (/^(lowest|worst)$/i.test(filterStr)) { return [...variants].sort((a, b) => { var _a, _b; return ((_a = a.bandwidth) !== null && _a !== void 0 ? _a : Number.MAX_SAFE_INTEGER) - ((_b = b.bandwidth) !== null && _b !== void 0 ? _b : Number.MAX_SAFE_INTEGER); })[0]; } // Exact identifier, name, URL, or index matching const exact = variants.find((v) => { if (v.id === filterStr) return true; if (v.name === filterStr) return true; if (`${v.index}` === filterStr) return true; if (v.url) { if (v.url === filterStr) return true; const lastPart = v.url.split(/[?#]/)[0].split('/').filter(Boolean).pop(); if (lastPart === filterStr) return true; if ((lastPart === null || lastPart === void 0 ? void 0 : lastPart.replace(/\.[^.]*$/, '')) === filterStr) return true; } return false; }); if (exact) { return exact; } // Resolution shorthand (e.g. '1920x1080') const resMatch = /^(\d+)x(\d+)$/i.exec(filterStr); if (resMatch) { const width = parseInt(resMatch[1], 10); const height = parseInt(resMatch[2], 10); const matches = variants.filter((v) => v.width === width && v.height === height); if (matches.length) { return highestBandwidth(matches); } } // Height shorthand (e.g. '1080p' or '1080') const heightMatch = /^(\d+)p?$/i.exec(filterStr); if (heightMatch) { const height = parseInt(heightMatch[1], 10); const matches = variants.filter((v) => v.height === height); if (matches.length) { return highestBandwidth(matches); } } return undefined; } function selectMedia(media, filter) { if (typeof filter !== 'string') { return undefined; } return media.find((m) => m.name === filter || m.groupId === filter || m.uri === filter || m.language === filter); } //# sourceMappingURL=parser.js.map