Browse Source

修改视频文件合并,但未测试,修改后是否符合原业务逻辑需要验证

Aladding 7 months ago
parent
commit
920e0c1a99
1 changed files with 94 additions and 62 deletions
  1. 94 62
      hazard-admin/src/main/java/com/ozs/web/core/util/CameraUtil.java

+ 94 - 62
hazard-admin/src/main/java/com/ozs/web/core/util/CameraUtil.java

@@ -1,6 +1,39 @@
 package com.ozs.web.core.util;
 
 
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
+import java.io.PrintWriter;
+import java.nio.charset.StandardCharsets;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.GregorianCalendar;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.UUID;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.stream.Collectors;
+
+import javax.annotation.PostConstruct;
+import javax.annotation.Resource;
+
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.stereotype.Component;
+import org.springframework.util.ObjectUtils;
+
 import com.alibaba.fastjson2.JSON;
 import com.alibaba.fastjson2.JSONArray;
 import com.alibaba.fastjson2.JSONObject;
@@ -13,13 +46,13 @@ import com.ozs.common.core.redis.RedisCache;
 import com.ozs.common.exception.base.BaseException;
 import com.ozs.common.utils.DateUtils;
 import com.ozs.common.utils.http.HttpUtils;
+import com.ozs.common.utils.uuid.IdUtils;
 import com.ozs.framework.config.ServerConfig;
 import com.ozs.service.entity.BaseCameraManagement;
 import com.ozs.service.entity.BaseDeviceDynamicManagement;
 import com.ozs.service.entity.CameraLogDetail;
 import com.ozs.service.entity.MqLog;
 import com.ozs.service.entity.MsgAlarm;
-import com.ozs.service.entity.MsgWebPush;
 import com.ozs.service.service.BaseCameraManagementService;
 import com.ozs.service.service.BaseDeviceDynamicManagementService;
 import com.ozs.service.service.CameraLogDetailService;
@@ -27,24 +60,9 @@ import com.ozs.service.service.MqLogService;
 import com.ozs.service.service.MsgAlarmService;
 import com.ozs.system.mapper.SysDictDataMapper;
 import com.ozs.web.core.config.CaneraConfig;
+
 import lombok.SneakyThrows;
 import lombok.extern.slf4j.Slf4j;
-import org.apache.commons.lang3.StringUtils;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.context.annotation.Configuration;
-import org.springframework.stereotype.Component;
-import org.springframework.util.ObjectUtils;
-
-import javax.annotation.PostConstruct;
-import javax.annotation.Resource;
-import java.io.*;
-import java.text.ParseException;
-import java.text.SimpleDateFormat;
-import java.util.*;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
-import java.util.concurrent.TimeUnit;
-import java.util.stream.Collectors;
 
 /**
  * 相机工具
@@ -205,13 +223,12 @@ public class CameraUtil {
             try {
                 return historyPlay(list, ph, flay);
             } catch (Exception e) {
-                log.error(e.getMessage());
-                e.printStackTrace();
+                log.error("e", e);
+                throw new BaseException("视频处理失败");
             }
         } else {
             throw new BaseException("当前相机无视频录像");
         }
-        return null;
     }
 
 
@@ -414,50 +431,65 @@ ffmpeg -i "concat:1.ts|2.ts" -c copy output.mp4
         }
         cUtil.cmd("chomd -R 777 " + file.getParentFile().getPath());
         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()) {
-                log.info("file:{}", fromVideoFileList.get(t));
-                String substring = fromVideoFileList.get(t).substring(0, fromVideoFileList.get(t).lastIndexOf("."));
-                int x;//定义两变量
-                Random ne = new Random();//实例化一个random的对象ne
-                x = ne.nextInt(9999 - 1000 + 1) + 1000;//为变量赋随机值1000-9999
-                substring = substring + x;//定义两变量
-
-                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\" ");
-                }
-            }
+        
+        List<String> validFileList = new ArrayList<>();
+        StringBuilder argFileTxt = new StringBuilder();
+        fromVideoFileList.forEach(filePath -> {
+        	File videoFile = new File(filePath);
+        	if (!videoFile.exists()) {
+        		return;
+        	}
+        	validFileList.add(filePath);
+        	log.info("file:{}", filePath);
+        	
+        	argFileTxt.append("file ")
+        		.append("'").append(filePath).append(".").append(System.lineSeparator());
+        	
+        });
+        
+        if (!flay) {
+        	validFileList.add(newfilePath);
         }
-
-        if (fileTs.size() > 0) {
-            Map<String, Object> mergeVideoTsFile = rc.getCacheMap(tsFilekey);
-            if (ObjectUtils.isEmpty(mergeVideoTsFile)) {
-                mergeVideoTsFile = new HashMap<>();
-            }
-            if (!flay) {
-                // 如果没有解除,把生成的文件放入要删除的定时任务 redis key 中
-                fileTs.add(newfilePath);
-            }
-            mergeVideoTsFile.put(new Date().getTime() + "", fileTs);
-            rc.deleteObject(tsFilekey);
-            if (mergeVideoTsFile.size() > 0) {
-                rc.setCacheMap(tsFilekey, mergeVideoTsFile);
-            }
-
-            sm.append("-c copy " + newfilePath);
-            log.info("合并命令:{}", sm.toString());
-            cUtil.cmd(sm.toString());
+        
+        putDeleteCache(validFileList);
+        
+        if (argFileTxt.length() == 0) {
+        	return;
         }
-
+        
+        String argFileName = IdUtils.fastSimpleUUID() + "_tmp.txt";
+        File argFile = new File(file.getParentFile(), argFileName);
+        try (
+    		OutputStreamWriter bos = new OutputStreamWriter(new FileOutputStream(argFile), StandardCharsets.UTF_8);
+        ) {
+        	bos.write(argFileTxt.toString());
+        	bos.flush();
+        }
+        
+        StringBuilder cmd = new StringBuilder(ffmpegPath);
+        cmd.append(" concat -i")
+        		.append(argFileName)
+	        	.append(" -c copy ")
+	        	.append(newfilePath);
+        
+        log.info("合并命令:{}", cmd);
+        
+        cUtil.cmd(cmd.toString());
+        
+        argFile.delete();
+    }
+    
+    private static void putDeleteCache(List<String> pathList) {
+    	if (ObjectUtils.isEmpty(pathList)) {
+    		return;
+    	}
+        Map<String, Object> mergeVideoTsFile = rc.getCacheMap(tsFilekey);
+        if (ObjectUtils.isEmpty(mergeVideoTsFile)) {
+            mergeVideoTsFile = new HashMap<>();
+        }
+        mergeVideoTsFile.put(new Date().getTime() + "", pathList);
+        rc.deleteObject(tsFilekey);
+        rc.setCacheMap(tsFilekey, mergeVideoTsFile);
     }
 
     public static List<Map<String, Object>> getRecordList(String channel, Date startTm, Date endTm) {