-->

Media Foundation get encoded bitrate

2019-09-07 04:40发布

问题:

I am trying to get the encoded bitrate of an audio file (mp4, m4a, aac) using Media Foundation.

What I did is:

PROPVARIANT prop;
IMFSourceReader* reader;

MFCreateSourceReaderFromURL(filePath, NULL, &reader);
reader->GetPresentationAttribute(MF_SOURCE_READER_MEDIASOURCE, MF_PD_AUDIO_ENCODING_BITRATE,
                                                                                     &prop);

The second line ends with an error and with empty PROPVARIAT.

However, when I do:

reader->GetPresentationAttribute(MF_SOURCE_READER_MEDIASOURCE, MF_PD_DURATION, &prop);

It works fine.

Does anyone know what is the issue and/or are there any other approaches to get the encoded bitrate of an audio track?

回答1:

Audio bitrate is a property of a track, not of a media file. Hence, you'd normally want to choose a specific track (yes, typically it's the first audio track even if the file is audio-only single track file) and query its attributes.

Presentation description would get you attributes like this (I list just a few relevant):

  • Key MF_MT_MAJOR_TYPE, vValue MFMediaType_Audio
  • Key MF_MT_SUBTYPE, vValue MFAudioFormat_AAC
  • Key MF_MT_AVG_BITRATE, vValue 125601
  • Key MF_MT_AAC_AUDIO_PROFILE_LEVEL_INDICATION, vValue 0
  • Key MF_MT_AAC_PAYLOAD_TYPE, vValue 0

If you only need an informational value, such as presented by Windows shell:

and you don't need Media Foundation otherwise (that is, just to access the value), you can use shell property handler to do this job for you. You would just request PKEY_Audio_EncodingBitrate property and the handler would leverage Media Foundation to retrieve that for you.