I write a video player that shows a live video stream (RTSP) and also encodes it into an mp4 file at the same time.
I want to also be able to playback (seek backward or forward) the video from that mp4 file while the stream continues being encoded to that mp4 file.
What is the best approach?
One approach is as follows:
Since "moov atom" is only added to the final mp4 file, I cannot read from mp4 file while the stream is still being encoded to it.
Therefore, from what I understand, in order to do this, I need to convert the stream into a fragmented MP4 format. Thus, the frames are written as chunks that each have their own chunk-specific MOOF header. These chunks are playable on their own. What I don't understand is how to read those chunks from mp4 file. Is there a C# library for that?
+ Reply to Thread
Results 1 to 8 of 8
Use the faststart option to place the moov atom at the start of the file.
Can you ellaborate? I'm not trying to stream the mp4 file. The stream comes from a camera and is being saved to a file (mp4). I need to be able to read (playback) whatever it is in the file while the stream is still being saved to that mp4 file.
Last edited by theateist; 12th Jul 2022 at 12:48.
I thought I remembered using the faststart option (with ffmpeg) to create an MP4 file that could be played while it was still downloading (or still being rendered). But I was wrong.
You can mux to an MKV or TS file, and that file can be played while it is still being downloaded (TS is specifically designed for that). You could then perform a quick remux to MP4 once the download has completed.
Just to clarify, I'm not trying to stream. I already have access to rtsp stream that I need to save to a file and be able to seek (back and forth to any position) in that file while that stream is still being saved to that file.
So, basically, in order to do what I need to have access file's metadata.
- Let's say I save the stream in fragmented mp4. Meaning the stream is saved as chunks(fragments) into the mp4. Now, let's say 10 fragments were saved to the file and while reading the first 10 fragments, another 6 fragments have been added to the file. So, how do I read those fragments in general? Is there a header in that fragmented mp4 that says how many fragments are currently in the file? Or, do I just read (how?) the first fragment and its header should tell me how many frames are inside the fragment and whether there is an additional fragment after it?
- You suggested saving (mux) the stream into TS file. But, again, how do I read the frames that are currently in the file while additional frames are being saved to that TS file? When new frames are added, is there some kind of metadata (index table) that I can use to be able to jump to any frame?
Do you know any good source where I can read about this? I'm not sure what to write in a google search.
I just play the file with a media player while it's being downloaded. Some players will stop playing once they reach what was the end of the video at the time they started playing it. Some will continue to play but the seek bar will stop moving once it reaches the right end. That is, if you start playing when 10 segments have been downloaded, some player will stop playing after playing those 10 segments (you have to restart the player to play more), some will continue to play beyond those 10 segments but the seek bar will be stuck at the far right (the seek bar only represents those 10 segments), you can't manually seek past that point.
If you are asking how to do that in your own program -- I don't know. But I assume it's basically the same as playing any media file. Open the file in read-only mode, get the stats, start playing. If you want to continue playing beyond the "end" you probably just have to get the stats again, then continue playing.
That was actually the whole point of this post - to get an answer on how to do this in code. What do you mean by "get the stats"?