can't add audio stream to MP4 file using sinkwriter from Media Foundation

喜你入骨 提交于 2019-12-10 23:37:17

问题


It's been a week since I'm trying to make my encoder works...

So the things is:

  1. I create a file using the Sink Writer from MF.

  2. I write all my video sample.

    --> (If I stop there and call the Finish methods, I'm able to read the file using VLC but if I do the bellow stuff, the file will be "corrupted")

  3. I write all my audio sample.

  4. I call the finish function and I get a HRESULT code saying: "Sink could not create valid output file because required headers were not provided to the sink". I totally understand the error, just I don't get how to solve it...

If you guys need any code , I'll be glad to put it here.


回答1:


I ran into this exact error when I first started using the MP4 container (upon finalizing the sink).

Error 0xC00D4A45: Sink could not create valid output file because required headers were not provided to the sink.

You didn't mention what sub-type of audio samples you are feeding the sink, nor what sub-type of audio stream that was added (for output), however I am confident that your issue is with the latter.

Developing with media foundation, the MP4 container is easiest to configure using MFAudioFormat_AAC or MFAudioFormat_MP3. If you look at the details for the MP4 File Sink at this link, you will see that the sink can generate the sample description box (stsd) for the following formats:

- H.264/AVC video
- AAC audio
- MP3 audio

It is possible to use a few other formats, however you will have to manually supply the sample description box (stsd) description using MF_MT_MPEG4_SAMPLE_DESCRIPTION GUID when configuring the attributes before creating the sink. This is accomplished using the following function:

// IMFAttributes::SetBlob
attributes->SetBlob(MF_MT_MPEG4_SAMPLE_DESCRIPTION, buffer, buffer_size);

However, there are few other types which can be used, as described by the MP4 File Source here. Under Media Types, there is a table of various allowed types. Other than AAC/MP3 previously mentioned, there are few remaining audio types, and none are very attractive choices. You will find that sticking to MFAudioFormat_AAC or MFAudioFormat_MP3 will serve you well.

Hope this helps.

EDIT:

If you choose to use an audio sub-type other than AAC or MP3, and provide a MF_MT_MPEG4_SAMPLE_DESCRIPTION configuration using IMFAttributes::SetBlob, the sample description box (stsd) is described in this answer. As that answer states, the boxes are nested.

Pertaining to Audio, take for instance, if you to select MFAudioFormat_PCM, MP4 File Source lists 5 entries in the Media Types section. As a result, use the appropriate sample entry code ('raw ', 'sowt', 'twos', 'NONE', 0x00) when building the sample description box. Note the space in 'raw '. The high level summary is as follows:

//  'raw '  Audio   MFAudioFormat_PCM   8-bit PCM audio
//  'sowt'  Audio   MFAudioFormat_PCM   16-bit little-endian PCM audio
//  'twos'  Audio   MFAudioFormat_PCM   16-bit big-endian PCM audio
//  'NONE'  Audio   MFAudioFormat_PCM   8-bit or 16-bit big-endian PCM audio
//   0x00   Audio   MFAudioFormat_PCM   8-bit or 16-bit big-endian PCM audio

You may also find these registered codecs interesting.



来源:https://stackoverflow.com/questions/25129263/cant-add-audio-stream-to-mp4-file-using-sinkwriter-from-media-foundation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!