npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

m3u8-grabber

v1.1.3

Published

HLS archiver — downloads m3u8 playlists, .ts and fMP4/CMAF segments, encryption keys to GCP Cloud Storage

Readme

Support this project

If this was helpful to you, please buy me a beer on PayPal: Click Here to Buy Me a Beer


m3u8-grabber

HLS archiver that downloads m3u8 playlists, video segments, encryption keys, and init segments to Google Cloud Storage. Preserves the original directory structure as-is.

Supported Formats

| Format | Extensions | Status | |--------|-----------|--------| | MPEG-TS | .ts | Supported | | Fragmented MP4 (fMP4) | .m4s, .m4v, .mp4, .cmfv, .cmfa | Supported | | CMAF | .m4s, .mp4 | Supported | | Init segments | #EXT-X-MAP URI | Supported | | Encryption keys | #EXT-X-KEY URI | Supported | | Master playlists | Multi-variant with #EXT-X-STREAM-INF | Supported | | Audio playlists | #EXT-X-MEDIA URI | Supported |

Install

npm install m3u8-grabber

Requires Node.js >= 18.

Usage

import { Storage } from '@google-cloud/storage';
import { downloadM3u8 } from 'm3u8-grabber';

const storage = new Storage({
  credentials: require('./google-credentials.json'),
});

await downloadM3u8(
  storage,
  'https://example.com/hls/master.m3u8',
  'my-gcs-bucket',
  'output',          // local temp dir for m3u8 files
  'user-1234',       // GCS path prefix
);

With progress callbacks

await downloadM3u8(
  storage,
  'https://example.com/hls/master.m3u8',
  'my-gcs-bucket',
  'output',
  'user-1234',
  null,              // baseUrl (auto-detected)
  {
    onSegmentComplete: (file, completed, total) => {
      console.log(`[${completed}/${total}] ${file}`);
    },
    onSegmentError: (file, error) => {
      console.error(`Failed: ${file}`, error.message);
    },
  },
);

How It Works

  1. Downloads the m3u8 manifest file
  2. Parses it using a state-machine parser (not regex) to extract:
    • Child m3u8 playlists (variant streams, audio tracks)
    • Media segments (.ts, .m4s, .m4v, .mp4, etc.)
    • Init segments (#EXT-X-MAP URIs)
    • Encryption keys (#EXT-X-KEY URIs)
  3. Recursively processes child playlists
  4. Streams all segments directly to GCS (no local buffering of large files)
  5. Size-aware skip: existing GCS files are only skipped if their size matches the expected Content-Length — zero-byte or partially-uploaded files are re-downloaded

Exported Functions

| Function | Description | |----------|-------------| | downloadM3u8(storage, url, bucket, localDir, gcsDir, baseUrl?, options?) | Download a full HLS stream to GCS | | streamDownloadToGcp(bucket, url, output, storage?, credsPath?, skip?) | Stream a single file to GCS | | uploadLocalFileToGCS(storage, bucket, src, dest, skip?, retry?) | Upload a local file to GCS with retry | | downloadFile(url, outputPath) | Download a file to local disk | | extractTsUrls(m3u8Content) | Extract segment URLs (.ts + fMP4) from manifest | | extractM3u8Urls(m3u8Content) | Extract child playlist URLs from manifest | | extractKeyUrls(m3u8Content) | Extract encryption key URLs from #EXT-X-KEY tags | | isValidManifestPath(url) | Check if a string is a valid file path (not a tag line) | | extractBaseUrl(url) | Get the base URL (everything before the filename) | | extractFilename(url) | Get the filename from a URL (strips query params) | | extractParentFolder(url, offset?) | Get a parent directory name from a URL | | createStorageClient(credsPath) | Create a GCS Storage client from a credentials file |

Retry & Resilience

  • HTTP downloads: 5 retries with linear backoff (axios-retry)
  • GCS uploads: 5 retries with size verification after each attempt
  • GCS client: IdempotencyStrategy.RetryAlways, max 10 retries, 500s total timeout
  • Resume support: size-aware skip checks Content-Length header against GCS blob metadata — partial uploads are detected and re-downloaded

Running Tests

Local: Place google-creds.json at project root (GCP service account JSON key).

CI/CD: Set GOOGLE_SERVICE_ACCOUNT_CREDS (JSON or base64-encoded JSON) and NODE_ENV=test as pipeline secrets.

npm test              # watch mode
npm run test:no-reloading  # single run

Unit tests (parsing) run without GCS credentials. Integration tests (download + upload) require valid credentials and create/delete a temporary GCS bucket.


Support this project

If this was helpful to you, please buy me a beer on PayPal: Click Here to Buy Me a Beer