2023-02-16 15:28:15 +08:00
|
|
|
|
package com.zhgd.xmgl.util;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @program: wisdomSite
|
|
|
|
|
|
* @description: 视频处理工具
|
|
|
|
|
|
* @author: Mr.Peng
|
|
|
|
|
|
* @create: 2021-11-19 10:50
|
|
|
|
|
|
**/
|
|
|
|
|
|
|
|
|
|
|
|
public class VideoUtils {
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
|
|
|
|
public static boolean getVideoOnlineSate(String videoUrl){
|
|
|
|
|
|
boolean flag=false;
|
|
|
|
|
|
try {
|
|
|
|
|
|
if(StringUtils.isNotEmpty(videoUrl)) {
|
|
|
|
|
|
FFmpegFrameGrabber grabber = FFmpegFrameGrabber.createDefault(videoUrl);
|
|
|
|
|
|
// 使用tcp的方式,不然会丢包很严重
|
|
|
|
|
|
grabber.setOption("rtsp_transport", "tcp");
|
|
|
|
|
|
grabber.setImageWidth(960);
|
|
|
|
|
|
grabber.setImageHeight(540);
|
|
|
|
|
|
log.info("grabber start");
|
|
|
|
|
|
grabber.start();
|
|
|
|
|
|
int index = 0;
|
|
|
|
|
|
while (true) {
|
|
|
|
|
|
Frame frame = grabber.grabImage();
|
|
|
|
|
|
if (frame != null) {
|
|
|
|
|
|
flag = true;
|
|
|
|
|
|
grabber.stop();
|
|
|
|
|
|
grabber.release();
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
index++;
|
|
|
|
|
|
if (index >= 20) {
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}catch (Exception e){
|
2024-04-14 21:05:01 +08:00
|
|
|
|
log.error("error:",e);
|
|
|
|
|
|
}
|
2023-02-16 15:28:15 +08:00
|
|
|
|
return flag;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static void playVideo(String url){
|
|
|
|
|
|
try {
|
|
|
|
|
|
//String url = "rtsp://182.101.141.23:554/openUrl/M4Hh77a";
|
|
|
|
|
|
FFmpegFrameGrabber grabber = FFmpegFrameGrabber.createDefault(url);
|
|
|
|
|
|
// 使用tcp的方式,不然会丢包很严重
|
|
|
|
|
|
grabber.setOption("rtsp_transport", "tcp");
|
|
|
|
|
|
grabber.setImageWidth(960);
|
|
|
|
|
|
grabber.setImageHeight(540);
|
|
|
|
|
|
log.info("grabber start");
|
|
|
|
|
|
grabber.start();
|
|
|
|
|
|
|
|
|
|
|
|
//1.播放视频
|
|
|
|
|
|
CanvasFrame canvasFrame = new CanvasFrame("摄像机");
|
|
|
|
|
|
canvasFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
|
|
|
|
canvasFrame.setAlwaysOnTop(true);
|
|
|
|
|
|
OpenCVFrameConverter.ToMat converter = new OpenCVFrameConverter.ToMat();
|
|
|
|
|
|
while (true){
|
|
|
|
|
|
Frame frame = grabber.grabImage();
|
|
|
|
|
|
opencv_core.Mat mat = converter.convertToMat(frame);
|
|
|
|
|
|
canvasFrame.showImage(frame);
|
|
|
|
|
|
}
|
|
|
|
|
|
}catch (Exception e){
|
2024-04-14 21:05:01 +08:00
|
|
|
|
log.error("error:",e);
|
|
|
|
|
|
}
|
2023-02-16 15:28:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static BufferedImage getVideoBufferedImage(String url){
|
|
|
|
|
|
BufferedImage bufferedImage=null;
|
|
|
|
|
|
try {
|
|
|
|
|
|
//String url = "rtsp://182.101.141.23:554/openUrl/M4Hh77a";
|
|
|
|
|
|
FFmpegFrameGrabber grabber = FFmpegFrameGrabber.createDefault(url);
|
|
|
|
|
|
// 使用tcp的方式,不然会丢包很严重
|
|
|
|
|
|
grabber.setOption("rtsp_transport", "tcp");
|
|
|
|
|
|
grabber.setImageWidth(960);
|
|
|
|
|
|
grabber.setImageHeight(540);
|
|
|
|
|
|
log.info("grabber start");
|
|
|
|
|
|
grabber.start();
|
|
|
|
|
|
int count=0;
|
|
|
|
|
|
//2.帧截图
|
|
|
|
|
|
while (true){
|
|
|
|
|
|
Frame frame = grabber.grabImage();
|
|
|
|
|
|
if(count>=50) {
|
|
|
|
|
|
if (frame != null) {
|
|
|
|
|
|
bufferedImage = FrameToBufferedImage(frame);
|
|
|
|
|
|
grabber.stop();
|
|
|
|
|
|
grabber.release();
|
|
|
|
|
|
log.info("图片已保存");
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
count++;
|
|
|
|
|
|
}
|
|
|
|
|
|
}catch (Exception e){
|
2024-04-14 21:05:01 +08:00
|
|
|
|
log.error("error:",e);
|
|
|
|
|
|
}
|
2023-02-16 15:28:15 +08:00
|
|
|
|
return bufferedImage;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//创建BufferedImage对象
|
|
|
|
|
|
public static BufferedImage FrameToBufferedImage(Frame frame) {
|
|
|
|
|
|
Java2DFrameConverter converter = new Java2DFrameConverter();
|
|
|
|
|
|
BufferedImage bufferedImage = converter.getBufferedImage(frame);
|
|
|
|
|
|
// bufferedImage=rotateClockwise90(bufferedImage);
|
|
|
|
|
|
return bufferedImage;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 处理图片,将图片旋转90度
|
|
|
|
|
|
public static BufferedImage rotateClockwise90(BufferedImage bi) {
|
|
|
|
|
|
int width = bi.getWidth();
|
|
|
|
|
|
int height = bi.getHeight();
|
|
|
|
|
|
BufferedImage bufferedImage = new BufferedImage(height, width, bi.getType());
|
|
|
|
|
|
for (int i = 0; i < width; i++) {
|
|
|
|
|
|
for (int j = 0; j < height; j++) {
|
|
|
|
|
|
bufferedImage.setRGB(j, i, bi.getRGB(i, j));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return bufferedImage;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
|
playVideo("rtsp://182.101.141.23:554/openUrl/EPwbuOA");
|
|
|
|
|
|
log.info("----");
|
|
|
|
|
|
}
|
|
|
|
|
|
**/
|
|
|
|
|
|
}
|