Skip to main content

 none
Encoding PCM audio to an MP4/avi file RRS feed

  • Question

  • Hi,

    I'm trying to encode audio into an mp4/avi file using sinkwriter. 

    I've used the example from /social.msdn.microsoft.com/Forums/windowsdesktop/en-US/55e14d46-053e-4cd5-b069-adfa4cd60207/sinkwriter-and-audio-encoding?forum=mediafoundationdevelopment for this purpose.

    My input audio details are

    const UINT32 SAMPLES_PER_SECOND = 8000;

    const UINT32 NUM_CHANNELS = 1;
    const UINT32 BITS_PER_SAMPLE = 16;
    const UINT32 BLOCK_ALIGNMENT = BITS_PER_SAMPLE / 8 * NUM_CHANNELS;
    const UINT32 ONE_SECOND = 10; // number of buffers per second  
    const UINT32 AVG_BYTES_PER_SECOND = BITS_PER_SAMPLE / 8 * NUM_CHANNELS * SAMPLES_PER_SECOND;

    hr = MFCreateSinkWriterFromURL(L"outputPCM.avi", NULL, NULL, &pSinkWriter);
    ////audio stream settings
    CHECK_HR(MFCreateMediaType(&spMFTypeOut));
    CHECK_HR(spMFTypeOut->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Audio));
    CHECK_HR(spMFTypeOut->SetGUID(MF_MT_SUBTYPE, MFAudioFormat_MPEG));
    CHECK_HR(spMFTypeOut->SetUINT32(MF_MT_AUDIO_BITS_PER_SAMPLE, BITS_PER_SAMPLE));
    UINT32 samplePerSec = 32000;
    CHECK_HR(spMFTypeOut->SetUINT32(MF_MT_AUDIO_SAMPLES_PER_SECOND, SAMPLES_PER_SECOND));
    CHECK_HR(spMFTypeOut->SetUINT32(MF_MT_AUDIO_NUM_CHANNELS, NUM_CHANNELS));
        CHECK_HR(spMFTypeOut->SetUINT32(MF_MT_AUDIO_AVG_BYTES_PER_SECOND, AVG_BYTES_PER_SECOND));

    CHECK_HR(spMFTypeOut->SetUINT32(MF_MT_AUDIO_BLOCK_ALIGNMENT, BLOCK_ALIGNMENT));
    CHECK_HR(pSinkWriter->AddStream(spMFTypeOut, &sindex));

    CHECK_HR(MFCreateMediaType(&spMFTypeIn));
    CHECK_HR(spMFTypeIn->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Audio));
    CHECK_HR(spMFTypeIn->SetGUID(MF_MT_SUBTYPE, MFAudioFormat_PCM));
    CHECK_HR(spMFTypeIn->SetUINT32(MF_MT_AUDIO_BITS_PER_SAMPLE, BITS_PER_SAMPLE));
    CHECK_HR(spMFTypeIn->SetUINT32(MF_MT_AUDIO_SAMPLES_PER_SECOND, SAMPLES_PER_SECOND));
    CHECK_HR(spMFTypeIn->SetUINT32(MF_MT_AUDIO_NUM_CHANNELS, NUM_CHANNELS));
    CHECK_HR(spMFTypeIn->SetUINT32(MF_MT_AUDIO_BLOCK_ALIGNMENT, BLOCK_ALIGNMENT));
    CHECK_HR(spMFTypeIn->SetUINT32(MF_MT_AUDIO_AVG_BYTES_PER_SECOND, AVG_BYTES_PER_SECOND));

    CHECK_HR(pSinkWriter->SetInputMediaType(sindex, spMFTypeIn, NULL)); <-Fails here


    I've tried MFAudioFormat_MPEG with 8000 but it fails with error "The data specified for the media type is invalid, inconsistent, or not supported by this object"

    I'm not able to find an encoder that would take a sample rate of 8000.

    Could anyone please help me with this?

    Regards,

    Shyamala

    Tuesday, November 19, 2019 7:16 AM

All replies

  • You indeed provide invalid data. There is no encoder in system to produce MFAudioFormat_MPEG as you request. Do you have any specific reason to create AVI in first place? There are good reasons to not do this, and create MP4 instead. For MP4 you can find suitable sample code, and for audio you should use AAC media type, details for attributes for which you can find here.

    http://alax.info/blog/tag/directshow

    Tuesday, November 19, 2019 9:29 AM
  • Hi Roman, 

    I've already tried to do the same with mp4, with AAC encoder and Mp3 encoder all of them are returning the same error.

    With mp4 and AAC, if I set the sample rate to 48000, it encodes the audio.

    The problem happens only when I try to set the sample rate to 8000 (which is what I need to work on my solution). I was just trying out different types of encoders and file formats to see if any of them work.

    Is there anything else I should check?

    Regards,

    Shyamala

    Tuesday, November 19, 2019 10:19 AM
  • Stock AAC encoder offers support for 44100 and 48000. I guess you can still feed 8000 PCM input if you are okay with it being converted to 48000 AAC, but you cannot get 8000 AAC.

    http://alax.info/blog/tag/directshow

    Tuesday, November 19, 2019 1:10 PM