我有一个mp4文件,只有一个AAC格式的音轨。我想将此mp4文件转换为PCM格式的音频文件。我可以从mp4文件中成功提取AAC格式的音频文件。但是我无法将AAC格式解码为PCM格式。
以下代码可以正常工作,并将PCM数据保存到文件中。但是此PCM格式的音频无法用任何媒体播放器打开。它说文件已损坏。我试图用ffmpeg将AAC格式解码为PCM格式,而ffmpeg没有问题。但是由于库的大小,我不想使用ffmpeg库。
首先,我尝试将mp4文件路径提供给inputfilePath,然后成功创建并保存了输出文件。但是不会在任何媒体播放器中打开此输出文件。
extractAudioFromVideo(“ ... / input.mp4”,“ ... / output.wav”);
第二,我成功地从mp4文件中提取了AAC音频文件。我尝试将ACC文件路径指定为inputfilePath,但输出中未发生任何变化。
extractAudioFromVideo(“ ... / input.aac”,“ ... / output.wav”);
静态void extractAudioFromVideo(@NonNull字符串inputfilePath,@NonNull字符串outputFilePath){
try
{
OutputStream outputStream = new FileOutputStream(outputFilePath);
MediaExtractor extractor = new MediaExtractor();
extractor.setDataSource(inputfilePath);
MediaCodec mediaCodec = null;
for(int index = 0; index < extractor.getTrackCount(); index++)
{
MediaFormat format = extractor.getTrackFormat(index);
String mime = format.getString(MediaFormat.KEY_MIME);
if(mime.startsWith("audio/"))
{
extractor.selectTrack(index);
mediaCodec = MediaCodec.createDecoderByType(mime);
mediaCodec.configure(format, null, null, 0);
break;
}
}
if (mediaCodec == null)
{
Log.e("Converter", "Can't find audio info");
return;
}
mediaCodec.start();
MediaCodec.BufferInfo info = new MediaCodec.BufferInfo();
ByteBuffer[] inputBuffers = mediaCodec.getInputBuffers();
ByteBuffer[] outputBuffers = mediaCodec.getOutputBuffers();
boolean isEOS = false;
while (true)
{
if (!isEOS)
{
int inputIndex = mediaCodec.dequeueInputBuffer(10000);
if (inputIndex >= 0)
{
ByteBuffer buffer = inputBuffers[inputIndex];
int sampleSize = extractor.readSampleData(buffer, 0);
if (sampleSize < 0)
{
Log.d("Converter", "InputBuffer BUFFER_FLAG_END_OF_STREAM");
mediaCodec.queueInputBuffer(inputIndex, 0, 0, 0, MediaCodec.BUFFER_FLAG_END_OF_STREAM);
isEOS = true;
}
else
{
mediaCodec.queueInputBuffer(inputIndex, 0, sampleSize, extractor.getSampleTime(), 0);
extractor.advance();
}
}
}
int outputIndex = mediaCodec.dequeueOutputBuffer(info, 10000);
switch (outputIndex)
{
case MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED:
Log.d("Converter", "INFO_OUTPUT_BUFFERS_CHANGED");
outputBuffers = mediaCodec.getOutputBuffers();
break;
case MediaCodec.INFO_OUTPUT_FORMAT_CHANGED:
Log.d("Converter", "New format " + mediaCodec.getOutputFormat());
break;
case MediaCodec.INFO_TRY_AGAIN_LATER:
Log.d("Converter", "dequeueOutputBuffer timed out!");
break;
default:
ByteBuffer buffer = outputBuffers[outputIndex];
/*
// I tried this line of code but it gives the same result.
byte[] chunk = new byte[info.size-info.offset];
int position = buffer.position();
buffer.get(chunk);
buffer.position(position);
outputStream.write(chunk,0,info.size-info.offset);
*/
final byte[] chunk = new byte[info.size - info.offset];
buffer.get(chunk);
buffer.clear();
if (chunk.length > 0)
{
outputStream.write(chunk,0,info.size - info.offset);
}
mediaCodec.releaseOutputBuffer(outputIndex, true);
break;
}
if ((info.flags & MediaCodec.BUFFER_FLAG_END_OF_STREAM) != 0)
{
Log.d("Converter", "OutputBuffer BUFFER_FLAG_END_OF_STREAM");
break;
}
}
outputStream.flush();
outputStream.close();
mediaCodec.stop();
mediaCodec.release();
extractor.release();
}
catch (Exception exception)
{
exception.printStackTrace();
}
}
除了将这行代码保存为PCM格式外,我还需要做其他事情吗?还是做错了什么?