无需 MediaMuxer 即可将 PCM 转换为 AAC 或 MP4 文件

Convert PCM to AAC or MP4 file without MediaMuxer

我需要将 PCM 文件转换为 AAC 或 MP4 文件。到目前为止,我是用 MediaCodecMediaMuxer 做到的,但是 Android 4.3 支持 MediaMuxer。有没有不使用 MediaMuxer 进行转换的方法?

我的代码是这样的:

MediaMuxer mux = null;
try {
    File inputFile = new File(filePath + ".pcm");
    FileInputStream fis = new FileInputStream(inputFile);
    mux = new MediaMuxer(filePath + ".mp4", MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);

    MediaFormat outputFormat = MediaFormat.createAudioFormat(COMPRESSED_AUDIO_FILE_MIME_TYPE,
            SAMPLING_RATE, 1);
    outputFormat.setInteger(MediaFormat.KEY_AAC_PROFILE, MediaCodecInfo.CodecProfileLevel.AACObjectLC);
    outputFormat.setInteger(MediaFormat.KEY_BIT_RATE, COMPRESSED_AUDIO_FILE_BIT_RATE);

    MediaCodec codec = MediaCodec.createEncoderByType(COMPRESSED_AUDIO_FILE_MIME_TYPE);
    codec.configure(outputFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
    codec.start();

    ByteBuffer[] codecInputBuffers = codec.getInputBuffers();
    ByteBuffer[] codecOutputBuffers = codec.getOutputBuffers();

    MediaCodec.BufferInfo outBuffInfo = new MediaCodec.BufferInfo();

    byte[] tempBuffer = new byte[BUFFER_SIZE];
    boolean hasMoreData = true;
    double presentationTimeUs = 0;
    int audioTrackIdx = 0;
    int totalBytesRead = 0;
    int percentComplete;

    do {

        int inputBufIndex = 0;
        while (inputBufIndex != -1 && hasMoreData) {
            inputBufIndex = codec.dequeueInputBuffer(CODEC_TIMEOUT_IN_MS);

            if (inputBufIndex >= 0) {
                ByteBuffer dstBuf = codecInputBuffers[inputBufIndex];
                dstBuf.clear();

                int bytesRead = fis.read(tempBuffer, 0, dstBuf.limit());
                if (bytesRead == -1) { // -1 implies EOS
                    hasMoreData = false;
                    codec.queueInputBuffer(inputBufIndex, 0, 0, (long) presentationTimeUs, MediaCodec.BUFFER_FLAG_END_OF_STREAM);
                } else {
                    totalBytesRead += bytesRead;
                    dstBuf.put(tempBuffer, 0, bytesRead);
                    codec.queueInputBuffer(inputBufIndex, 0, bytesRead, (long) presentationTimeUs, 0);
                    presentationTimeUs = 1000000l * (totalBytesRead / 2) / SAMPLING_RATE;
                }
            }
        }

        // Drain audio
        int outputBufIndex = 0;
        while (outputBufIndex != MediaCodec.INFO_TRY_AGAIN_LATER) {

            outputBufIndex = codec.dequeueOutputBuffer(outBuffInfo, CODEC_TIMEOUT_IN_MS);
            if (outputBufIndex >= 0) {
                ByteBuffer encodedData = codecOutputBuffers[outputBufIndex];
                encodedData.position(outBuffInfo.offset);
                encodedData.limit(outBuffInfo.offset + outBuffInfo.size);

                if ((outBuffInfo.flags & MediaCodec.BUFFER_FLAG_CODEC_CONFIG) != 0 && outBuffInfo.size != 0) {
                    codec.releaseOutputBuffer(outputBufIndex, false);
                } else {

                    mux.writeSampleData(audioTrackIdx, codecOutputBuffers[outputBufIndex], outBuffInfo);
                    codec.releaseOutputBuffer(outputBufIndex, false);
                }
            } else if (outputBufIndex == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) {
                outputFormat = codec.getOutputFormat();
                Log.v("AUDIO", "Output format changed - " + outputFormat);
                audioTrackIdx = mux.addTrack(outputFormat);
                mux.start();
            } else if (outputBufIndex == MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED) {
                Log.e("AUDIO", "Output buffers changed during encode!");
            } else if (outputBufIndex != MediaCodec.INFO_TRY_AGAIN_LATER){
                Log.e("AUDIO", "Unknown return code from dequeueOutputBuffer - " + outputBufIndex);
            }
        }
        percentComplete = (int) Math.round(((float) totalBytesRead / (float) inputFile.length()) * 100.0);
        Log.v("AUDIO", "Conversion % - " + percentComplete);
    } while (outBuffInfo.flags != MediaCodec.BUFFER_FLAG_END_OF_STREAM);

    fis.close();
    mux.stop();
    mux.release();

正如您已经指出的那样,没有 public 系统 API 与旧版本的 Android 兼容用于此类工作。

无论如何,您可以寻求自定义解决方案使用本机编码器(例如FFMPEG)。我可以向您提出以下建议:timsu/android-aac-enc

Android AAC 编码器项目

Extraction of Android Stagefright VO AAC encoder with a nice Java API.

该项目为底层 JNI 编码器 提供了 easy Java API,从 开始就可以使用了本机库已针对通用 ARM 架构 编译(请注意,我尚未对其进行测试)。整个库只有 500kb,因此不会使您的 APK 变胖。

为了进行快速测试,请在您的项目中导入以下部分:

  1. Java bindings 原生 AAC 编码器
  2. 预编译AAC encoder .so library
  3. Example 用法(语音编码)

您应该能够轻松地使示例适应您的代码。