0.10.6 • Published 3 years ago
Share package
hls-parser Provides synchronous functions to read/write HLS playlists (conforms to the HLS spec rev.23 , the Apple Low-Latency Spec rev. 2020/02/05 , and HLS.js's Low-Latency spec )
Install
Usage const HLS = require('hls-parser'); // For node
// For browsers, just use dist/hls-parser.min.js defined as a UMD module.
// Parse the playlist
const playlist = HLS.parse(textData);
// You can access the playlist as a JS object
if (playlist.isMasterPlaylist) {
// Master playlist
} else {
// Media playlist
}
// Create a new playlist
const {MediaPlaylist, Segment} = HLS.types;
const obj = new MediaPlaylist({
targetDuration: 9,
playlistType: 'VOD',
segments: [
new Segment({
uri: 'low/1.m3u8',
duration: 9
})
]
});
// Convert the object into a text
HLS.stringify(obj);
/*
#EXTM3U
#EXT-X-TARGETDURATION:9
#EXT-X-PLAYLIST-TYPE:VOD
#EXTINF:9,
low/1.m3u8
*/API HLS.parse(str)Converts a text playlist into a structured JS object
params return value An instance of either MasterPlaylist or MediaPlaylist (See Data format below.)
HLS.stringify(obj)Converts a JS object into a plain text playlist
params Name Type Required Default Description obj MasterPlaylist or MediaPlaylist (See Data format below.)Yes N/A An object returned by HLS.parse() or a manually created object
return value A text data that conforms to the HLS playlist spec
HLS.setOptions(obj)Updates the option values
params Name Type Required Default Description obj Object Yes {} An object holding option values which will be used to overwrite the internal option values.
supported options Name Type Default Description strictModeboolean false If true, the function throws an error when parse/stringify failed. If false, the function just logs the error and continues to run. allowClosedCaptionsNoneboolean false If true, CLOSED-CAPTIONS attribute on the EXT-X-STREAM-INF tag will be set to the enumerated-string value NONE when there are no closed-captions. See CLOSED-CAPTIONS silentboolean false If true, console.error will be suppressed.
HLS.getOptions()Retrieves the current option values
return value A cloned object containing the current option values
HLS.typesAn object that holds all the classes described below.
Data format This section describes the structure of the object returned by parse() method.
DataProperty Type Required Default Description typestring Yes N/A Either playlist or segment or part}
Playlist (extends Data)Property Type Required Default Description isMasterPlaylistboolean Yes N/A true if this playlist is a master playlisturistring No undefined Playlist URL versionnumber No undefined See EXT-X-VERSION independentSegmentsboolean No false See EXT-X-INDEPENDENT-SEGMENTS startobject({offset: number, precise: boolean}) No undefined See EXT-X-START sourcestring No undefined The unprocessed text of the playlist
MasterPlaylist (extends Playlist)VariantRenditionProperty Type Required Default Description typestring Yes N/A See TYPE attribute in EXT-X-MEDIA uristring No undefined See URI attribute in EXT-X-MEDIA groupIdstring Yes N/A See GROUP-ID attribute in EXT-X-MEDIA languagestring No undefined See LANGUAGE attribute in EXT-X-MEDIA assocLanguagestring No undefined See ASSOC-LANGUAGE attribute in EXT-X-MEDIA namestring Yes N/A See NAME attribute in EXT-X-MEDIA isDefaultboolean No false See DEFAULT attribute in EXT-X-MEDIA autoselectboolean No false See AUTOSELECT attribute in EXT-X-MEDIA forcedboolean No false See FORCED attribute in EXT-X-MEDIA instreamIdstring No undefined See INSTREAM-ID attribute in EXT-X-MEDIA characteristicsstring No undefined See CHARACTERISTICS attribute in EXT-X-MEDIA channelsstring No undefined See CHANNELS attribute in EXT-X-MEDIA
SessionDataMediaPlaylist (extends Playlist)Segment (extends Data)Property Type Required Default Description uristring Yes* N/A URI of the media segment. *Not required if the segment contains EXT-X-PRELOAD-HINT tag durationnumber Yes* N/A See EXTINF *Not required if the segment contains EXT-X-PRELOAD-HINT tag titlestring No undefined See EXTINF byterangeobject ({length: number, offset: number}) No undefined See EXT-X-BYTERANGE discontinuityboolean No undefined See EXT-X-DISCONTINUITY mediaSequenceNumbernumber No 0 See the description about 'Media Sequence Number' in 3. Media Segments discontinuitySequencenumber No 0 See the description about 'Discontinuity Sequence Number' in 6.2.1. General Server Responsibilities keyKeyNo undefined See EXT-X-KEY mapMediaInitializationSectionNo undefined See EXT-X-MAP programDateTimeDateNo undefined See EXT-X-PROGRAM-DATE-TIME dateRangeDateRangeNo undefined See EXT-X-DATERANGE markersSpliceInfoNo [] SCTE-35 messages associated with this segment partsPartialSegmentNo [] Partial Segments that constitute this segment
PartialSegment (extends Data)Property Type Required Default Description hintboolean No false true indicates a hinted resource (TYPE=PART) See EXT-X-PRELOAD-HINT uristring Yes N/A See URI attribute in EXT-X-PART durationnumber Yes* N/A See DURATION attribute in EXT-X-PART *Not required if hint is true independentboolean No undefined See INDEPENDENT attribute in EXT-X-PART byterangeobject ({length: number, offset: number}) No undefined See BYTERANGE attribute in EXT-X-PART gapboolean No undefined See GAP attribute in EXT-X-PART
PrefetchSegment (extends Data)KeyProperty Type Required Default Description methodstring Yes N/A See METHOD attribute in EXT-X-KEY uristring No undefined See URI attribute in EXT-X-KEY ivBuffer(length=16)No undefined See IV attribute in EXT-X-KEY formatstring No undefined See KEYFORMAT attribute in EXT-X-KEY formatVersionstring No undefined See KEYFORMATVERSIONS attribute in EXT-X-KEY
MediaInitializationSectionProperty Type Required Default Description hintboolean No false true indicates a hinted resource (TYPE=MAP) See EXT-X-PRELOAD-HINT uristring Yes N/A See URI attribute in EXT-X-MAP byterangeobject ({length: number, offset: number}) No undefined See BYTERANGE attribute in EXT-X-MAP
DateRangeProperty Type Required Default Description idstring Yes N/A See ID attribute in EXT-X-DATERANGE classIdstring No undefined See CLASS attribute in EXT-X-DATERANGE startDateNo undefined See START-DATE attribute in EXT-X-DATERANGE endDateNo undefined See END-DATE attribute in EXT-X-DATERANGE durationnumber No undefined See DURATION attribute in EXT-X-DATERANGE plannedDurationnumber No undefined See PLANNED-DURATION attribute in EXT-X-DATERANGE endOnNextboolean No undefined See END-ON-NEXT attribute in EXT-X-DATERANGE attributesobject No {} A hash object that holds SCTE35 attributes and user defined attributes. See SCTE35-* and X- attributes in EXT-X-DATERANGE
SpliceInfoOnly EXT-X-CUE-OUT and EXT-X-CUE-IN tags are supported. Other SCTE-35-related tags are stored as raw (string) values.
Property Type Required Default Description typestring Yes N/A {'OUT', 'IN', 'RAW'} durationnumber No undefined Required if the type is 'OUT' tagNamestring No undefined Holds the tag name if any unsupported tag are found. Required if the type is 'RAW' valuestring No undefined Holds a raw (string) value for the unsupported tag.
RenditionReport