Parcourir la source

删除录制视频

buzhanyi il y a 2 ans
Parent
commit
232105fa9f
1 fichiers modifiés avec 30 ajouts et 43 suppressions
  1. 30 43
      hazard-admin/src/main/java/com/ozs/web/core/util/CameraUtil.java

+ 30 - 43
hazard-admin/src/main/java/com/ozs/web/core/util/CameraUtil.java

@@ -18,6 +18,7 @@ import org.springframework.util.ObjectUtils;
 
 import javax.annotation.PostConstruct;
 import java.io.*;
+import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.*;
 import java.util.regex.Matcher;
@@ -381,14 +382,9 @@ public class CameraUtil {
      * @throws IOException
      * @throws InterruptedException
      */
-    public void deleteFlvExceed() throws IOException, InterruptedException {
+    public void deleteFlvExceed() throws IOException, InterruptedException, ParseException {
         //字典中设置的值
         List<SysDictData> sysCameraRecordTime = dictDataMapper.selectDictDataByType("sys_camera_record_time");
-        //开启流
-        List<String> rspList = new ArrayList<String>();
-        Process proc = Runtime.getRuntime().exec("/bin/bash", 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();
@@ -403,51 +399,42 @@ public class CameraUtil {
             calendar.add(Calendar.DAY_OF_YEAR, -45);
         }
         date = calendar.getTime();
-        //时间格式化为字符串
-        String formatD = sdf.format(date);
 
         //遍历每个进行视频录制的摄像头
         File file = new File(transcribeFilePath);
         log.info("file.exists()======================================" + file.exists());
-        if (file.exists()) {
-            String s = HttpUtils.sendGet(bakUrl + transcribeFilePath, null);
-            log.info("s======================================" + s);
-            if (!StringUtils.isBlank(s) || "null".equals(s)) {
-                List<Map<String, Object>> maps = JSON.parseArray(s, Map.class);
-                log.info("maps======================================" + maps);
-                if (ObjectUtils.isEmpty(maps)) {
-                    return;
-                }
-                List<String> commands = new ArrayList<>();
-                for (Map<String, Object> map : maps) {
-                    Object path = map.get("Path");
-                    log.info("path======================================" + path.toString());
-                    //查出符合条件的视频文件,删掉
-                    if (!ObjectUtils.isEmpty(path)) {
-                        String commit = " rm -rf  " + transcribeFilePath + "/" + formatD + "*";
-                        commands.add(commit);
+        if (file.exists() && file.isDirectory()) {
+            //获取文件夹中所有的子文件夹和文件
+            File[] files = file.listFiles();
+            if (!ObjectUtils.isEmpty(files) && files.length > 0) {
+                for (File file1 : files) {
+                    //   file1=/opt/streams/record/flv/34020000001320000167
+                    if (file1.exists() && file1.isDirectory()) {
+                        //相机文件
+                        File[] fs = file1.listFiles();
+                        if (!ObjectUtils.isEmpty(fs) && fs.length > 0) {
+                            for (File f : fs) {
+                                //    f=/opt/streams/record/flv/34020000001320000167/20230328_192033_192037-91509173-cd5a-11ed-8a42-fa163e4e1e9f.flv
+                                //fName=20230328_192033_192037-91509173-cd5a-11ed-8a42-fa163e4e1e9f.flv
+                                String fName = f.getName();
+                                String[] split = fName.split("_");
+                                if (split.length > 0) {
+                                    //20230328
+                                    String s = split[0];
+                                    Date parse = sdf.parse(s);
+                                    long fTime = parse.getTime();
+                                    long dateTime = date.getTime();
+                                    //删除过期文件
+                                    if (fTime < dateTime) {
+                                        f.delete();
+                                    }
+                                }
+                            }
+                        }
                     }
                 }
-                // 删除
-                for (String line : commands) {
-                    out.println(line);
-                    log.info("line======================================" + 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();
     }