簡體   English   中英

在沒有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();

正如您已經指出的那樣,對於這種工作, 沒有與舊版Android兼容公共系統API

無論如何,您可以使用本機編碼器 (如FFMPEG )來尋求自定義解決方案。 我可以建議你以下: timsu / android-aac-enc

Android AAC編碼器項目

使用漂亮的Java API提取Android Stagefright VO AAC編碼器。

該項目為底層JNI編碼器提供了一個簡單的Java API ,它應該可以使用,因為本機庫已經針對通用ARM體系結構進行了編譯 (請注意,我還沒有測試過它)。 整個圖書館只有500kb,所以它不會給你的APK增加太多。

要進行快速測試,請在項目中導入以下部分:

  1. 本機AAC編碼器的Java綁定
  2. 預編譯的AAC編碼器.so庫
  3. 用法示例 (語音編碼)

您應該能夠輕松地將示例調整到您的代碼中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM