我正在尝试通过http从DVR读取多台摄像机,并且当我仅选择一个摄像机流时,我的代码可以正常工作。
我正在使用JavaCV library。
Java2DFrameConverter conv = new Java2DFrameConverter();
HttpURLConnection con = (HttpURLConnection) new URL("127.0.0.1").openConnection();
con.setRequestMethod("GET");
InputStream is = con.getInputStream();
FFmpegFrameGrabber g = new FFmpegFrameGrabber(is);
g.start();
g.setFormat("h264");
int read = 0;
int fileNum = 0;
while (true)
{
BufferedImage img = conv.convert(g.grabFrame());
ImageIO.write(img, "png", new File("out" + (fileNum++) + ".png"));
}
使用与上面类似的代码,我知道可以正常工作,并且可以正确解码流,我尝试写出两个摄像机的流,只是交替抓取帧并保存它们,但是,似乎无法正确解码,并且有很多“损坏? ”如下图所示,以及原始的h264二进制流。
h264 stream of 2 cameras with "corruption?"
h264 binary stream of 2 cameras
ffmpeg报告以下流信息
Input #0, h264, from 'out.h264':
Duration: N/A, bitrate: N/A
Stream #0:0: Video: h264 (Baseline), yuv420p(progressive), 720x288, 25 fps, 25 tbr, 1200k tbn, 50 tbc
Java2DFrameConverter conv = new Java2DFrameConverter();
HttpURLConnection con = (HttpURLConnection) new URL("127.0.0.1").openConnection();
con.setRequestMethod("GET");
InputStream is = con.getInputStream();
FFmpegFrameGrabber g = new FFmpegFrameGrabber(is);
g.setFormat("h264");
g.start();
int read = 0;
int[] fileNum = new int[2];
int cameras = 2;
int camera = 0;
while (true)
{
BufferedImage img = conv.convert(g.grabFrame());
ImageIO.write(img, "png", new File("out" + (camera) + (fileNum[camera]++) + ".png"));
camera++;
if(camera >= cameras) camera = 0;
}
这是流数据的问题还是我在解码流时缺少东西?