Skip to content
arielfikruPublic

About

Turn a Hugging Face Storage Bucket into the storage+CDN layer for HLS video

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

hlsdrive

Turn a Hugging Face Storage Bucket into the storage+CDN layer for HLS video.

Upload an mp4. The server transcodes it into a fMP4/CMAF HLS ladder with ffmpeg, syncs it to a private Hugging Face bucket via the hf CLI, and serves playback where the playlists come from your own origin but every media byte is fetched by the browser directly from Hugging Face's CDN.

This started as a research prototype: does the "small text from your origin, all the bytes from a signed CDN redirect" pattern actually hold together end to end, given that Hugging Face buckets weren't designed to be a video CDN? It does — verified in a real browser, both serving modes, zero playback errors. See docs/FINDINGS.md for the measured details and docs/ARCHITECTURE.md for how the request paths work.

Why this is interesting

  • Hugging Face gives every account 100GB of private bucket storage, and its resolve endpoint issues short-lived signed CDN URLs (no auth needed, range requests supported, access-control-allow-origin: *) — the exact shape of a redirect-based CDN, for free, without provisioning one.
  • The catch is that a naive implementation breaks in a very specific, silent way: redirecting the playlist request (not just the segments) to the CDN corrupts relative-URL resolution and playback stalls with no error. hlsdrive exists because that trap (and a couple of adjacent ones) were non-obvious enough to be worth documenting properly. See docs/FINDINGS.md.

Quick start (demo)

npm install
cp .env.example .env   # fill in HF_TOKEN and HF_BUCKET
npm run demo

Open http://localhost:3200, upload an mp4, wait for its status to reach ready, and play it back via the MP4 or HLS toggle.

Library usage

hlsdrive is a small library first, demo second. Everything under src/ is framework-agnostic and importable on its own:

import { transcodeToHls, uploadDir, uploadFile, resolveAsset } from 'hlsdrive';

const { renditions } = await transcodeToHls(sourcePath, outDir);
await uploadFile(sourcePath, `videos/${id}/source.mp4`);
await uploadDir(outDir, `videos/${id}/hls`);

// later, in your own HTTP route handler:
const descriptor = await resolveAsset(id, 'hls/master.m3u8');
// -> {kind: 'redirect'|'proxy'|'body', ...} — turn it into a response

See docs/INTEGRATION.md for wiring examples against Express, Hono, Next.js, and plain node:http.

Layout

src/            reusable library (no HTTP server, no framework)
  index.js      public API barrel
  transcode.js  ffmpeg -> fMP4/CMAF HLS ladder
  hf-bucket.js  HF bucket client: uploadDir/uploadFile/signUrl + TTL cache
  playlist.js   playlist rewriting (SIGN_MODE=rewrite)
  asset.js      framework-agnostic redirect/proxy/rewrite decision
  store.js      tiny JSON store (demo-support only, not part of the public API)
  config.js     env config
demo/           reference application: plain node:http, upload form, hls.js player
  server.js
  routes/       upload.js, videos.js, stream.js — thin adapters over src/
  public/       index.html, app.js
docs/
  ARCHITECTURE.md   request paths, why playlists are proxied and segments redirected
  FINDINGS.md       every measured number, presented as a research findings doc
  INTEGRATION.md    how to use this from another project
  OPERATIONS.md     env vars, bucket setup, redirect vs rewrite, troubleshooting

Requirements

  • Node >= 20
  • ffmpeg / ffprobe on PATH (or FFMPEG_BIN/FFPROBE_BIN set)
  • The hf CLI on PATH (or HF_BIN set), authenticated against a bucket you control
  • A Hugging Face account with a private Storage Bucket

Limitations and risks

  • This is a research prototype, not a sanctioned CDN. Hugging Face's Content Policy lists "Hosting excessive or irrelevant data in repositories" under Platform Abuse. Heavy, sustained public-facing video egress through a personal bucket is the most likely thing to attract moderation attention. Don't point unbounded public traffic at this without accepting that risk — see docs/FINDINGS.md for the full reasoning.
  • The demo's multipart parser handles exactly one file field; it is not a general-purpose multipart implementation.
  • The demo has no auth on its upload endpoint — do not expose it publicly as-is.
  • src/store.js is a flat JSON file with serialized writes, fine for a handful of videos in a demo, not a real database for concurrent multi-user production use.
  • SIGN_MODE=rewrite playlists carry pre-signed URLs that expire ~60 minutes after the playlist was fetched; long-running sessions should use SIGN_MODE=redirect instead (see docs/OPERATIONS.md).

License

MIT — see LICENSE.

About

Turn a Hugging Face Storage Bucket into the storage+CDN layer for HLS video

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages