What is m3u8stream?
The m3u8stream npm package is used to create a readable stream from an M3U8 playlist, which is commonly used for HTTP Live Streaming (HLS). This allows you to download or process media streams in a programmatic way.
What are m3u8stream's main functionalities?
Stream from M3U8 URL
This feature allows you to create a readable stream from an M3U8 URL and pipe it to a writable stream, such as a file. In this example, the media stream from the M3U8 URL is saved to 'output.ts'.
const m3u8stream = require('m3u8stream');
const fs = require('fs');
const stream = m3u8stream('http://example.com/playlist.m3u8');
stream.pipe(fs.createWriteStream('output.ts'));
Handle Stream Events
This feature allows you to handle various events emitted by the stream, such as 'progress' and 'end'. This can be useful for tracking the download progress and performing actions when the download is complete.
const m3u8stream = require('m3u8stream');
const stream = m3u8stream('http://example.com/playlist.m3u8');
stream.on('progress', (segment, totalSegments, downloaded) => {
console.log(`Downloading segment ${segment.num} of ${totalSegments}`);
console.log(`Downloaded: ${downloaded} bytes`);
});
stream.on('end', () => {
console.log('Download complete');
});
Custom Request Options
This feature allows you to pass custom request options, such as headers, to the underlying HTTP requests made by m3u8stream. In this example, a custom User-Agent header is set for the requests.
const m3u8stream = require('m3u8stream');
const fs = require('fs');
const stream = m3u8stream('http://example.com/playlist.m3u8', {
requestOptions: {
headers: {
'User-Agent': 'my-custom-user-agent'
}
}
});
stream.pipe(fs.createWriteStream('output.ts'));
Other packages similar to m3u8stream
hls.js
hls.js is a JavaScript library that allows you to play HLS streams in browsers that do not support HLS natively. It is primarily used for client-side playback in web applications, whereas m3u8stream is used for server-side streaming and downloading.
fluent-ffmpeg
fluent-ffmpeg is a Node.js library for working with FFmpeg, a powerful multimedia framework. It can be used to download and process media streams, including HLS streams. However, it requires FFmpeg to be installed and is more complex to use compared to m3u8stream.
node-m3u8stream
Reads segments from a m3u8 playlist or DASH MPD file into a consumable stream.
![codecov](https://codecov.io/gh/fent/node-m3u8stream/branch/master/graph/badge.svg)
Usage
const fs = require('fs');
const m3u8stream = require('m3u8stream')
m3u8stream('http://somesite.com/link/to/the/playlist.m3u8')
.pipe(fs.createWriteStream('videofile.mp4'));
API
m3u8stream(url, [options])
Creates a readable stream of binary media data. options
can have the following
begin
- Where to begin playing the video. Accepts an absolute unix timestamp or date, and a relative time in the formats 1:23:45.123
and 1m2s
.liveBuffer
- How much buffer in milliseconds to have for live streams. Default is 20000
.chunkReadahead
- How many chunks to preload ahead. Default is 3
.highWaterMark
- How much of the download to buffer into the stream. See node's docs for more. Note that the actual amount buffered can be higher since each chunk request maintains its own buffer.requestOptions
- Any options you want to pass to miniget, such as headers
.parser
- Either "m3u8" or "dash-mpd". Defaults to "m3u8".id
- If playlist contains multiple media options. Otherwise, the first representation will be picked.
Stream has an .end()
method, that if called, stops requesting segments, and refreshing the playlist.
Limitations
Currently, it does not support encrypted media segments. This is because the sites where this was tested on and intended for, YouTube and Twitch, don't use it.
This does not parse master playlists, only media playlists. If you want to parse a master playlist to get links to media playlists, you can try the m3u8 module.
Install
npm install m3u8stream
Tests
Tests are written with mocha
npm test