title: Mux ts segments to fragmented mp4 in realtime
body: <p>I have an HLS stream which plays in the browser. What I want to do is have a URL like <code>/stream.mp4</code> that plays the livestream back as a fragmented mp4 from the current point in time. This way you could download the stream as a single file and it would work without any js dependencies.</p>
  <p>The method I thought of is:</p>
  <ol>
  <li>send an mp4 header</li>
  <li>wait for a new segment</li>
  <li>convert the segment into an mp4 fragment with <code>ffmpeg</code></li>
  <li>send the mp4 fragment</li>
  <li>go to 2.</li>
  </ol>
  <p>(I know you can concatenate <code>.ts</code> segments but they don't play in Firefox.)</p>
  <p><a href="https://stackoverflow.com/a/9734251">This answer</a> describes how to create a fragmented mp4 when you have all the segments at hand (tl;dr use <code>-movflags frag_keyframe+emptymoov</code>). But I want to do this on the fly so each new HLS segment is converted to an mp4 fragment as soon as it's created.</p>
  <p>I think this is similar to what DASH does natively, where any chunk <code>chunk-$n.m4s</code> can be appended to <code>init.m4s</code> and it'll be a proper fragmented mp4. But can this be done without using DASH?</p>
  <p>How can I transmux a ts segment into an mp4 fragment?</p>
  <p>Edit: I found a way of doing what I wanted, but not through transmuxing mpegts segments to fmp4. It turns out that later versions of HLS do support fragmented mp4 (like DASH does), and FFmpeg provides the <a href="https://ffmpeg.org/ffmpeg-formats.html#Options-6" rel="nofollow noreferrer"><code>-hls_segment_type fmp4</code></a> option for this.</p>