<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: FirstLoveVibe</title>
    <description>The latest articles on DEV Community by FirstLoveVibe (@maniinpurple).</description>
    <link>https://dev.to/maniinpurple</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4263006%2F81ab0428-b42b-4fb3-ab48-4b658d5f6c5a.jpg</url>
      <title>DEV Community: FirstLoveVibe</title>
      <link>https://dev.to/maniinpurple</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/maniinpurple"/>
    <language>en</language>
    <item>
      <title>I turned HLS video streams into proper MP4 files entirely in the browser</title>
      <dc:creator>FirstLoveVibe</dc:creator>
      <pubDate>Tue, 24 Nov 2026 15:48:35 +0000</pubDate>
      <link>https://dev.to/maniinpurple/i-turned-hls-video-streams-into-proper-mp4-files-entirely-in-the-browser-25p</link>
      <guid>https://dev.to/maniinpurple/i-turned-hls-video-streams-into-proper-mp4-files-entirely-in-the-browser-25p</guid>
      <description>&lt;p&gt;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: &lt;strong&gt;&lt;a href="https://github.com/maniinpurple/Tsundoku" rel="noopener noreferrer"&gt;Tsundoku&lt;/a&gt;&lt;/strong&gt;, a Firefox/Chrome extension where I tick the episodes I want, press Download, and get normal MP4 files with subtitles inside.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  How I built it
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h1&gt;
  
  
  THE STORY
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Step 1: Learning from ani-cli
&lt;/h2&gt;

&lt;p&gt;I started by reverse-engineering the source of &lt;a href="https://github.com/pystardust/ani-cli" rel="noopener noreferrer"&gt;ani-cli&lt;/a&gt;, 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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: The "video" wasn't a video
&lt;/h2&gt;

&lt;p&gt;I expected an &lt;code&gt;.mp4&lt;/code&gt;. What I got was an &lt;strong&gt;HLS playlist&lt;/strong&gt; (&lt;code&gt;.m3u8&lt;/code&gt;): a text file listing the qualities, and for each quality, a list of short pieces:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#EXTINF:5.005,
https://cdn.example/1080/seg_000.ts.jpg
#EXTINF:5.005,
https://cdn.example/1080/seg_001.ts.jpg
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

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

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: Not keeping a 900 MB episode in memory
&lt;/h2&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;handle&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;dir&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getFileHandle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;create&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;writable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;handle&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createWritable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;keepExistingData&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;writable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"write"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;position&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;segment&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Firefox's memory stayed flat during the whole download. It also made &lt;strong&gt;resuming&lt;/strong&gt; 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.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 4: Turning TS into MP4
&lt;/h2&gt;

&lt;p&gt;I learned that TS and MP4 are just different &lt;em&gt;containers&lt;/em&gt; for the same video and audio. Changing the container ("remuxing") keeps every bit and takes seconds, no re-encoding needed. I used &lt;a href="https://github.com/videojs/mux.js" rel="noopener noreferrer"&gt;mux.js&lt;/a&gt; for that.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 5: QuickTime didn't like my MP4
&lt;/h2&gt;

&lt;p&gt;The file played in VLC and ffprobe looked fine. Then I checked it with AVFoundation, which is what QuickTime uses:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;AVFoundation duration 97406.08 seconds
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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 &lt;strong&gt;fragmented MP4&lt;/strong&gt; itself: it stores timing in many small &lt;code&gt;moof&lt;/code&gt; boxes, and Apple's players misread it.&lt;/p&gt;

&lt;p&gt;The fix was the hardest part of the project. I wrote a small &lt;strong&gt;"flattener"&lt;/strong&gt; that reads each fragment as it's produced, appends all the samples into one big &lt;code&gt;mdat&lt;/code&gt; box, and at the end writes a normal &lt;code&gt;moov&lt;/code&gt; box with the full sample tables (&lt;code&gt;stts&lt;/code&gt;, &lt;code&gt;ctts&lt;/code&gt;, &lt;code&gt;stss&lt;/code&gt;, &lt;code&gt;stsz&lt;/code&gt;, &lt;code&gt;co64&lt;/code&gt;). 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.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 6: Subtitles inside the video
&lt;/h2&gt;

&lt;p&gt;The site serves WebVTT subtitle files. Separate &lt;code&gt;.srt&lt;/code&gt; files are fine for VLC, but I watch on a Mac and wanted a subtitle menu in QuickTime. I learned that needs &lt;code&gt;mov_text&lt;/code&gt; (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.&lt;/p&gt;

&lt;p&gt;Small surprise along the way: the player labelled &lt;em&gt;every&lt;/em&gt; subtitle as &lt;code&gt;en&lt;/code&gt;, even the Arabic and French ones, so I read the language from the label instead.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 7: Making it work in Chrome too
&lt;/h2&gt;

&lt;p&gt;I built the extension with &lt;a href="https://wxt.dev" rel="noopener noreferrer"&gt;WXT&lt;/a&gt;, so one codebase builds for both browsers. I started with Firefox. When I tried Chrome, I learned its background script is a &lt;strong&gt;service worker with no DOM&lt;/strong&gt;, so a few things broke:&lt;/p&gt;

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

&lt;h2&gt;
  
  
  Step 8: Testing with real downloads
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  What's next
&lt;/h2&gt;

&lt;p&gt;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 (&lt;code&gt;docs/HOW-IT-WORKS.md&lt;/code&gt;) on how I investigated the site with DevTools and curl:&lt;/p&gt;

&lt;p&gt;👉 &lt;a href="https://github.com/maniinpurple/Tsundoku" rel="noopener noreferrer"&gt;https://github.com/maniinpurple/Tsundoku&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;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!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>typescript</category>
      <category>webdev</category>
      <category>opensource</category>
    </item>
  </channel>
</rss>
