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.
我正在设计一个服务器应用程序,它可以将live H.264视频流存储为MP4,供以后的浏览器使用。由于服务器需要处理尽可能多的并发流,所以我认为I/O将成为自然的瓶颈,我希望将I/O保持到最小。我正在运行一个经典的MP4 moov/mdat排序问题:MP4生成器优先编写mdat框(包含实际的媒体框架),然后在它实际知道mdat文件偏移量之后,再编写moov框(包含文件偏移量和其他结构信息)。MP4的消费者喜欢相反的渐进流——首先阅读moov box,所以mdat结构是已知的,视频可以快速播放,而不需要下载整个文件。
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.
通常的解决方案是将moov框置于mdat框前面,并相应地重写文件偏移量。但是,对于一个高容量的应用程序,我希望避免将传入的视频数据写入磁盘的I/O惩罚,重新读取所有的数据,并重新编写新的安排。
Several approaches come to mind:
有几种方法浮现在脑海中:
- Post-process the MP4 as usual, incurring the I/O penalty and potentially delaying the video's availability. (not good.)
- 后处理MP4,像往常一样,引起I/O处罚,并可能延迟视频的可用性。(不好)。
- Use fragmented MP4 and small fragment sizes that are reasonable to fit in memory. (This could have negative effects on whole-file seekability, I'd think.)
- 使用分散的MP4和较小的片段大小,这是适合内存的。(我认为这可能会对整个文件的seekability产生负面影响。)
- It would be awesome if filesystems provided a fast "prepend" option to add new blocks to the beginning of a file's block chain. (I don't think this has been invented yet.)
- 如果文件系统提供了一个快速的“prepend”选项,将新的块添加到文件块链的开头,那就太棒了。(我认为这还没有被发明出来。)
- Generate the MP4 as two files -- an "mdat" file (containing the actual media frames) and a "moov" file (containing the ftyp header and moov data). These two files, if concatenated, would produce a valid moov-first MP4 file. A simple web server module could present a virtual .mp4 file to the user, but read the .moov and .mdat files behind the scenes.
- 生成MP4作为两个文件——一个“mdat”文件(包含实际的媒体框架)和一个“moov”文件(包含ftyp头和moov数据)。如果将这两个文件连接起来,将产生一个有效的moov-first MP4文件。一个简单的web服务器模块可以向用户呈现一个虚拟的.mp4文件,但是在后台读取.moov和.mdat文件。
I'm leaning towards #4 for now. Is there any more practical way to solve this problem?
现在我倾向于4。有什么更实际的方法来解决这个问题吗?
1 个解决方案
#1
2
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.
如果moov数据的大小是可估计的,那么在文件开始时预先分配空间。其中一些可能会被浪费,但是您不必重新计算任何偏移量,并且它将避免在某些情况下的I/O成本。当moov数据大于你的估计时,确保你有一个回退。
#1
2
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.
如果moov数据的大小是可估计的,那么在文件开始时预先分配空间。其中一些可能会被浪费,但是您不必重新计算任何偏移量,并且它将避免在某些情况下的I/O成本。当moov数据大于你的估计时,确保你有一个回退。