Quellcode durchsuchen

`添加视频文件转换、事件防抖、事件消散`

wenhongquan vor 2 Monaten
Ursprung
Commit
fa895299d7

+ 6 - 6
ruoyi-admin/pom.xml

@@ -108,11 +108,11 @@
         </dependency>
 
         <!--视频多媒体工具包 包含 FFmpeg、OpenCV-->
-        <dependency>
-            <groupId>org.bytedeco</groupId>
-            <artifactId>javacv-platform</artifactId>
-            <version>1.5.10</version>
-        </dependency>
+<!--        <dependency>-->
+<!--            <groupId>org.bytedeco</groupId>-->
+<!--            <artifactId>javacv-platform</artifactId>-->
+<!--            <version>1.5.10</version>-->
+<!--        </dependency>-->
         <!-- skywalking 整合 logback -->
 <!--        <dependency>-->
 <!--            <groupId>org.apache.skywalking</groupId>-->
@@ -164,7 +164,7 @@
                 <configuration>
                     <!--from节点用来设置镜像的基础镜像,相当于Docerkfile中的FROM关键字-->
                     <from>
-                        <image>docker.xt.wenhq.top:8083/docker_r/javarun17:v1.0</image>
+                        <image>docker.xt.wenhq.top:8083/docker_r/java17ffmpeg:1.0</image>
                     </from>
                     <to>
                         <!--镜像名称和tag,使用了mvn内置变量${project.version},表示当前工程的version-->

+ 2 - 0
ruoyi-admin/src/main/java/org/dromara/DromaraApplication.java

@@ -3,6 +3,7 @@ package org.dromara;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.boot.context.metrics.buffering.BufferingApplicationStartup;
+import org.springframework.scheduling.annotation.EnableScheduling;
 
 /**
  * 启动程序
@@ -11,6 +12,7 @@ import org.springframework.boot.context.metrics.buffering.BufferingApplicationSt
  */
 
 @SpringBootApplication
