I'm a student from Vietnam. Whenever a long power cut or a storm is coming, I usually download some anime to watch offline. Doing that one episode at a time got old fast, so I decided to build a tool for it: Tsundoku, a Firefox/Chrome extension where I tick the episodes I want, press Download, and get normal MP4 files with subtitles inside.

I didn't know much about how video streaming works when I started. This is the path I took, including the parts where I got stuck.

How I built it

I'm not good at frontend, so I built this together with Claude (AI). It acted as my teacher to me, and most of the Chrome version and the UI. My part was investigating the site, testing, deciding what the tool should do, and reading and debugging what we wrote, which is where most of my learning came from.

THE STORY

Step 1: Learning from ani-cli

I started by reverse-engineering the source of ani-cli, a terminal tool for watching anime, to see how it finds the videos. Then I opened the browser DevTools on the site myself and watched the Network tab while clicking around.

I found out the site is really three services glued together: the site itself (episode lists, a small API), an embedded player on another domain, and a CDN that serves the video. The player's page held the real video address, lightly hidden with base64 and XOR. Once I decoded it in the console, I finally saw the address.

Step 2: The "video" wasn't a video

I expected an .mp4. What I got was an HLS playlist (.m3u8): a text file listing the qualities, and for each quality, a list of short pieces:

#EXTINF:5.005, https://cdn.example/1080/seg_000.ts.jpg #EXTINF:5.005, https://cdn.example/1080/seg_001.ts.jpg 

One 24-minute episode was about 200–300 of these segments. They end in .jpg, but when I checked one with ffprobe it was plain MPEG-TS video, so the extension is just a disguise.

When I opened the playlist in a new tab, I got 403 Forbidden. Testing with curl, I learned the server checks the Referer header and only answers requests that look like they come from the player. An extension can't set Referer on its own fetch(), so I added a temporary declarativeNetRequest rule, limited to the extension's own requests (tabIds: [-1]) so it never touches normal browsing.

My first real download also got blocked halfway: I had fetched about 100 MB in 3 seconds and the CDN started answering 403. I slowed down to 4 segments at a time, with longer waits when the server says 403 or 429, and it's been stable since.

Step 3: Not keeping a 900 MB episode in memory

A 1080p episode can be close to a gigabyte, so I couldn't just collect everything in RAM. I learned about OPFS (the Origin Private File System), a private disk storage for the extension, and wrote each segment straight to it:

const handle = await dir.getFileHandle(name, { create: true }); const writable = await handle.createWritable({ keepExistingData: true }); await writable.write({ type: "write", position, data: segment }); 

Firefox's memory stayed flat during the whole download. It also made resuming possible: I save the size of each finished segment, so if the browser stops halfway, I cut the file back to the last complete segment and continue from there.

Step 4: Turning TS into MP4

I learned that TS and MP4 are just different containers for the same video and audio. Changing the container ("remuxing") keeps every bit and takes seconds, no re-encoding needed. I used mux.js for that.

My first attempt gave me a 300-second video from a 241-second clip. It turned out I was feeding mux.js arbitrary 3 MB chunks instead of whole segments, and it repeated frames. Feeding it one whole segment at a time fixed it.

Step 5: QuickTime didn't like my MP4

The file played in VLC and ffprobe looked fine. Then I checked it with AVFoundation, which is what QuickTime uses:

AVFoundation duration 97406.08 seconds 

That's 27 hours for a 4-minute clip. I tried ffmpeg's own fragmented MP4 to compare, and that came back as 4.6 seconds with no audio at all. So the problem wasn't my code, it was fragmented MP4 itself: it stores timing in many small moof boxes, and Apple's players misread it.

The fix was the hardest part of the project. I wrote a small "flattener" that reads each fragment as it's produced, appends all the samples into one big mdat box, and at the end writes a normal moov box with the full sample tables (stts, ctts, stss, stsz, co64). After that, AVFoundation said 241.48 s, exactly right, and ffmpeg decoded it without a single warning. I learned more about the MP4 format here than anywhere else.

Step 6: Subtitles inside the video

The site serves WebVTT subtitle files. Separate .srt files are fine for VLC, but I watch on a Mac and wanted a subtitle menu in QuickTime. I learned that needs mov_text (tx3g) tracks. I made a reference file with ffmpeg, compared its boxes with mine, and wrote the same structure: each subtitle line becomes a sample with a 16-bit length and the text, and gaps become empty samples. One episode ended up with nine languages, all selectable in QuickTime.

Small surprise along the way: the player labelled every subtitle as en, even the Arabic and French ones, so I read the language from the label instead.

Step 7: Making it work in Chrome too

I built the extension with WXT, so one codebase builds for both browsers. I started with Firefox. When I tried Chrome, I learned its background script is a service worker with no DOM, so a few things broke:

  • DOMParser didn't exist, so I switched to node-html-parser
  • URL.createObjectURL didn't exist either, so a small Chrome-only offscreen page now creates the download links
  • Chrome doesn't start the service worker on its own after a restart, so I listen to runtime.onStartup to resume unfinished downloads

Step 8: Testing with real downloads

Normal unit tests wouldn't have caught the QuickTime bug, so my main test downloads a real episode: a script builds a test version, starts a hidden Firefox or Chrome, downloads an episode and checks the file. There's also a GitHub Action that checks every 6 hours whether the site and player still work.

What's next

Right now Tsundoku supports one site, but each site and player is a small plug-in, so I want to add more. The code is on GitHub (GPL-3.0), with a guide (docs/HOW-IT-WORKS.md) on how I investigated the site with DevTools and curl:

👉 https://github.com/maniinpurple/Tsundoku

If you have feedback or ideas, I'd love to hear them. And if you liked reading this, a STAR on the repo would make my day!