Java PCM byte[]转AAC 示例代码
时间: 2023-09-13 22:07:56 浏览: 126
以下是Java PCM byte[]转AAC的示例代码:
```
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.UnsupportedAudioFileException;
import org.bytedeco.javacpp.avcodec;
import org.bytedeco.javacpp.avutil;
import org.bytedeco.javacpp.opus;
import org.bytedeco.javacpp.presets.opus;
import org.bytedeco.javacv.FFmpegFrameRecorder;
import org.bytedeco.javacv.FrameRecorder.Exception;
import org.bytedeco.javacv.Java2DFrameConverter;
public class AudioConverter {
private static ByteArrayOutputStream audioOutputStream = new ByteArrayOutputStream();
private static final int OPUS_SAMPLE_RATE = 48000;
private static final int OPUS_CHANNEL_COUNT = 2;
private static final int OPUS_FRAME_SIZE = 1280;
private static final int RECORDING_DURATION = 60000;
private static final int RECORDING_START_DELAY = 2000;
public static void main(String args[]) throws Exception {
AudioInputStream audioInputStream = null;
try {
audioInputStream = AudioSystem.getAudioInputStream(AudioConverter.class.getResourceAsStream("/test.wav"));
} catch (UnsupportedAudioFileException e) {
System.out.println("Unsupported file format");
} catch (IOException e) {
System.out.println("I/O Exception");
}
AudioFormat sourceFormat = audioInputStream.getFormat();
AudioFormat opusFormat = new AudioFormat(opus.OPUS_SAMPLE_RATE, opus.OPUS_CHANNEL_COUNT, opus.AvSampleFormat.AV_SAMPLE_FMT_S16.getValue(), true, false);
byte[] audioBytes = null;
int numBytes = 0;
byte[] buffer = new byte[4096];
try {
while ((numBytes = audioInputStream.read(buffer)) != -1) {
audioOutputStream.write(buffer, 0, numBytes);
}
audioBytes = audioOutputStream.toByteArray();
} catch (IOException e) {
e.printStackTrace();
}
Java2DFrameConverter java2dConverter = new Java2DFrameConverter();
FFmpegFrameRecorder frameRecorder = new FFmpegFrameRecorder("test.aac", OPUS_SAMPLE_RATE);
frameRecorder.setAudioChannels(OPUS_CHANNEL_COUNT);
frameRecorder.setAudioOption("ac", "2");
frameRecorder.setAudioOption("ar", Integer.toString(OPUS_SAMPLE_RATE));
frameRecorder.setAudioOption("sample_fmt", "s16p");
frameRecorder.setAudioOption("preset", "ultrafast");
frameRecorder.setAudioOption("tune", "zerolatency");
frameRecorder.setAudioOption("compression_level", "10");
try {
frameRecorder.start();
frameRecorder.setTimestamp(0);
long startTime = System.currentTimeMillis() + RECORDING_START_DELAY;
long endTime = startTime + RECORDING_DURATION;
int startTimeIndex = (int) ((RECORDING_START_DELAY / 1000) * sourceFormat.getFrameRate()) * sourceFormat.getFrameSize();
int endTimeIndex = (int) ((RECORDING_DURATION / 1000) * sourceFormat.getFrameRate()) * sourceFormat.getFrameSize();
for (int i = startTimeIndex; i < endTimeIndex; i += sourceFormat.getFrameSize()) {
byte[] opusBuffer = new byte[OPUS_FRAME_SIZE * OPUS_CHANNEL_COUNT * 2];
int numBytesRead = 0;
for (int j = 0; j < OPUS_FRAME_SIZE; j++) {
if ((i + j * sourceFormat.getFrameSize()) < audioBytes.length) {
opusBuffer[numBytesRead++] = audioBytes[i + j * sourceFormat.getFrameSize()];
opusBuffer[numBytesRead++] = audioBytes[i + j * sourceFormat.getFrameSize() + 1];
}
}
frameRecorder.recordSamples(avutil.avSampleFormatValue(opusFormat.getSampleSizeInBits()), opusBuffer, OPUS_CHANNEL_COUNT);
long currentTimestamp = System.currentTimeMillis() - startTime;
frameRecorder.setTimestamp(currentTimestamp);
Thread.sleep(Math.max(0, 20 - currentTimestamp));
}
} catch (org.bytedeco.javacv.FrameRecorder.Exception e) {
System.out.println("Could not start recording");
} catch (InterruptedException e) {
System.out.println("Current thread interrupted");
} finally {
try {
frameRecorder.stop();
} catch (org.bytedeco.javacv.FrameRecorder.Exception e) {
System.out.println("Could not stop recording");
}
}
}
}
```
阅读全文