0.13.2 • Published 4 days ago

hls-parser v0.13.2

Weekly downloads
11,481
License
MIT
Repository
github
Last release
4 days ago

HLS parser tests Coverage Status Known Vulnerabilities npm Downloads XO code style

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

NPM npm.io

Usage

import { parse, types, stringify } from 'hls-parser';

// Parse the playlist
const playlist = 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} = types;
const obj = new MediaPlaylist({
  targetDuration: 9,
  playlistType: 'VOD',
  segments: [
    new Segment({
      uri: 'low/1.m3u8',
      duration: 9
    })
  ]
});
// Convert the object into a text
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

NameTypeRequiredDefaultDescription
strstringYesN/AA text data that conforms to the HLS playlist spec

return value

An instance of either MasterPlaylist or MediaPlaylist (See Data format below.)

HLS.stringify(obj, postProcess)

Converts a JS object into a plain text playlist

params

NameTypeRequiredDefaultDescription
objMasterPlaylist or MediaPlaylist (See Data format below.)YesN/AAn object returned by HLS.parse() or a manually created object
postProcessPostProcessNoundefinedA function to be called for each segment or variant to manipulate the output.
PostProcess
PropertyTypeRequiredDefaultDescription
segmentProcessor(lines: string[], start: number, end: number, segment: Segment, i: number) => voidNoundefinedA function to manipulate the segment output.
variantProcessor(lines: string[], start: number, end: number, variant: Variant, i: number) => voidNoundefinedA function to manipulate the variant output.

return value

A text data that conforms to the HLS playlist spec

HLS.setOptions(obj)

Updates the option values

params

NameTypeRequiredDefaultDescription
objObjectYes{}An object holding option values which will be used to overwrite the internal option values.
supported options
NameTypeDefaultDescription
strictModebooleanfalseIf true, the function throws an error when parse/stringify failed. If false, the function just logs the error and continues to run.
allowClosedCaptionsNonebooleanfalseIf 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
silentbooleanfalseIf true, console.error will be suppressed.

HLS.getOptions()

Retrieves the current option values

return value

A cloned object containing the current option values

HLS.types

An object that holds all the classes described below.

Data format

This section describes the structure of the object returned by parse() method.

data structure

Data

PropertyTypeRequiredDefaultDescription
typestringYesN/AEither playlist or segment or part}

Playlist (extends Data)

PropertyTypeRequiredDefaultDescription
isMasterPlaylistbooleanYesN/Atrue if this playlist is a master playlist
uristringNoundefinedPlaylist URL
versionnumberNoundefinedSee EXT-X-VERSION
independentSegmentsbooleanNofalseSee EXT-X-INDEPENDENT-SEGMENTS
startobject({offset: number, precise: boolean})NoundefinedSee EXT-X-START
sourcestringNoundefinedThe unprocessed text of the playlist

MasterPlaylist (extends Playlist)

PropertyTypeRequiredDefaultDescription
variantsVariantNo[]See EXT-X-STREAM-INF and EXT-X-I-FRAME-STREAM-INF
currentVariantnumberNoundefinedArray index that points to the chosen item in variants
sessionDataListSessionDataNo[]See EXT-X-SESSION-DATA
sessionKeyListKeyNo[]See EXT-X-SESSION-KEY

Variant

PropertyTypeRequiredDefaultDescription
uristringYesN/AURI of the variant playlist
isIFrameOnlybooleanNoundefinedtrue if the variant is an I-frame media playlist. See EXT-X-I-FRAME-STREAM-INF
bandwidthnumberYesN/ASee BANDWIDTH attribute in EXT-X-STREAM-INF
averageBandwidthnumberNoundefinedSee AVERAGE-BANDWIDTH attribute in EXT-X-STREAM-INF
scorenumberNoundefinedSee SCORE attribute in EXT-X-STREAM-INF
codecsstringNoundefinedSee CODECS attribute in EXT-X-STREAM-INF
resolutionobject ({width: number, height: number})NoundefinedSee RESOLUTION attribute in EXT-X-STREAM-INF
frameRatenumberNoundefinedSee FRAME-RATE attribute in EXT-X-STREAM-INF
hdcpLevelstringNoundefinedSee HDCP-LEVEL attribute in EXT-X-STREAM-INF
allowedCpc[object ({format: string, cpcList: string})]NoundefinedSee ALLOWED-CPC attribute in EXT-X-STREAM-INF
videoRangestring {"SDR","HLG","PQ"}NoundefinedSee VIDEO-RANGE attribute in EXT-X-STREAM-INF
stableVariantIdstringNoundefinedSee STABLE-VARIANT-ID attribute in EXT-X-STREAM-INF
audioRendition(type='AUDIO')No[]See AUDIO attribute in EXT-X-STREAM-INF
videoRendition(type='VIDEO')No[]See VIDEO attribute in EXT-X-STREAM-INF
subtitlesRendition(type='SUBTITLES')No[]See SUBTITLES attribute in EXT-X-STREAM-INF
closedCaptionsRendition(type='CLOSED-CAPTIONS')No[]See CLOSED-CAPTIONS attribute in EXT-X-STREAM-INF
currentRenditionsobject ({audio: number, video: number, subtitles: number, closedCaptions: number})No{}A hash object that contains array indices that points to the chosen Rendition for each type

