电脑端FFmpeg直接操作PCM
提取PCM数据
ffmpeg -i out.mp4 -vn -ar 44100 -ac 2 -f s16le out.pcm
播放PCM
ffplay -ar 44100 -ac 2 -f s16le -i out.pcm
PCM转WAV
ffmpeg -f s16be -ar 8000 -ac 2 -acodec pcm_s16be -i input.raw output.wav
解码播放音频
package com.zhangyu.myopengl.testMediaCodec;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.media.MediaCodec;
import android.media.MediaExtractor;
import android.media.MediaFormat;
import android.text.TextUtils;
import android.util.Log;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
public class MCDecoderAudio {
private static final String TAG = "MCDecoder";
private MediaExtractor mediaExtractor;
private MediaCodec mediaCodec;
private AudioTrack audioTrack;
private String srcPath;
private boolean isFinish = false;
private String dstPath;
public void setDstPath(String dstPath) {
this.dstPath = dstPath;
}
public void decodeAudio(String srcPath) {
this.srcPath = srcPath;
initMediaCodec();
initAudioTrack();
new Thread(new Runnable() {
@Override
public void run() {
decodeAndPlay();
}
}).start();
}
private void decodeAndPlay() {
OutputStream os = null;
if (!TextUtils.isEmpty(dstPath)){
try {
os = new FileOutputStream(dstPath);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();
ByteBuffer inputBuffer;
while (!isFinish && mediaCodec != null) {
long TIME_OUT_US = 10 * 1000;
int inputIndex = mediaCodec.dequeueInputBuffer(TIME_OUT_US);
if (inputIndex < 0) {
break;
}
inputBuffer = mediaCodec.getInputBuffer(inputIndex);
if (inputBuffer != null) {
inputBuffer.clear();
}else {
continue;
}
int sampleSize = mediaExtractor.readSampleData(inputBuffer, 0);
if (sampleSize > 0) {
mediaCodec.queueInputBuffer(inputIndex, 0, sampleSize, 0, 0);
mediaExtractor.advance();
} else {
break;
}
int outputIndex = mediaCodec.dequeueOutputBuffer(bufferInfo, TIME_OUT_US);
ByteBuffer outputBuffer;
byte[] pcmData;
while (outputIndex >= 0) {
outputBuffer = mediaCodec.getOutputBuffer(outputIndex);
pcmData = new byte[bufferInfo.size];
if (outputBuffer != null) {
outputBuffer.get(pcmData);
outputBuffer.clear();
}
audioTrack.write(pcmData, 0, bufferInfo.size);
if (os != null) {
try {
os.write(pcmData);
} catch (IOException e) {
e.printStackTrace();
}
}
mediaCodec.releaseOutputBuffer(outputIndex, false);
outputIndex = mediaCodec.dequeueOutputBuffer(bufferInfo, TIME_OUT_US);
}
}
if (mediaCodec != null) {
mediaCodec.stop();
mediaCodec.release();
mediaCodec = null;
Log.e(TAG, "stopPlay");
}
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void initMediaCodec() {
try {
mediaExtractor = new MediaExtractor();
mediaExtractor.setDataSource(srcPath);
int audioTrackIndex = -1;
String mime = null;
MediaFormat trackFormat = null;
for (int i = 0; i < mediaExtractor.getTrackCount(); i++) {
trackFormat = mediaExtractor.getTrackFormat(i);
mime = trackFormat.getString(MediaFormat.KEY_MIME);
if (!TextUtils.isEmpty(mime) && mime.startsWith("audio")) {
audioTrackIndex = i;
Log.d(TAG, "找到音频流的索引为:" + audioTrackIndex);
break;
}
}
if (audioTrackIndex == -1) {
Log.e(TAG, "initAudioDecoder: 没有找到音频流");
return;
}
mediaExtractor.selectTrack(audioTrackIndex);
mediaCodec = MediaCodec.createDecoderByType(mime);
mediaCodec.configure(trackFormat, null, null, 0);
mediaCodec.start();
} catch (IOException e) {
e.printStackTrace();
}
}
private void initAudioTrack() {
int streamType = AudioManager.STREAM_MUSIC;
int sampleRate = 44100;
int channelConfig = AudioFormat.CHANNEL_OUT_STEREO;
int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
int mode = AudioTrack.MODE_STREAM;
int minBufferSize = AudioTrack.getMinBufferSize(sampleRate, channelConfig, audioFormat);
audioTrack = new AudioTrack(streamType, sampleRate, channelConfig, audioFormat,
Math.max(minBufferSize, 2048), mode);
audioTrack.play();
}
public void onDestroy() {
isFinish = true;
}
}