Search code examples
androidaudioandroid-mediacodecmediaextractor

For some particular video's audio I am getting half channel count and half sample rate with mediaExtractor and mediaFormat


For some particular video's audio, I am getting half channel count and half sample rate with android mediaExtractor and mediaFormats (Ex: Channel count is 2 and sample rate is 44100 but I am getting channel count 1 and sample rate 22050). For other videos, it is working fine. One thing I noticed is that for "aac profile = 29" it causing the problem.

The code I am using

   MediaExtractor extractor = new MediaExtractor();



        try {
            extractor.setDataSource( path);

            int trackIndex = MetadataUtils.getTrackIndex(extractor, "audio/");
            if (trackIndex != -1) {
                MediaFormat format = extractor.getTrackFormat(trackIndex);

                audioMetadata.mAudioTrackIndex = trackIndex;
                audioMetadata.mAudioTrackFormat = format;

                if (format != null) {

                    if (format.containsKey(MediaFormat.KEY_MIME)) {
                        audioMetadata.mAudioMimeType = format.getString(MediaFormat.KEY_MIME);
                        MediaFormat.KEY_AAC_PROFILE
                    }

                    if (format.containsKey(MediaFormat.KEY_CHANNEL_COUNT)) {
                        audioMetadata.mNoInputChannel = format.getInteger(MediaFormat.KEY_CHANNEL_COUNT);
                    }
                    
                    if (format.containsKey(MediaFormat.KEY_SAMPLE_RATE)) {
                        audioMetadata.mInputSampleRateHz = format.getInteger(MediaFormat.KEY_SAMPLE_RATE);
                    }

                    if (format.containsKey(MediaFormat.KEY_DURATION)) {
                        audioMetadata.mAudioDurationUs = format.getLong(MediaFormat.KEY_DURATION);
                    }
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            extractor.release();
        }

How can i fix this ?


Solution

  • When KEY_AAC_PROFILE is 29, it means AACObjectHE_PS or HE AAC v2. When KEY_AAC_PROFILE is 5, it means AACObjectHE or HE AAC

    In HE AAC, SBR(Spectral Band Replication) is used, so the actual sample rate will be doubled. In, HE AAC v2, both SBR(Spectral Band Replication) and PS(Parametric Stereo) are used, so the actual sample rate and channel count will be doubled.

    For more info, refer following links

    https://tech.ebu.ch/docs/techreview/trev_305-moser.pdf https://datatracker.ietf.org/doc/html/rfc6416

    Another Solution : For this, you have to decode the audio file using mediaCodec, in onOutputFormatChanged(codec: MediaCodec, format: MediaFormat) callback, you will have accurate channelCount and sampleRate in the format .