Turning an HLS stream into an MP4 inside a Chrome extension (MV3 lessons learned)
I recently shipped a Chrome extension that saves online course lessons as MP4 files. The video side turned out to be the hard part: the lessons are HLS streams, and Manifest V3 doesn't make big binary jobs easy. Here are
I recently shipped a Chrome extension that saves online course lessons as MP4 files. The video side turned out to be the hard part: the lessons are HLS streams, and Manifest V3 doesn't make big binary jobs easy. Here are the problems I hit and what worked, in case you're building anything similar.
The shape of the problem
An HLS lesson isn't one file. The player loads a master playlist (.m3u8), picks a quality, then fetches a few hundred 2–6 second segments (MPEG-TS or fMP4). Often audio is a separate rendition. To give the user one .mp4 you have to:
- find the right playlist,
- download every segment (fast, but without hammering the host),
- remux video + audio into an MP4 container,
- hand the result to
chrome.downloads.
1. Finding the playlist: webRequest, not page scraping
The cleanest signal is the player's own network traffic. A chrome.webRequest.onBeforeRequest listener (read-only, no blocking) sees the .m3u8 URL the moment the player asks for it:
chrome.webRequest.onBeforeRequest.addListener((d) => {
if (/\.m3u8(\?|$)/.test(d.url)) onCaptured(d.tabId, d.url, d.initiator);
}, { urls: ['https://*.example-video-host.com/*'] });
Keep the host list tight. Reviewers (and users) look at it.
2. Some hosts check Referer / Origin
Fetching segments from the extension gave me 403s, because the CDN expected requests to come from the course site. declarativeNetRequest session rules fix that without broad permissions:
await chrome.declarativeNetRequest.updateSessionRules({
addRules: [{
id: ruleId, priority: 1,
action: { type: 'modifyHeaders', requestHeaders: [
{ header: 'referer', operation: 'set', value: 'https://www.example-course-site.com/' },
{ header: 'origin', operation: 'set', value: 'https://www.example-course-site.com' },
]},
condition: { urlFilter: '||example-video-host.com/', tabIds: [chrome.tabs.TAB_ID_NONE],
initiatorDomains: [chrome.runtime.id] },
}],
});
tabIds: [TAB_ID_NONE] + initiatorDomains: [chrome.runtime.id] means the rule only touches the extension's own requests, never the user's browsing.
3. The service worker can't do the heavy lifting
An MV3 service worker can be killed after ~30 s idle and has no DOM. Holding 500 MB of segments there is a bad idea. The fix is an offscreen document:
await chrome.offscreen.createDocument({
url: 'offscreen.html',
reasons: ['BLOBS'],
justification: 'Assemble downloaded video segments into one file',
});
The offscreen page downloads the segments, remuxes them, creates a blob: URL and messages it back; the service worker then calls chrome.downloads.download({ url: blobUrl }).
4. Remuxing in the browser with mux.js
mux.js transmuxes MPEG-TS into fragmented MP4 in plain JS. No ffmpeg.wasm, no 25 MB download. Feed it segments in order; it emits an init segment plus media fragments you can concatenate. When audio is a separate rendition, transmux both and interleave by timestamp.
5. Big lessons and memory: write to OPFS
A two-hour 1080p lesson can be over a gigabyte. Instead of keeping every chunk in RAM, I stream remuxed output into the Origin Private File System and build the final Blob from the file:
const root = await navigator.storage.getDirectory();
const fh = await root.getFileHandle(tempName, { create: true });
const w = await fh.createWritable();
// for each remuxed chunk: await w.write(chunk);
await w.close();
const file = await fh.getFile(); // a Blob backed by disk
Delete the temp file once chrome.downloads reports complete, and also on cancel.
6. Cancel has to cancel everything
Users click "stop". That has to abort in-flight fetches (one AbortController per job), tell the offscreen document to drop its job, cancel the chrome.downloads item if it already started, and delete the OPFS temp file. Otherwise you leak half-written gigabyte files.
7. Firefox: no offscreen documents
Firefox runs MV3 backgrounds as event pages and has no chrome.offscreen. My port loads the offscreen script directly into the background page and replaces the runtime messages with direct function calls, plus a tiny keep-alive while a job runs. Firefox's webRequest also has no initiator, so use originUrl / documentUrl instead.
Result
The extension is called Keep My Course. It does this for Skool lessons (native player and Loom embeds), adds a searchable transcript from the captions, and everything runs locally: nothing is uploaded.
If you're fighting HLS in an extension, I'm happy to answer questions in the comments. And if you see a better way to handle separate audio renditions, I'd love to hear it.
Earlier I wrote about the user side of this, why right-click "Save video as" doesn't work on Skool.
Disclosure: I'm the developer of Keep My Course.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.