HLS parser
This module is available to use in your EdgeWorkers code bundle to parse and modify HTTP Live Streaming (HLS) playlist files.
The protocols for transferring unbounded streams of multimedia data, as described in RFC 8216, were applied when creating this EdgeWorkers module.
Currently the HLS parser only accepts complete UTF-8 encoded m3u8 playlist file contents and does not work in streaming mode to parse chunks of data.
👍 Refer to Import a JavaScript module for instructions on how to load an external JavaScript module into the EdgeWorkers
main.jsfile.
Code samples and use cases
Go to the the EdgeWorkers GitHub repo for access to the HLS module, the main helper class you need to import into your main.js file and the TypeScript definition file.
To help you learn more about how to use the HLS Parser module, we’ve added the following use cases to this guide. You can also find the code samples in the EdgeWorkers GitHub repo.
-
Manifest and Playlist Manipulation
Dynamically create personalized renditions of available Video on demand (VOD) playlists. -
Live program replacement
Dynamically replace a live stream or linear program with a blackout slate during a time period and within specific geographic locations. -
Content insertion
Dynamically add auxiliary media content to an existing Video on Demand (VOD) asset using Pre-Roll, Mid-Roll, and Post-Roll operations.
Manifest Parsing
parseManifest()
Parses and creates an internal object using the input text manifest. Returns a Playlist Object.
stringifyManifest()
Creates the manifest from the playlistObject passed from parseManifest(). Returns a string.
getManifestType()
Indicates the type of playlist that was requested such as, master, media, or invalid playlist. Returns a MANIFEST_TYPE enum.
Variant Manipulation
preserveVariantsByBitrate()
Removes all variants with specified bitrates from the playlist. Returns true if a variant is deleted by this function, false otherwise.
preserveVariantsByResolution()
Removes all resolutions higher than the specified maximum supported resolution. It accepts a single resolution as a string in the format <width>x<height>. Returns true if a variant is deleted by this function, false otherwise.
moveVariantToIndex()
Moves the metadata of a variant with a given resolution to the newIndex. Returns the last index where the variant is updated. Its value can differ from the value of newIndex provided there can be repetition of the same resolution in a master playlist.
updateResolutionOrder()
Moves the metadata of all variants with specified resolutions to the top. The order of these variants is preserved as provided in the array of resolutions. Providing multiple entries of the same resolution can cause undesired results. This function internally uses the moveVariantToIndex to move the respective variants. Returns true if the resolution order is updated by this function, false otherwise.
Rendition Manipulation
preserveAudioRenditionByLanguage()
Removes audio renditions that use languages not specified in the master playlist. Returns true if a rendition is deleted by this function, false otherwise.
preserveSubtitleRenditionByLanguage()
Removes subtitle renditions that use languages not specified in the master playlist. Returns true if a rendition is deleted by this function, false otherwise.
Content (bumper) insertion
insertAuxiliaryContent()
Inserts segments from the auxiliary content into the primary media playlist at the specified duration. This auxiliary content:
- Must be present on the origin server as individual segments.
- Must have its own playlist.
- Can be inserted as pre (before), mid (middle), post (after) roll. The roll refers to the primary media playlist segments.
- Auxiliary content segments should adhere to the parent stream’s encoding profile such as bitrates, resolutions, and segment duration as the original content.
This function does not return a value.
Exceptions
Throws an {Error} with a message if the argument type checks fails. For example, if the playlist object sent is invalid the following error is thrown.
Error('Received invalid playlist object, expected valid master playlist object.')
playlistObj
Instance of the mediaPlaylist that corresponds to the primary media bumpers.
Bumper
A holder for an auxiliary media playlist that will be inserted into the primary media playlist at the specified duration.
LiveManifestTransformer
The LiveManifestTransformer class replaces black slate segments in the HLS playlist file.
The window in the HLS file:
- Should not contain overlapping windows.
- Has at least one
HLS_TAG_EXTX_PROGRAM_DATE_TIMEtag for the media playlist.
LiveManifestTransformer()
Constructor for the new LiveManifestTransformer Object to initialize the policies. Returns a LiveManifestTransformer Object.
marshalPolicy()
Resolves content of each policy to the media playlist object. Content can be either a URL or media playlist encoded as a string. If a URL is passed as content, the actual blackout slate media playlist is fetched and marshaled to a JavaScript playlist object. Returns a Promise<Policy[]>.
If marshaling fails for the UTF-8 encoded playlistError the following error message is thrown.
Error('Failed to parse blackslate media playlist')
transform()
Transforms the original playlist with the content to be replaced from the policy for the specified window.
Returns a string - Modified playlist manifest.