Rendition

PropertyTypeRequiredDefaultDescription
typestringYesN/ASee TYPE attribute in EXT-X-MEDIA
uristringNoundefinedSee URI attribute in EXT-X-MEDIA
groupIdstringYesN/ASee GROUP-ID attribute in EXT-X-MEDIA
languagestringNoundefinedSee LANGUAGE attribute in EXT-X-MEDIA
assocLanguagestringNoundefinedSee ASSOC-LANGUAGE attribute in EXT-X-MEDIA
namestringYesN/ASee NAME attribute in EXT-X-MEDIA
isDefaultbooleanNofalseSee DEFAULT attribute in EXT-X-MEDIA
autoselectbooleanNofalseSee AUTOSELECT attribute in EXT-X-MEDIA
forcedbooleanNofalseSee FORCED attribute in EXT-X-MEDIA
instreamIdstringNoundefinedSee INSTREAM-ID attribute in EXT-X-MEDIA
characteristicsstringNoundefinedSee CHARACTERISTICS attribute in EXT-X-MEDIA
channelsstringNoundefinedSee CHANNELS attribute in EXT-X-MEDIA

SessionData

PropertyTypeRequiredDefaultDescription
idstringYesN/ASee DATA-ID attribute in EXT-X-SESSION-DATA
valuestringNoundefinedSee VALUE attribute in EXT-X-SESSION-DATA
uristringNoundefinedSee URI attribute in EXT-X-SESSION-DATA
languagestringNoundefinedSee LANGUAGE attribute in EXT-X-SESSION-DATA

MediaPlaylist (extends Playlist)

PropertyTypeRequiredDefaultDescription
targetDurationnumberYesN/ASee EXT-X-TARGETDURATION
mediaSequenceBasenumberNo0See EXT-X-MEDIA-SEQUENCE
discontinuitySequenceBasenumberNo0See EXT-X-DISCONTINUITY-SEQUENCE
endlistbooleanNofalseSee EXT-X-ENDLIST
playlistTypestringNoundefinedSee EXT-X-PLAYLIST-TYPE
isIFramebooleanNoundefinedSee EXT-X-I-FRAMES-ONLY
segmentsSegmentNo[]A list of available segments
prefetchSegmentsPrefetchSegmentNo[]A list of available prefetch segments
lowLatencyCompatibilityobject ({canBlockReload: boolean, canSkipUntil: number, holdBack: number, partHoldBack: number})NoundefinedSee CAN-BLOCK-RELOAD, CAN-SKIP-UNTIL, HOLD-BACK, and PART-HOLD-BACK attributes in EXT-X-SERVER-CONTROL
partTargetDurationnumberNo*undefined*Required if the playlist contains one or more EXT-X-PART tags. See EXT-X-PART-INF
renditionReportsRenditionReportNo[]Update status of the associated renditions
skipnumberNo0See EXT-X-SKIP

Segment (extends Data)

PropertyTypeRequiredDefaultDescription
uristringYes*N/AURI of the media segment. *Not required if the segment contains EXT-X-PRELOAD-HINT tag
durationnumberYes*N/ASee EXTINF *Not required if the segment contains EXT-X-PRELOAD-HINT tag
titlestringNoundefinedSee EXTINF
byterangeobject ({length: number, offset: number})NoundefinedSee EXT-X-BYTERANGE
discontinuitybooleanNoundefinedSee EXT-X-DISCONTINUITY
mediaSequenceNumbernumberNo0See the description about 'Media Sequence Number' in 3. Media Segments
discontinuitySequencenumberNo0See the description about 'Discontinuity Sequence Number' in 6.2.1. General Server Responsibilities
keyKeyNoundefinedSee EXT-X-KEY
mapMediaInitializationSectionNoundefinedSee EXT-X-MAP
programDateTimeDateNoundefinedSee EXT-X-PROGRAM-DATE-TIME
dateRangeDateRangeNoundefinedSee EXT-X-DATERANGE
markersSpliceInfoNo[]SCTE-35 messages associated with this segment
partsPartialSegmentNo[]Partial Segments that constitute this segment

PartialSegment (extends Data)

