Search code examples
c++winapivideocomms-media-foundation

Media Foundation SinkWriter (MP4 FastStart)


I'm using Media Foundation to create an MP4 (H264 + AAC) output file out of an input MP4 after a series of filters. The creation of the video works perfectly and the video is reproduced without issues locally. The problem is that when executed remotely (through a web player or even VLC), the video doesn't start until it's fully downloaded.

I checked and confirmed that the http website hosting the file supports the Accepts-Ranges header field and after a while I figured out that the problem happens because the file hasn't been created with the "fast start" flag that allows for progressive download of the video.

I tried to search online for a solution, but I've been unable to find a way to apply that flag with Media Foundation's Sinkwriter. Any idea? (I can't use any external application to do this as this code is going to run within the Windows Store environment)


Solution

  • Progressive download requires that moov box goes before mdat box in the MPEG-4 file, which typically requires additional effort when the file is generated, and which is not the default behavior with Media Foundation.

    Media Foundation introduced MF_MPEG4SINK_MOOV_BEFORE_MDAT attribute to handle this:

    The default behavior of the mpeg4 media sink is to write 'moov' after 'mdat' box. Setting this attribute causes the generated file to write 'moov' before 'mdat' box.

    In order for the mpeg4 sink to use this attribute, the byte stream passed in must not be slow seek or remote for .

    This feature involves an additional file copying/remuxing.

    Note minimal requirements. Or, you need to post-process the file to move the moov box to the beginning.

    See also: