需要进行简单的音视频编程,如果不是特别数据C/C++,那么JavaCV应该是比较好的选择,下面记录一下使用JavaCV H264数据内存读写(PipedInputStream管道流)的方法。
使用管道流需要注意的是PipedInputStream和PipedOutputStream要在不同线程。
整体的流程就是:
- FFmpegFrameRecorder将视频数据塞给PipedOutputStream
- 程序从PipedInputStream中读取视频数据
实现代码:
public class Sample05 {
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.getImageWidth(), grabber.getImageHeight());
recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);
recorder.setFormat("h264");
recorder.start();
Thread readThread = new VideoReadThread(is);
readThread.start();
Frame frame;
while ((frame = grabber.grab()) != null) {
if (frame.image != null) {
recorder.record(frame);
}
}
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 + ", " + bytes[0] + "," + bytes[1] + "," + bytes[2] + "," + bytes[3]);
}
}
debug日志:
buff.size = 797, 0,0,0,1
buff.size = 274, 0,0,0,1
buff.size = 248, 0,0,0,1
buff.size = 609, 0,0,0,1
buff.size = 308, 0,0,0,1
可以看到,读取的H264帧,是包含NALU起始码的。
标签:H264,PipedInputStream,bytes,音视频,grabber,new,recorder,size From: https://www.cnblogs.com/michong2022/p/17016324.html