PropertyTypeRequiredDefaultDescription
hintbooleanNofalsetrue indicates a hinted resource (TYPE=PART) See EXT-X-PRELOAD-HINT
uristringYesN/ASee URI attribute in EXT-X-PART
durationnumberYes*N/ASee DURATION attribute in EXT-X-PART *Not required if hint is true
independentbooleanNoundefinedSee INDEPENDENT attribute in EXT-X-PART
byterangeobject ({length: number, offset: number})NoundefinedSee BYTERANGE attribute in EXT-X-PART
gapbooleanNoundefinedSee GAP attribute in EXT-X-PART

PrefetchSegment (extends Data)

PropertyTypeRequiredDefaultDescription
uristringYesN/ASee value of EXT-X-PREFETCH
discontinuitybooleanNoundefinedSee EXT-X-PREFETCH-DISCONTINUITY
mediaSequenceNumbernumberNo0See the description about 'Media Sequence Number' in 3. Media Segments
discontinuitySequencenumberNo0See the description about 'Discontinuity Sequence Number' in 6.2.1. General Server Responsibilities

Key

PropertyTypeRequiredDefaultDescription
methodstringYesN/ASee METHOD attribute in EXT-X-KEY
uristringNoundefinedSee URI attribute in EXT-X-KEY
ivArrayBuffer(length=16)NoundefinedSee IV attribute in EXT-X-KEY
formatstringNoundefinedSee KEYFORMAT attribute in EXT-X-KEY
formatVersionstringNoundefinedSee KEYFORMATVERSIONS attribute in EXT-X-KEY

MediaInitializationSection

PropertyTypeRequiredDefaultDescription
hintbooleanNofalsetrue indicates a hinted resource (TYPE=MAP) See EXT-X-PRELOAD-HINT
uristringYesN/ASee URI attribute in EXT-X-MAP
byterangeobject ({length: number, offset: number})NoundefinedSee BYTERANGE attribute in EXT-X-MAP

DateRange

PropertyTypeRequiredDefaultDescription
idstringYesN/ASee ID attribute in EXT-X-DATERANGE
classIdstringNoundefinedSee CLASS attribute in EXT-X-DATERANGE
startDateNoundefinedSee START-DATE attribute in EXT-X-DATERANGE
endDateNoundefinedSee END-DATE attribute in EXT-X-DATERANGE
durationnumberNoundefinedSee DURATION attribute in EXT-X-DATERANGE
plannedDurationnumberNoundefinedSee PLANNED-DURATION attribute in EXT-X-DATERANGE
endOnNextbooleanNoundefinedSee END-ON-NEXT attribute in EXT-X-DATERANGE
attributesobjectNo{}A hash object that holds SCTE35 attributes and user defined attributes. See SCTE35-* and X- attributes in EXT-X-DATERANGE

SpliceInfo

Only EXT-X-CUE-OUT and EXT-X-CUE-IN tags are supported. Other SCTE-35-related tags are stored as raw (string) values.

PropertyTypeRequiredDefaultDescription
typestringYesN/A{'OUT', 'IN', 'RAW'}
durationnumberNoundefinedRequired if the type is 'OUT'
tagNamestringNoundefinedHolds the tag name if any unsupported tag are found. Required if the type is 'RAW'
valuestringNoundefinedHolds a raw (string) value for the unsupported tag.

RenditionReport

PropertyTypeRequiredDefaultDescription
uristringYesN/ASee URI attribute in EXT-X-RENDITION-REPORT
lastMSNnumberNoundefinedSee LAST-MSN attribute in EXT-X-RENDITION-REPORT
lastPartnumberNoundefinedSee LAST-PART attribute in EXT-X-RENDITION-REPORT
0.13.0

5 days ago

0.13.1

4 days ago

0.13.2

4 days ago

0.12.0

13 days ago

0.11.1

20 days ago

0.11.0

21 days ago

0.10.9

1 month ago

0.10.8

7 months ago

0.10.7

8 months ago

0.10.6

1 year ago

0.10.5

1 year ago

0.10.4

3 years ago

0.10.3

3 years ago

0.10.1

3 years ago

0.10.2

3 years ago

0.10.0

3 years ago

0.9.0

3 years ago

0.8.0

3 years ago

0.7.0

3 years ago

0.6.4

4 years ago

0.6.3

4 years ago

0.6.2

4 years ago

0.6.1

4 years ago

0.6.0

4 years ago

0.5.1

4 years ago

0.5.0

4 years ago

0.4.6

5 years ago

0.4.5

5 years ago

0.4.4

5 years ago

0.4.3

5 years ago

0.4.2

5 years ago

0.4.1

5 years ago

0.4.0

5 years ago

0.3.0

5 years ago

0.2.5

5 years ago

0.2.4

5 years ago

0.2.3

5 years ago

0.2.2

6 years ago

0.2.1

6 years ago

0.2.0

6 years ago

0.1.8

6 years ago

0.1.7

6 years ago

0.1.6

7 years ago

0.1.5

7 years ago

0.1.4

7 years ago

0.1.3

7 years ago

0.1.2

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago