Browse Source

删除录制视频

buzhanyi 1 year ago
parent
commit
61cf6b9fb6

+ 6 - 0
hazard-admin/src/main/java/com/ozs/web/core/config/CaneraConfig.java

@@ -25,6 +25,12 @@ public class CaneraConfig {
     @Value("${base.filePath:/opt/streams/}")
     private String filePath;
 
+    /**
+     * 录制视频存放路径
+     */
+    @Value("${base.transcribeFilePath:/opt/streams/record/flv/}")
+    private String transcribeFilePath;
+
 
     @Value("${base.webUrl:http://47.106.159.135/streams}")
     private String webUrl;

+ 78 - 1
hazard-admin/src/main/java/com/ozs/web/core/util/CameraUtil.java

@@ -4,9 +4,11 @@ package com.ozs.web.core.util;
 import com.alibaba.fastjson2.JSON;
 import com.ozs.common.config.BaseConfig;
 import com.ozs.common.constant.Constants;
+import com.ozs.common.core.domain.entity.SysDictData;
 import com.ozs.common.exception.base.BaseException;
 import com.ozs.common.utils.DateUtils;
 import com.ozs.common.utils.http.HttpUtils;
+import com.ozs.system.mapper.SysDictDataMapper;
 import com.ozs.web.core.config.CaneraConfig;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
@@ -32,11 +34,14 @@ public class CameraUtil {
     private static String historyUrl;
     private static String ffmpegPath;
     private static String filePath;
+    private static String transcribeFilePath;
     private static String webUrl;
     private static String bakUrl;
 
     @Autowired
     private CaneraConfig caneraConfig;
+    @Autowired
+    private SysDictDataMapper dictDataMapper;
 
 
     /**
@@ -211,8 +216,9 @@ public class CameraUtil {
 
     /**
      * 合并视频(转化文件)
+     *
      * @param fromVideoFileList 视频路径
-     * @param newfilePath 生产新的视频文件路径
+     * @param newfilePath       生产新的视频文件路径
      * @throws IOException
      */
     public static void myConvetor(List<String> fromVideoFileList,
@@ -320,6 +326,7 @@ public class CameraUtil {
         historyUrl = caneraConfig.getHistoryUrl();
         ffmpegPath = caneraConfig.getFfmpegPath();
         filePath = caneraConfig.getFilePath();
+        transcribeFilePath = caneraConfig.getTranscribeFilePath();
         webUrl = caneraConfig.getWebUrl();
         bakUrl = caneraConfig.getBakUrl();
     }
@@ -368,4 +375,74 @@ public class CameraUtil {
     }
 
 
+    /**
+     * 定时任务:删除超过配置时长的录制视频
+     *
+     * @throws IOException
+     * @throws InterruptedException
+     */
+    public void deleteFlvExceed() throws IOException, InterruptedException {
+        //字典中设置的值
+        List<SysDictData> sysCameraRecordTime = dictDataMapper.selectDictDataByType("sys_camera_record_time");
+        //开启流
+        List<String> rspList = new ArrayList<String>();
+        Process proc = Runtime.getRuntime().exec(transcribeFilePath, null, null);
+        BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
+        PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(proc.getOutputStream())), true);
+
+        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
+        Date date = new Date();
+        Calendar calendar = new GregorianCalendar();
+        calendar.setTime(date);
+        //正数,日期天数加
+        //负数,日期天数减                         提前45天
+        if (!ObjectUtils.isEmpty(sysCameraRecordTime)) {
+            String dictValue = sysCameraRecordTime.get(0).getDictValue();
+            calendar.add(Calendar.DAY_OF_YEAR, Integer.parseInt("-" + dictValue));
+        } else {
+            calendar.add(Calendar.DAY_OF_YEAR, -45);
+        }
+        date = calendar.getTime();
+        //时间格式化为字符串
+        String formatD = sdf.format(date);
+        //遍历每个进行视频录制的摄像头
+        File file = new File(transcribeFilePath);
+        if (file.exists()) {
+            String s = HttpUtils.sendGet(transcribeFilePath, null);
+            if (!StringUtils.isBlank(s) || "null".equals(s)) {
+                List<Map<String, Object>> maps = JSON.parseArray(s, Map.class);
+                if (ObjectUtils.isEmpty(maps)) {
+                    return;
+                }
+                List<String> commands = new ArrayList<>();
+                for (Map<String, Object> map : maps) {
+                    Object path = map.get("Path");
+                    //查出符合条件的视频文件,删掉
+                    if (!ObjectUtils.isEmpty(path)) {
+                        String commit = " rm -rf  " + transcribeFilePath + formatD + "*";
+                        commands.add(commit);
+                    }
+                }
+                // 删除
+                for (String line : commands) {
+                    out.println(line);
+                }
+            }
+        }
+        // 这个命令必须执行,否则in流不结束。
+        out.println("exit");
+        String rspLine = "";
+        while ((rspLine = in.readLine()) != null) {
+            System.out.println(rspLine);
+            rspList.add(rspLine);
+        }
+        int i = proc.waitFor();
+        log.info("执行结果:{}", i);
+        //关闭流
+        in.close();
+        out.close();
+        proc.destroy();
+    }
+
+
 }