What is m3u8-parser?
The m3u8-parser npm package is a library for parsing M3U8 files, which are used for HTTP Live Streaming (HLS). It allows you to parse M3U8 playlists and extract information such as segments, media sequences, and more.
What are m3u8-parser's main functionalities?
Parsing M3U8 Playlists
This feature allows you to parse an M3U8 playlist string and convert it into a JavaScript object. The code sample demonstrates how to use the Parser class to parse a simple M3U8 string and log the resulting manifest object.
const { Parser } = require('m3u8-parser');
const parser = new Parser();
const m3u8String = '#EXTM3U\n#EXTINF:10,\nhttp://media.example.com/first.ts\n#EXTINF:10,\nhttp://media.example.com/second.ts\n#EXT-X-ENDLIST';
parser.push(m3u8String);
parser.end();
console.log(parser.manifest);
Extracting Segment Information
This feature allows you to extract information about individual segments from an M3U8 playlist. The code sample demonstrates how to parse an M3U8 string and then iterate over the segments to log their URI and duration.
const { Parser } = require('m3u8-parser');
const parser = new Parser();
const m3u8String = '#EXTM3U\n#EXTINF:10,\nhttp://media.example.com/first.ts\n#EXTINF:10,\nhttp://media.example.com/second.ts\n#EXT-X-ENDLIST';
parser.push(m3u8String);
parser.end();
const segments = parser.manifest.segments;
segments.forEach(segment => {
console.log(`URI: ${segment.uri}, Duration: ${segment.duration}`);
});
Handling Media Sequences
This feature allows you to handle media sequences in an M3U8 playlist. The code sample demonstrates how to parse an M3U8 string and log the media sequence number from the resulting manifest object.
const { Parser } = require('m3u8-parser');
const parser = new Parser();
const m3u8String = '#EXTM3U\n#EXT-X-MEDIA-SEQUENCE:1\n#EXTINF:10,\nhttp://media.example.com/first.ts\n#EXTINF:10,\nhttp://media.example.com/second.ts\n#EXT-X-ENDLIST';
parser.push(m3u8String);
parser.end();
console.log(`Media Sequence: ${parser.manifest.mediaSequence}`);
Other packages similar to m3u8-parser
hls-parser
The hls-parser package is another library for parsing M3U8 playlists. It offers similar functionality to m3u8-parser, allowing you to parse playlists and extract segment information. However, it also provides additional features such as support for writing M3U8 playlists and more detailed error handling.
m3u8
The m3u8 package is a comprehensive library for working with M3U8 files. It supports both parsing and generating M3U8 playlists, making it a more versatile option compared to m3u8-parser. It also includes features for handling encryption keys and other advanced HLS features.
hls.js
The hls.js package is a JavaScript library that focuses on playing HLS streams in browsers. While it includes some parsing capabilities, its primary focus is on providing a robust HLS playback experience. It is more feature-rich in terms of playback options compared to m3u8-parser, but less focused on parsing alone.
m3u8-parser
m3u8 parser
Table of Contents
Installation
npm install --save m3u8-parser
The npm installation is preferred, but Bower works, too.
bower install --save m3u8-parser
Usage
To include m3u8-parser on your website or web application, use any of the following methods.
License
Apache-2.0. Copyright (c) Brightcove, Inc