I have an HLS stream which plays in the browser. What I want to do is have a URL like /stream.mp4
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.
The method I thought of is:
- send an mp4 header
- wait for a new segment
- convert the segment into an mp4 fragment with
ffmpeg
- send the mp4 fragment
- go to 2.
(I know you can concatenate .ts
segments but they don't play in Firefox.)
This answer describes how to create a fragmented mp4 when you have all the segments at hand (tl;dr use -movflags frag_keyframe+emptymoov
). 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.
I think this is similar to what DASH does natively, where any chunk chunk-$n.m4s
can be appended to init.m4s
and it'll be a proper fragmented mp4. But can this be done without using DASH?
How can I transmux a ts segment into an mp4 fragment?
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 -hls_segment_type fmp4
option for this.