有没有办法使用标准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,16位符号需要单声道。
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();
}
答案 0 :(得分:3)
回答我自己的问题,
在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;
}
答案 1 :(得分:1)
基本上,您将所有生成的编码缓冲区写入文件,但缺少与track / samples / file等相关的各种 MetaData 。
我想我能想到的解决方案是:要么你能找到一个好的 muxer 库来将缓冲区编写成正确的文件格式,要么你必须等待,看看未来的Android是否会为您提供此类API。
答案 2 :(得分:1)
为它选择一个容器。我也喜欢adts,但flv / mp4也适用。
将有效负载数据复制到足够容纳您容器的aray中,只需添加您的位。因此,在搜索了我的解决方案之后,我将一些片段放到了适当位置
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);
答案 3 :(得分:0)
无法播放该文件,因为它不包含任何标题信息。使这个可玩的简单(ish)方法是将ADTS头添加到原始AAC帧。您可以找到说明here。
祝你好运!