需要进行简单的音视频编程,如果不是特别数据C/C++,那么JavaCV应该是比较好的选择,下面记录一下使用JavaCV AAC数据内存读写(PipedInputStream管道流)的方法。
使用管道流需要注意的是PipedInputStream和PipedOutputStream要在不同线程。
整体的流程就是:
- FFmpegFrameRecorder将音频数据塞给PipedOutputStream
- 程序从PipedInputStream中读取音频数据
实现代码:
public class Sample06 {
public static void main(String[] args) throws IOException {
FFmpegFrameGrabber grabber = new FFmpegFrameGrabber("E:\\lpl.mp4");
grabber.start();
PipedInputStream is = new PipedInputStream(1024 * 1024);
PipedOutputStream os = new PipedOutputStream(is);
FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(os, grabber.getAudioChannels());
recorder.setFormat("adts");
recorder.start(grabber.getFormatContext());
Thread readThread = new AudioReadThread(is);
readThread.start();
AVPacket packet;
int audioStream = grabber.getAudioStream();
while ((packet = grabber.grabPacket()) != null) {
if (audioStream == packet.stream_index()) {
recorder.recordPacket(packet);
}
}
recorder.close();
grabber.close();
}
}
读取实现跟普通的读InputStream是一样的:
while (!Thread.interrupted()) {
if (is.available() > 0) {
byte[] bytes = new byte[is.available()];
is.read(bytes);
System.out.println("buff.size = " + bytes.length + ", "
+ Integer.toHexString(bytes[0] & 0xFF) + ","
+ Integer.toHexString(bytes[1] & 0xFF) + ","
+ Integer.toHexString(bytes[2] & 0xFF) + ","
+ Integer.toHexString(bytes[3] & 0xFF));
}
}
debug日志:
buff.size = 180, ff,f1,4c,80
buff.size = 176, ff,f1,4c,80
buff.size = 171, ff,f1,4c,80
buff.size = 178, ff,f1,4c,80
buff.size = 173, ff,f1,4c,80
buff.size = 193, ff,f1,4c,80
标签:AAC,PipedInputStream,bytes,音视频,grabber,80,buff,size
From: https://www.cnblogs.com/michong2022/p/17016380.html