hexiao 2 роки тому
батько
коміт
2ad1801c72

+ 107 - 53
hazard-admin/src/main/java/com/ozs/web/core/util/CameraUtil.java

@@ -86,15 +86,16 @@ public class CameraUtil {
             @Override
             public void run() {
                 try {
-                    Map<String, String> map = myConvetor(fromVideoFileList, NewfilePath, uuid);
-                    if (!ObjectUtils.isEmpty(map)) {
-                        cUtil.cmd(map.get("cmd"));
-                        //删除生成的ts文件
-                        File file1 = new File(map.get("path"));
-                        if (file1.exists()) {
-                            file1.delete();
-                        }
-                    }
+//                    Map<String, String> map = myConvetor(fromVideoFileList, NewfilePath, uuid);
+//                    if (!ObjectUtils.isEmpty(map)) {
+//                        cUtil.cmd(map.get("cmd"));
+//                        //删除生成的ts文件
+//                        File file1 = new File(map.get("path"));
+//                        if (file1.exists()) {
+//                            file1.delete();
+//                        }
+//                    }
+                    txConvetor(fromVideoFileList, NewfilePath, uuid);
                 } catch (IOException e) {
                     log.error(e.getMessage());
                     e.printStackTrace();
@@ -359,6 +360,57 @@ public class CameraUtil {
     }
 
 
+    public static void txConvetor(List<String> fromVideoFileList,
+                                  String newfilePath,
+                                  String uuid) throws IOException {
+        /*
+        * ffmpeg -i 20230411_155847_155947-d4c2265d-d83e-11ed-8e7f-fa163e4e1e9f.flv -c:v copy 1.ts
+ffmpeg -i 20230411_155948_160048-f91fea03-d83e-11ed-8e7f-fa163e4e1e9f.flv -c:v copy 2.ts
+ffmpeg -i "concat:1.ts|2.ts" -c copy output.flv
+        * */
+
+        File file = new File(newfilePath);
+        boolean flay = false;
+        if (!file.getParentFile().exists()) {
+            file.getParentFile().mkdirs();
+            log.info("创建文件夹:{}", file.getParentFile().getPath());
+            flay = true;
+        }
+        log.info("newfilePath:{}", newfilePath);
+        StringBuffer sm = new StringBuffer(ffmpegPath + "-i \"concat: ");
+        List<String> fileTs = new ArrayList<>();
+        for (int t = 0; t < fromVideoFileList.size(); t++) {
+            File ft = new File(fromVideoFileList.get(t));
+            if (ft.exists()) {
+                // ffmpeg -i 20230411_155847_155947-d4c2265d-d83e-11ed-8e7f-fa163e4e1e9f.flv -c:v copy 1.ts
+                // ffmpeg -i 20230411_155948_160048-f91fea03-d83e-11ed-8e7f-fa163e4e1e9f.flv -c:v copy 2.ts
+                log.info("file:{}", fromVideoFileList.get(t));
+                String substring = fromVideoFileList.get(t).substring(0, fromVideoFileList.get(t).lastIndexOf("."));
+                String cmdstr = ffmpegPath + " -i " + fromVideoFileList.get(t) + " -c:v copy " + substring + ".ts";
+                log.info("转换命令:{}", cmdstr);
+                cUtil.cmd(cmdstr);
+                fileTs.add(substring + ".ts");
+                if (t != fromVideoFileList.size() - 1) {
+                    sm.append(substring + ".ts|");
+                } else {
+                    sm.append(substring + ".ts \" ");
+                }
+            }
+        }
+
+        if (fileTs.size() > 0) {
+            sm.append("-c copy " + newfilePath);
+            log.info("合并命令:{}", sm.toString());
+            cUtil.cmd(sm.toString());
+        }
+        for (String fileT : fileTs) {
+            File ft = new File(fileT);
+            if (ft.exists()) {
+                ft.delete();
+            }
+        }
+    }
+
     @PostConstruct
     public void init() {
         historyUrl = caneraConfig.getHistoryUrl();
@@ -479,6 +531,51 @@ public class CameraUtil {
         }
     }
 
+    public static void main(String[] args) throws InterruptedException, ParseException, IOException {
+//        CameraUtil cameraUtil = new CameraUtil();
+//        cameraUtil.closeRecording();
+        String s = "/opt/streams/record/flv/42010001541320001116/20230403_235506_000506-22e1523b-d170-11ed-8a42-fa163e4e1e9f.flv";
+        String substring = s.substring(0, s.lastIndexOf("."));
+        System.out.println(substring);
+//        String fileStr = s.substring(s.lastIndexOf("/") + 1, s.length());
+//        String pathStr = s.substring(0, s.lastIndexOf("/"));
+//        System.out.println(fileStr);
+//        System.out.println(pathStr);
+
+//        String s = "20230403235512";
+//        Date sdate = DateUtils.dateTime(DateUtils.YYYYMMDDHHMMSS, s);
+//        sdate = DateUtils.addDays(sdate, -1);
+//        System.out.println(DateUtils.parseDateToStr(DateUtils.YYYYMMDDHHMMSS, sdate));
+    }
+
+    public static File[] getCurFilesList(String filePath) {
+        File path = new File(filePath);
+        File[] listFiles = path.listFiles(new java.io.FileFilter() {
+
+            @Override
+            public boolean accept(File pathname) {
+                if (pathname.isFile()) {
+                    return true;
+                } else {
+
+                    return false;
+                }
+            }
+        });
+
+        return listFiles;
+    }
+
+    public static void execute(String command) {
+        try {
+            ProcessBuilder process = new ProcessBuilder(command);
+            process.inheritIO().start().waitFor();
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+
+    }
+
     /**
      * 定时任务:关闭录制视频
      *
@@ -548,7 +645,7 @@ public class CameraUtil {
                                 }
                                 baseCameraManagementService.updateById(baseCameraManagement);
                             }
-                        }else {
+                        } else {
                             LambdaQueryWrapper<BaseCameraManagement> lw = new LambdaQueryWrapper<BaseCameraManagement>();
                             lw.eq(BaseCameraManagement::getCameraCode, substring);
                             BaseCameraManagement baseCameraManagement = baseCameraManagementService.getOne(lw);
@@ -570,47 +667,4 @@ public class CameraUtil {
             }
         }
     }
-
-    public static File[] getCurFilesList(String filePath) {
-        File path = new File(filePath);
-        File[] listFiles = path.listFiles(new java.io.FileFilter() {
-
-            @Override
-            public boolean accept(File pathname) {
-                if (pathname.isFile()) {
-                    return true;
-                } else {
-
-                    return false;
-                }
-            }
-        });
-
-        return listFiles;
-    }
-
-    public static void execute(String command) {
-        try {
-            ProcessBuilder process = new ProcessBuilder(command);
-            process.inheritIO().start().waitFor();
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
-
-    }
-
-    public static void main(String[] args) throws InterruptedException, ParseException, IOException {
-//        CameraUtil cameraUtil = new CameraUtil();
-//        cameraUtil.closeRecording();
-//        String s = "/opt/streams/record/flv/42010001541320001116/20230403_235506_000506-22e1523b-d170-11ed-8a42-fa163e4e1e9f.flv";
-//        String fileStr = s.substring(s.lastIndexOf("/") + 1, s.length());
-//        String pathStr = s.substring(0, s.lastIndexOf("/"));
-//        System.out.println(fileStr);
-//        System.out.println(pathStr);
-
-        String s = "20230403235512";
-        Date sdate = DateUtils.dateTime(DateUtils.YYYYMMDDHHMMSS, s);
-        sdate = DateUtils.addDays(sdate, -1);
-        System.out.println(DateUtils.parseDateToStr(DateUtils.YYYYMMDDHHMMSS, sdate));
-    }
 }