簡體   English   中英

將Wav文件重新編碼為AAC LC,AMR WB / NB

[英]Re-encoding Wav file to AAC LC, AMR WB/NB

有什么方法可以使用標准的Android SDK將PCM wav文件重新編碼為另一種編碼?

我可以看到可以直接從麥克風錄制成這些格式,但我編寫的應用程序必須先以PCM錄制。 由於許可限制,ffmpeg不是可用選項。


我現在有以下Jelly bean代碼,但任何媒體播放器都無法讀取輸出。

AOSP的舞台驚嚇代碼似乎暗示了mpeg4容器

profile.nSampleRate = sampleRate;
profile.nBitRate = bitRate;
profile.nAudioBandWidth = 0;
profile.nFrameLength = 0;
profile.nAACtools = OMX_AUDIO_AACToolAll;
profile.nAACERtools = OMX_AUDIO_AACERNone;
profile.eAACProfile = (OMX_AUDIO_AACPROFILETYPE) aacProfile;
profile.eAACStreamFormat = OMX_AUDIO_AACStreamFormatMP4FF;

但是android代碼的輸出不可讀。

輸入的wav文件是32khz,16bit帶符號單聲道(根據需要)。

public void doConvert( View v)
{
    new AsyncTask<Void, Void, Void>()
    {

        @Override
        protected Void doInBackground(Void... params) 
        {
            try
            {
                int codecCount = MediaCodecList.getCodecCount();

                for ( int i=0; i < codecCount; i++)
                {
                    MediaCodecInfo info = MediaCodecList.getCodecInfoAt(i);
                    Logger.getLogger(MainActivity.class.getSimpleName()).log(Level.INFO, info.getName());
                    for ( String type : info.getSupportedTypes() )
                    {
                        Logger.getLogger(MainActivity.class.getSimpleName()).log(Level.INFO, type);
                    }

                }

                File inputFile = new File( Environment.getExternalStorageDirectory().getAbsolutePath()+"/Media/Report-test5.wav");
                FileInputStream fis = new FileInputStream(inputFile);
                fis.skip(44);//remove wav header

                File outputFile = new File( Environment.getExternalStorageDirectory().getAbsolutePath()+"/Media/out.mp4");
                if ( outputFile.exists()) outputFile.delete();

                FileOutputStream fos = new FileOutputStream(outputFile);

                MediaCodec codec = MediaCodec.createEncoderByType("audio/mp4a-latm");

                MediaFormat outputFormat = MediaFormat.createAudioFormat("audio/mp4a-latm", 32000, 1);
                outputFormat.setInteger(MediaFormat.KEY_AAC_PROFILE, MediaCodecInfo.CodecProfileLevel.AACObjectLC);
                //outputFormat.setInteger(MediaFormat.KEY_CHANNEL_MASK, AudioFormat.CHANNEL_OUT_MONO);
                outputFormat.setInteger(MediaFormat.KEY_BIT_RATE, 48000 );
                //outputFormat.setInteger(MediaFormat.KEY_MAX_INPUT_SIZE, 64000);
                double durationInMs = (inputFile.length()/64.0)*1000.0;

                outputFormat.setLong(MediaFormat.KEY_DURATION, (long)durationInMs );
                //Logger.getLogger(MainActivity.class.getSimpleName()).log(Level.INFO, codec.getOutputFormat().toString());

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

                ByteBuffer[] inputBuffers = codec.getInputBuffers();
                ByteBuffer[] outputBuffer = codec.getOutputBuffers();

                boolean hasMoreData = true;
                MediaCodec.BufferInfo outBuffInfo = new BufferInfo();
                byte readBuffer[] = new byte[64000];
                byte writeBuffer[] = new byte[64000];

                do
                {
                    int nextBuffer = codec.dequeueInputBuffer(1000);
                    logger.log(Level.INFO,"nextInputBuffer = "+nextBuffer);

                    if ( nextBuffer >= 0 )
                    {



                        ByteBuffer inBuf = inputBuffers[nextBuffer];
                        inBuf.clear();
                        int bytesRead = fis.read( readBuffer,0, inBuf.capacity() );
                        logger.log(Level.INFO,"Read = "+bytesRead);

                        if ( bytesRead < inBuf.capacity() )
                        {
                            hasMoreData = false;
                        }

                        inBuf.put(readBuffer, 0, bytesRead );

                        codec.queueInputBuffer(nextBuffer, 0, bytesRead, 0, hasMoreData?0:MediaCodec.BUFFER_FLAG_END_OF_STREAM);
                    }


                    int outputBufferIndex = codec.dequeueOutputBuffer( outBuffInfo, 1000 );
                    logger.log(Level.INFO,"nextOutputBuffer = "+outputBufferIndex);
                    logger.log(Level.INFO,"outBuffInfo offset = "+outBuffInfo.offset);
                    logger.log(Level.INFO,"outBuffInfo size = "+outBuffInfo.size);
                    logger.log(Level.INFO,"outBuffInfo flags = "+outBuffInfo.flags);


                    //while ( outputBufferIndex > -1 )
                    //{ 

                        outputBuffer[outputBufferIndex].position(outBuffInfo.offset);
                        outputBuffer[outputBufferIndex].get(writeBuffer,0,outBuffInfo.size);

                        fos.write(writeBuffer,0, outBuffInfo.size);
                        logger.log(Level.INFO,"Writing = "+outBuffInfo.size+" bytes");


                        outputBuffer[outputBufferIndex].clear();

                        codec.releaseOutputBuffer(outputBufferIndex, false);

                        if ( outBuffInfo.flags == MediaCodec.BUFFER_FLAG_END_OF_STREAM )
                        {
                            codec.flush();
                            codec.stop();
                            codec.release();
                            break;
                        }

                        //outputBufferIndex = codec.dequeueOutputBuffer( outBuffInfo, 1000 );
                        //logger.log(Level.INFO,"nextOutputBuffer = "+outputBufferIndex);
                    //}

                } while ( outBuffInfo.flags != MediaCodec.BUFFER_FLAG_END_OF_STREAM);

                fis.close();
                fos.flush();
                fos.close();



            }
            catch ( Exception e)
            {
                Logger.getLogger(MainActivity.class.getSimpleName()).log(Level.INFO, "Codec Error",e);
            }

            logger.log(Level.INFO,"Done");

            return null;
        }

    }.execute();
}