+@EnableScheduling
 public class DromaraApplication {
 
     public static void main(String[] args) {

+ 112 - 0
ruoyi-admin/src/main/java/org/dromara/web/task/KafkaMessageConsumer.java

@@ -2,28 +2,109 @@ package org.dromara.web.task;
 
 import cn.hutool.core.date.DateUtil;
 import cn.hutool.core.io.FileUtil;
+import cn.hutool.core.lang.UUID;
 import cn.hutool.core.thread.ThreadUtil;
 import cn.hutool.core.util.StrUtil;
 import cn.hutool.http.HttpUtil;
+import cn.hutool.json.JSON;
 import cn.hutool.json.JSONArray;
 import cn.hutool.json.JSONObject;
 import cn.hutool.json.JSONUtil;
 import org.apache.kafka.clients.consumer.ConsumerRecord;
 import org.dromara.common.core.service.OssService;
 import org.dromara.common.core.utils.SpringUtils;
+import org.dromara.system.domain.bo.TblDeviceBo;
 import org.dromara.system.domain.bo.TblEventBo;
 import org.dromara.system.domain.vo.SysOssUploadVo;
 import org.dromara.system.domain.vo.SysOssVo;
+import org.dromara.system.domain.vo.TblDeviceVo;
+import org.dromara.system.domain.vo.TblEventVo;
+import org.dromara.system.service.ISysConfigService;
 import org.dromara.system.service.ISysOssService;
+import org.dromara.system.service.ITblDeviceService;
 import org.dromara.system.service.ITblEventService;
+import org.dromara.system.utils.Utils;
+import org.slf4j.Logger;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Component;
 
 import java.io.File;
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Map;
 
+@Component
 public class KafkaMessageConsumer {
+    private static final Logger log = org.slf4j.LoggerFactory.getLogger(KafkaMessageConsumer.class);
+    private Map<String, Long> eventMap = new java.util.HashMap<>();
+    private Map<String, Long> eventSaveMap = new java.util.HashMap<>();
+
+    private String finalEventTime = "200";
+    private String finalEventChangeTime = "400";
+
+
+    @Scheduled(cron = "*/1 * * * * *")
+    public void doChange() {
+        System.out.println("事件判断");
+        try{
+            eventMap.forEach((key, value) -> {
+                if(DateUtil.current()-value>Integer.parseInt(finalEventChangeTime)){
+                    //改变状态
+                    ThreadUtil.execAsync(() -> {
+                        //先查找状态
+                        TblEventVo tblEventVo = SpringUtils.getBean(ITblEventService.class).queryById(eventSaveMap.get(key));
+                        if(tblEventVo != null && tblEventVo.getStatus().equals("2")){
+                            TblEventBo tblEventBo = new TblEventBo();
+                            tblEventBo.setId(eventSaveMap.get(key));
+                            tblEventBo.setStatus("4");
+                            SpringUtils.getBean(ITblEventService.class).updateByBo(tblEventBo);
+                        }
+                    });
+                }
+            });
+
+        } catch (Exception e) {
+
+        }
+
+
+    }
+
+
+
+
+
+
     public void consumerMessage(List<ConsumerRecord<String, Object>> message) {
         System.out.println("收到消息:" + message);
+
+        String eventTime = SpringUtils.getBean(ISysConfigService.class).selectConfigByKey("event_time");
+        if(StrUtil.isEmptyIfStr(eventTime)){
+            eventTime = "200";
+        }
+        finalEventTime = eventTime;
+        String eventChangeTime = SpringUtils.getBean(ISysConfigService.class).selectConfigByKey("event_change_time");
+        if(StrUtil.isEmptyIfStr(eventChangeTime)){
+            eventChangeTime = "400";
+        }
+        finalEventChangeTime = eventChangeTime;
+
+        //查询摄像头
+        List<TblDeviceVo> listdev = SpringUtils.getBean(ITblDeviceService.class).queryList(new TblDeviceBo());
+        Map<String, TblDeviceVo> map = new java.util.HashMap<>();
+        listdev.forEach(item ->{
+            if(item.getExt1()!=null){
+                try{
+                    JSONObject ysObject = JSONUtil.parseObj(item.getExt1());
+                    if(ysObject.get("name")!=null){
+                        map.put(ysObject.getStr("name"), item);
+                    }
+                } catch (Exception e) {
+
+                }
+            }
+        });
         message.forEach(consumerRecord ->{
 
             if(consumerRecord.topic().equals("zs_events_r2p3")){
@@ -36,6 +117,14 @@ public class KafkaMessageConsumer {
                                 JSONArray envlist = item.getJSONArray("Evt_List");
                                 for (int j = 0; j < envlist.size(); j++) {
                                     JSONObject envitem = envlist.getJSONObject(j);
+                                    String key = envitem.getInt("EvtType")+msg.getStr("DevNo")+"源驶科技";
+                                    if(eventMap.get(key)!=null){
+                                        if(DateUtil.current()-eventMap.get(key)<Integer.parseInt(finalEventTime)){
+                                            continue;
+                                        }
+                                    }
+                                    eventMap.put(key,DateUtil.current());
+
                                     envitem.set("from","源驶科技");
                                     envitem.set("lx", getEventName(envitem.getInt("EvtType")));
                                     TblEventBo tblEventBo = new TblEventBo();
@@ -70,7 +159,15 @@ public class KafkaMessageConsumer {
                                     }
                                     tblEventBo.setExt1(JSONUtil.toJsonStr(urls));
                                     tblEventBo.setAddr(msg.getStr("DevNo"));
+                                    //根据DevNo 查询设备名称
+                                    TblDeviceVo deviceVo = map.get(msg.getStr("DevNo"));
+                                    if(deviceVo!=null){
+                                        tblEventBo.setAddr(deviceVo.getName());
+                                        envitem.set("deviceVo", deviceVo);
+                                        tblEventBo.setExt2(JSONUtil.toJsonStr(envitem));
+                                    }
                                     SpringUtils.getBean(ITblEventService.class).insertByBo(tblEventBo);
+                                    eventSaveMap.put(key,tblEventBo.getId());
                                 }
                             }
 
@@ -92,9 +189,24 @@ public class KafkaMessageConsumer {
             FileUtil.mkdir("./temp/");
         }
         File file = HttpUtil.downloadFileFromUrl(path, FileUtil.file("./temp/"));
+        try{
+            String pathfile = "./"+ UUID.fastUUID()+".mp4";
+            Utils.exchangeToMp4("/Users/wenhongquan/miniforge3/bin/ffmpeg",file.toPath().toString(),pathfile);
+            FileUtil.del(file);
+            file =  new File(pathfile);;
+        }catch (Exception e){
+
+        }
         SysOssVo oss = SpringUtils.getBean(ISysOssService.class).upload(file);
+        FileUtil.del(file);
         return oss.getFileName();
     }
+//    public static void main(String[] args) {
+//        KafkaMessageConsumer kafkaMessageConsumer = new KafkaMessageConsumer();
+//        kafkaMessageConsumer.downloadImage("http://jtjai.xt.wenhq.top:8083/api/oss/local/upload/image/2025/06/13/1.mp4");
+//    }
+//
+
 
 
 

+ 4 - 1
ruoyi-admin/src/main/java/org/dromara/web/task/SysKafkaData.java

@@ -24,6 +24,9 @@ public class SysKafkaData {
     private final KafkaUtils kafkaUtils;
 
     @Autowired
+    private KafkaMessageConsumer kafkaMessageConsumer;
+
+    @Autowired
     public SysKafkaData(KafkaUtils kafkaUtils) {
         this.kafkaUtils = kafkaUtils;
     }
@@ -35,7 +38,7 @@ public class SysKafkaData {
         try {
             if(isEnable){
                 //创建监听容器
-                kafkaUtils.registerListenerContainer(listenerID, "test-consumer-group1", new KafkaMessageConsumer(), KafkaMessageConsumer.class.getDeclaredMethod("consumerMessage", List.class), topic);
+                kafkaUtils.registerListenerContainer(listenerID, "test-consumer-group1", kafkaMessageConsumer, KafkaMessageConsumer.class.getDeclaredMethod("consumerMessage", List.class), topic);
                 kafkaUtils.setStateNormalListenerContainer(listenerID);
 
 

+ 2 - 0
ruoyi-admin/src/main/resources/application.yml

@@ -47,6 +47,7 @@ user:
   fileUrl: http://jtjai.xt.wenhq.top:8083/api/oss/local/upload/
   difyUrl: ${difiurl:http://172.16.5.83/v1}
   difykey: ${difykey:app-bam2jWvQRd16MnfFaaGWK0Lj}
+  ffmpegpath: ${ffmpegpath:/usr/local/bin/ffmpeg}
   password:
     # 密码最大错误次数
     maxRetryCount: 5
@@ -123,6 +124,7 @@ security:
     - /oss/local/**
     - /resource/oss/**
     - /system/event/**
+    - /system/device/**
 
 # 多租户配置
 tenant:

+ 103 - 0
ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/utils/Utils.java

@@ -0,0 +1,103 @@
+package org.dromara.system.utils;
+
+
+import javax.imageio.ImageIO;
+import java.awt.image.BufferedImage;
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+public class Utils {
+
+
+
+
+    public static List<Integer> random(int baseNum, int length) {
+        List<Integer> list = new ArrayList<>(length);
+        while (list.size() < length) {
+            Integer next = (int) (Math.random() * baseNum);
+            if (list.contains(next)) {
+                continue;
+            }
+            list.add(next);
+        }
+        Collections.sort(list);
+        return list;
+    }
+    public static  boolean exchangeToMp4(String ffmpegPath, String upFilePath, String codcFilePath) throws Exception {
+        // 创建List集合来保存转换视频文件为flv格式的命令
+        List<String> convert = new ArrayList<String>();
+        convert.add(ffmpegPath); // 添加转换工具路径
+        convert.add("-y"); // 该参数指定将覆盖已存在的文件
+        convert.add("-i");
+        convert.add(upFilePath);
+        convert.add("-c:v");
+        convert.add("libx264");
+        convert.add("-c:a");
+        convert.add("aac");
+        convert.add("-strict");
+        convert.add("-2");
+        convert.add("-pix_fmt");
+        convert.add("yuv420p");
+        convert.add("-movflags");
+        convert.add("faststart");
+        // convert.add("-vf"); // 添加水印
+        // convert.add("movie=watermark.gif[wm];[in][wm]overlay=20:20[out]");
+        convert.add(codcFilePath);
+
+        boolean mark = true;
+
+        try {
+            Process videoProcess = new ProcessBuilder(convert).redirectErrorStream(true).start();
+            new PrintStream(videoProcess.getInputStream()).start();
+            videoProcess.waitFor(); // 加上这句,系统会等待转换完成。不加,就会在服务器后台自行转换。
+
+        } catch (Exception e) {
+            mark = false;
+            System.out.println(e);
+            e.printStackTrace();
+        }
+        return mark;
+    }
+
+    public static class PrintStream extends Thread {
+        java.io.InputStream __is = null;
+
+        public PrintStream(java.io.InputStream is) {
+            __is = is;
+        }
+
+        public void run() {
+            try {
+                while (this != null) {
+                    int _ch = __is.read();
+                    if (_ch != -1)
+                        System.out.print((char) _ch);
+                    else
+                        break;
+                }
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
+        }
+    }
+
+//    public static void main(String[] args) {
+//        try {
+//                Utils.cropMp4("/Users/wenhongquan/Downloads/aliyunmedia.mp4", 0, 22,"/Users/wenhongquan/Desktop/tmp/aa.mp4");
+//                Utils.GrabberFistFFmpegImage("/Users/wenhongquan/Desktop/tmp/aa.mp4","/Users/wenhongquan/Desktop/tmp/","aa");
+//                Utils.GrabberFistFFmpegImage("/Users/wenhongquan/Downloads/aliyunmedia.mp4","/Users/wenhongquan/Desktop/tmp/","pp",15);
+
+//            Utils sUtils = new Utils();
+
+//            sUtils.exchangeToMp4("/Users/wenhongquan/miniforge3/bin/ffmpeg", "/Users/wenhongquan/Downloads/demo/target/classes/temp/1.mp4", "/Users/wenhongquan/Downloads/demo/target/classes/temp/3.mp4");
+//			Utils.ConvertVideo("/Users/wenhongquan/Downloads/demo/target/classes/temp/1.mp4");
+
+//        } catch (Exception e) {
+//
+//        }
+//    }
+
+}