1:讯飞支持的音频文件格式只有pcm和wav(这种格式直接忽略,用ffmpeg库转换其他音频格式成wav格式之后,全部都是噪音),并且音频文件的参数也有要求:采样率为8khz或16khz,采样精度为16bits,声道为单声道
2:先将MP4中的音频用原生的MediaExtractor和MediaMuxer提取出来,代码如下:
private void muxerAudio() {
mediaExtractor = new MediaExtractor();
int audioIndex = -1;
try {
mediaExtractor.setDataSource("/storage/emulated/0/1.mp4");
int trackCount = mediaExtractor.getTrackCount();
for (int i = 0; i < trackCount; i++) {
MediaFormat trackFormat = mediaExtractor.getTrackFormat(i);
if (trackFormat.getString(MediaFormat.KEY_MIME).startsWith("audio/")) {
audioIndex = i;
}
}
mediaExtractor.selectTrack(audioIndex);
MediaFormat trackFormat = mediaExtractor.getTrackFormat(audioIndex);
mediaMuxer = new MediaMuxer("/storage/emulated/0/output_audio.wav", MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);
int writeAudioIndex = mediaMuxer.addTrack(trackFormat);
mediaMuxer.start();
ByteBuffer byteBuffer = ByteBuffer.allocate(500 * 1024);
MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();
long stampTime = 0;
//获取帧之间的间隔时间
{
mediaExtractor.readSampleData(byteBuffer, 0);
if (mediaExtractor.getSampleFlags() == MediaExtractor.SAMPLE_FLAG_SYNC) {
mediaExtractor.advance();
}
mediaExtractor.readSampleData(byteBuffer, 0);
long secondTime = mediaExtractor.getSampleTime();
mediaExtractor.advance();
mediaExtractor.readSampleData(byteBuffer, 0);
long thirdTime = mediaExtractor.getSampleTime();
stampTime = Math.abs(thirdTime - secondTime);
}
mediaExtractor.unselectTrack(audioIndex);
mediaExtractor.selectTrack(audioIndex);
while (true) {
int readSampleSize = mediaExtractor.readSampleData(byteBuffer, 0);
if (readSampleSize < 0) {
break;
}
mediaExtractor.advance();
bufferInfo.size = readSampleSize;
//bufferInfo.flags = mediaExtractor.getSampleFlags();
bufferInfo.flags = MediaCodec.BUFFER_FLAG_END_OF_STREAM;
bufferInfo.offset = 0;
bufferInfo.presentationTimeUs += stampTime;
mediaMuxer.writeSampleData(writeAudioIndex, byteBuffer, bufferInfo);
}
mediaMuxer.stop();
mediaMuxer.release();
mediaExtractor.release();
Log.e("fuck", "finish");
} catch (IOException e) {
e.printStackTrace();
}
}