Skip to content

Downloads HLS Playlist file and TS chunks. You can use it for content pre-fetching from CDN to Edge Server for your end viewers.

License

Notifications You must be signed in to change notification settings

nurrony/hlsdownloader

Repository files navigation

HLSDownloader

Downloads HLS Playlist file and TS chunks. You can use it for content pre-fetching from CDN to Edge Server for your end viewers.

NPMDocumentationGitHub

⚠️ This package is native ESM and no longer provides a CommonJS export. If your project uses CommonJS, you will have to convert to ESM. Please don't open issues for questions regarding CommonJS / ESM.

⚠️ HLSDownloader v2.x.x is no longer maintained and we will not accept any backport requests.

Features

  • Retryable
  • Promise Based
  • Support for HTTP/2
  • Overwrite protection
  • Support for custom HTTP Headers
  • Support for custom HTTP Client
  • Bring your own progress bar during download
  • Concurrent download segments with multiple http connections

Prerequisites

  • node >=18.x.x

Installation

It is pretty straight forward

# using npm
npm install --save hlsdownloader
# or with yarn
yarn add hlsdownloader
# or pnpm
pnpm install hlsdownloader

How to use

destination field is optional. If destination is not provided it just fetches the content from origin. It can also be useful if you want to do content pre-fetching from CDN for your end viewers. If any TS or m3u8 variant download is failed it continues downloading others and reports after finishing.

It's simple as below with.

import HLSDownloader from 'hlsdownloader';

const options = {
  playlistURL: 'http://example.com/path/to/your/playlist.m3u8', // change it
  destination: '/tmp', // change it (optional: default '')
  concurrency: 10, // change it (optional: default = 1),
  overwrite: true, // change it (optional: default = false)
  // (optional: default = null
  onData: function (data) {
    console.log(data); // {url: "<url-just-downloaded>", totalItems: "<total-items-to-download>", path: "<absolute-path-of-download-loation>"}
  },
  // (optional: default = null
  onError: function (error) {
    console.log(error); // { url: "<URLofItem>", name: "<nameOfError>", message: "human readable message of error" }
  },
};
const downloader = new HLSDownloader(options);
downloader.startDownload().then(response => console.log(response));

ℹ️ Check example.js for working example

// on success
{
  total: <number>,
  playlistURL: 'your playlist url'
  message: 'Downloaded successfully',
}

// on partial download
{
  total: <number>,
  playlistURL: 'your playlist url',
  message: 'Download done with some errors',
  errors: [
    {
      name: 'InvalidPlaylist',
      message: 'Playlist parsing is not successful'
      url: 'https://cnd.hls-server.test/playlist.m3u8'
    }
  ] // items url that is skipped or could not downloaded for error
}

Advance Usage

HLSDownloader supports all Ky API except these options given below

  • uri
  • url
  • json
  • form
  • body
  • method
  • setHost
  • isStream
  • parseJson
  • prefixUrl
  • cookieJar
  • playlistURL
  • concurrency
  • allowGetBody
  • stringifyJson
  • methodRewriting

It also disable retry failed request that you can easily override

Running Tests

npm test

To run it on watch mode

npm run test:watch

Generate Documentations

npm docs:gen

Authors

👤 Nur Rony

Contributing

Contributions, issues and feature requests are welcome!
Feel free to check issues page. You can also take a look at the contributing guide.

Show your support

Give a ⭐️ if this project helped you!. I will be grateful if you all help me to improve this package by giving your suggestions, feature request and pull requests. I am all ears!!

Special Thanks to

License

Copyright © 2024 Nur Rony.
This project is MIT licensed.