為了回答我自己的問題,

在Android 4.3及更高版本中,存在MediaMuxer類,該類創建用於音頻和視頻流的MPEG4容器。

public boolean encode( File outputFile )

{

if ( Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2 )
{
    try
    {
        mInputWav.close();
    }
    catch (IOException e)
    {
        logger.log(Level.WARNING,"Unable to close Input Wav File ",e);
    }

    throw new UnsupportedOperationException("Only Available on Android 4.3 and Above");
}

try
{

    int sampleRate = mInputWav.getSampleRate();
    int percentComplete = 0;
    int fileSize = mInputWav.getDataLength();
    int totalBytesRead = 0;

    if ( outputFile.exists()) outputFile.delete();

    MediaMuxer mux = new MediaMuxer( outputFile.getAbsolutePath(), OutputFormat.MUXER_OUTPUT_MPEG_4);

    MediaCodec codec = MediaCodec.createEncoderByType("audio/mp4a-latm");

    MediaFormat outputFormat = MediaFormat.createAudioFormat("audio/mp4a-latm",sampleRate,1);
    outputFormat.setInteger(MediaFormat.KEY_AAC_PROFILE, MediaCodecInfo.CodecProfileLevel.AACObjectLC);
    outputFormat.setInteger(MediaFormat.KEY_BIT_RATE, 128000 );

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

    ByteBuffer[] inputBuffers = codec.getInputBuffers();
    ByteBuffer[] outputBuffer = codec.getOutputBuffers();

    boolean hasMoreData = true;
    MediaCodec.BufferInfo outBuffInfo = new BufferInfo();
    byte readBuffer[] = new byte[64000];
    double presentationTimeUs=0;
    int audioTrackIdx=0;

    do
    {

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

            if ( nextBuffer >= 0 )
            {
                ByteBuffer inBuf = inputBuffers[nextBuffer];
                inBuf.clear();
                int bytesRead = mInputWav.read( readBuffer,0, inBuf.limit() );

                if ( bytesRead == -1 )
                {
                    hasMoreData = false;                                
                    codec.queueInputBuffer(nextBuffer, 0, 0, (long)presentationTimeUs, MediaCodec.BUFFER_FLAG_END_OF_STREAM);
                }
                else
                {

                    totalBytesRead += bytesRead;                            
                    inBuf.put(readBuffer, 0, bytesRead );

                    codec.queueInputBuffer(nextBuffer, 0, bytesRead, (long)presentationTimeUs, 0);

                    presentationTimeUs  = 1000000l * (totalBytesRead) / 2 / sampleRate;
                }
            }

        }


        //Drain audio
        int outputBufferIndex = 0;
        while (outputBufferIndex != MediaCodec.INFO_TRY_AGAIN_LATER )
        {
            outputBufferIndex = codec.dequeueOutputBuffer( outBuffInfo, 10000 );
            if ( outputBufferIndex >= 0 )
            {

                ByteBuffer encodedData = outputBuffer[outputBufferIndex];
                encodedData.position(outBuffInfo.offset);
                encodedData.limit(outBuffInfo.offset + outBuffInfo.size );

                if ((outBuffInfo.flags & MediaCodec.BUFFER_FLAG_CODEC_CONFIG)!= 0 && outBuffInfo.size != 0) 
                {
                    logger.log(Level.FINE, "video encoder: codec config buffer");
                    // Simply ignore codec config buffers.
                    codec.releaseOutputBuffer(outputBufferIndex, false);

                }
                else
                {
                    mux.writeSampleData(audioTrackIdx, outputBuffer[outputBufferIndex], outBuffInfo);
                    codec.releaseOutputBuffer(outputBufferIndex, false);
                }

            }
            else if ( outputBufferIndex == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED )
            {
                logger.info("Output Format Changed :"+codec.getOutputFormat().toString());
                outputFormat = codec.getOutputFormat();
                audioTrackIdx = mux.addTrack(outputFormat);
                mux.start();

            }
            else if (outputBufferIndex == MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED )
            {
                logger.info("Output Buffers Changed: shouldn't happen on an encode ");
            }
            else if ( outputBufferIndex == MediaCodec.INFO_TRY_AGAIN_LATER)
            {
                logger.info("Encoder Timed Out");
            }
            else
            {
                logger.info("Unkown return code from dequeueOutputBuffer "+outputBufferIndex);
            }
        }

        percentComplete = (int)Math.round(((float)totalBytesRead / (float)fileSize)*100.0);
        logger.info("Percent Complete "+percentComplete+"%");

        if ( mListener != null )
        {
            mListener.onProgressUpdate(percentComplete);
        }

    } while ( outBuffInfo.flags != MediaCodec.BUFFER_FLAG_END_OF_STREAM && !mCanceled);

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

    logger.info("Finished");



}
catch ( Exception e)
{
    logger.log(Level.INFO, "Codec Error",e);
}


