mbaros
mbaros

Reputation: 845

Encode Audio using Sink Writer

I found this article that explaing how to encode a video using Media Foundation.

I am trying to encode an audio using the principle used in the above link.

I am stuck on setting a correct input media type for the sink writer.

Here is that part:

if (SUCCEEDED(hr))
    hr = MFCreateMediaType(&pMediaTypeIn);

if (SUCCEEDED(hr))
    hr = pMediaTypeIn->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Audio);

if (SUCCEEDED(hr))
    hr = pMediaTypeIn->SetGUID(MF_MT_SUBTYPE, MFAudioFormat_Float);

if (SUCCEEDED(hr))
    hr = pMediaTypeIn->SetUINT32(MF_MT_AUDIO_NUM_CHANNELS, cChannels);

if (SUCCEEDED(hr))
    hr = pMediaTypeIn->SetUINT32(MF_MT_AUDIO_SAMPLES_PER_SECOND, sampleRate);

if (SUCCEEDED(hr))
    hr = pSinkWriter->SetInputMediaType(streamIndex, pMediaTypeIn, NULL);

My code fails on the last line while setting the input media type.

Any help, how to handle this?

Thanks.

Upvotes: 0

Views: 1476

Answers (1)

VuVirt
VuVirt

Reputation: 1917

You also need to supply MF_MT_AUDIO_BITS_PER_SAMPLE in the media type. It must be set to 16. Check the Input types requirements here: https://msdn.microsoft.com/en-us/library/windows/desktop/dd742785(v=vs.85).aspx. The sample rate must be either 44100 or 48000. The subtype must be MFAudioFormat_PCM.

The most important thing is that you need to call AddStream on the Sink Writer, prior to calling SetMediaType, with an output audio type enumerated by a call to MFTranscodeGetAudioOutputAvailableTypes function with MFAudioFormat_AAC subtype if you need to encode audio as AAC.

MFCreateSinkWriterFromURL may not be able to guess that you need to encode to an mp4 container from the m4a extension. You might need to supply MFTranscodeContainerType_MPEG4 containe type using the MF_TRANSCODE_CONTAINERTYPE attribute when calling MFCreateSinkWriterFromURL. Another option would be to use MFCreateMPEG4MediaSink and MFCreateSinkWriterFromMediaSink instead.

Upvotes: 2

Related Questions