Domanda

Esiste un modo per ri-codificare un file WAV PCM su un altro codifica utilizzando Android SDK standard?

Vedo che è possibile registrare direttamente dal microfono in questi formati, ma l'app che sto scrivendo deve prima registrare in PCM. A causa delle restrizioni alle licenze, FFMPEG non è un'opzione disponibile.


Ora ho il seguente codice per Jelly Bean, ma l'output non è leggibile da nessun giocatore di media.

Il codice spaventato da Aosp sembra suggerire un contenitore 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;

Ma l'output del codice Android non è leggibile.

Il file WAV di input è 32kHz, 16 bit Mono come richiesto.

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();
}
È stato utile?

Soluzione

Fondamentalmente, stai scrivendo tutti i buffer codificati generati in un file, ma che mancano ogni sorta di Metadati relativi a tracce/campioni/file ecc.

Immagino che le soluzioni a cui riesco a pensare sia: o puoi trovare un bene Muxer Libreria per scrivere i buffer nel giusto formato di file, oppure devi aspettare e vedere se il futuro Android fornirà tali API per te.

Altri suggerimenti

Per rispondere alla mia domanda,

In Android 4.3 e sopra c'è la classe Mediamuxer che crea contenitori MPEG4 per il flusso audio e video.

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;

}

Scegli un contenitore per questo. Preferisco anche gli ADT, ma anche FLV/MP4 funziona.

Copia i dati del payload in un aray abbastanza grande per il tuo contenitore, basta aggiungere i tuoi bit. Quindi, dopo aver cercato Internet per la mia soluzione, ho lavorato uno snippet in posizione

    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;

Usando qualcosa di simile a quello sotto, questo chiama quanto sopra

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

Il file non può essere riprodotto perché non contiene informazioni di intestazione. Un modo semplice (ISH) per rendere questo giocabile è quello di aggiungere intestazioni ADTS ai telai AAC grezzi. Puoi trovare una descrizione qui.

Buona fortuna!

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top