m3u8parse
Version:
Structural parsing of Apple HTTP Live Streaming .m3u8 format
37 lines (36 loc) • 1.45 kB
JavaScript
import { AttrList } from "./attrlist.js";
import { BasePlaylist, cloneAttrArray, cloneAttrMap, isStringish, rewriteAttrs, rewriteMappedAttrs } from "./playlist-base.js";
export class MainPlaylist extends BasePlaylist {
static cast(index) {
if (!index.master) {
throw new Error('Cannot cast a media playlist');
}
return index;
}
constructor(obj) {
obj ?? (obj = {});
super(obj);
this.master = true;
if (obj.master !== undefined && !!obj.master !== this.master) {
throw new Error('Cannot create from media playlist');
}
this.variants = obj.variants?.map((variant) => ({ uri: variant.uri, info: new AttrList(variant.info) })) ?? [];
this.groups = cloneAttrMap(obj.groups);
this.iframes = cloneAttrArray(obj.iframes);
this.data = cloneAttrMap(obj.data);
this.session_keys = cloneAttrArray(obj.session_keys);
}
rewriteUris(mapFn) {
for (const variant of this.variants) {
const newUri = mapFn(variant.uri, 'variant', variant);
if (isStringish(newUri)) {
variant.uri = newUri;
}
}
rewriteAttrs(mapFn, this.iframes, 'iframe');
rewriteMappedAttrs(mapFn, this.groups, 'group');
rewriteMappedAttrs(mapFn, this.data, 'data');
rewriteAttrs(mapFn, this.session_keys, 'session-key');
return this;
}
}