logger.log(Level.INFO,"Done");

return true;

}

基本上,您會將所有生成的編碼緩沖區寫入一個文件,但是缺少與音軌/樣本/文件等有關的各種元數據

我想我可以想到的解決方案是:您可以找到一個好的muxer庫以將緩沖區寫入正確的文件格式,或者必須等待,看看未來的Android是否會為您提供此類API。

選擇一個容器。 我也更喜歡adts,但是flv / mp4也可以。

將有效載荷數據復制到足以容納您的容器的托盤中,只需添加一些位即可。 因此,在為我的解決方案搜索互聯網之后,我編寫了一些代碼片段

    profile =( configParams[0]>>3 )&0x1f;

    frequency_index = (this.configParams[0]&0x7) <<1 | (this.configParams[1]>>7) &0x1;

    channel_config = (this.configParams[1]>>3) &0xf;

    int finallength = encoded_length + 7;       
    ENCodedByteArray[0] = (byte) 0xff;
    ENCodedByteArray[1] = (byte) 0xf1;
    ENCodedByteArray[2] = (byte) ( ((profile - 1) << 6) + (frequency_index << 2) +(channel_config >> 2));
    ENCodedByteArray[3] = (byte) (((channel_config & 0x3) << 6) + (finallength >> 11));
    ENCodedByteArray[4] = (byte)( (finallength & 0x7ff) >> 3);
    ENCodedByteArray[5] = (byte) (((finallength & 7) << 5) + 0x1f) ;
    ENCodedByteArray[6] = (byte) 0xfc;

使用類似下面的內容,調用上面的內容

            byte chunkADTS[]=new byte[info.size + 7];
            fillInADTSHeader(chunkADTS,info.size);
            outputBuffers[bR].get(chunkADTS,7,info.size);
            buffer.pushData(chunkADTS);

該文件不包含任何標題信息,因此無法播放。 使此可播放的一種簡單方法是將ADTS標頭添加到原始AAC幀。 您可以在此處找到說明。

祝好運!

暫無
暫無

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

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