I'm designing a server application that stores a live H.264 video stream as MP4 for later consumption by browsers. Since a server will need to handle as many simultaneous streams as possible, I believe I/O will be the natural bottleneck and I'd like to keep I/O to a minimum. I'm running into the classic MP4 moov/mdat ordering problem: MP4 generators prefer to write the mdat box (containing the actual media frames) first, then write the moov box (containing file offsets and other structural information) later, after it actually knows what the mdat file offsets are. MP4 consumers prefer the opposite for progressive streaming -- reading the moov box first, so the mdat structure is known and the video can start playing quickly without needing to download the whole file.
The usual solution is to post-process MP4 files to move the moov box ahead of the mdat box, and rewriting file offsets accordingly. However, for a high-volume application, I'd like to avoid the I/O penalty of writing the incoming video data to disk, reading it all back in, and writing it again with a new arrangement.
Several approaches come to mind:
I'm leaning towards #4 for now. Is there any more practical way to solve this problem?
I've discovered that Microsoft Media Foundation generates MPEG4 file in which the MDAT atom comes before the MOOV atom. MOOV before MDAT is required for streaming. I assumed the solution to my prob...
1 1 I've discovered that Microsoft Media Foundation generates MPEG4 file in which the MDAT atom comes before the MOOV atom. MOOV before MDAT is required for streaming. I assumed the solution to my problem would be to use the MF_MPEG4SINK_MOOV_BEFORE_MDATattribute when creating the sink, but I can't seem to get it to have an effect.
If the moov atom is at the beginning of a video, the preview generation works. If the the moov atom is at end of the video, the generation fails. If it is a local file would it be possible to load it completely? Removing the limit in the stream_get_contents call generates the preview just fine.
It seems as there is no simple solution to creating mp4 previews on a truly remote storage except pulling the entire file without pre-processing the file, because the moov atom placement might be at the end of the file.
If the size of the moov data is estimable, preallocate space at the beginning of the file. Some of it might be wasted, but you won't have to recalculate any offsets, and it will avoid the I/O costs in some percentage of cases. Just make sure you have a fallback when the moov data gets bigger than your estimate.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With