suntianwu 3 年 前
コミット
16849c261f

+ 1 - 1
src/main/java/com/iden/bms/controller/TraceController.java

@@ -70,7 +70,7 @@ public class TraceController {
     @GetMapping("/listUploadImagesTrace")
     @ApiOperation(value = "使用上传图像获取目标轨迹列表(地图打点划线) ")
     @ApiImplicitParams(value = {
-            @ApiImplicitParam(paramType = "query", name = "imageNames", value = "图像名称,多个用逗号分割"),
+            @ApiImplicitParam(paramType = "query", name = "imageNames", value = "图像名称,多个用逗号分割,格式:  新名字_老名字,新名字_老名字,新名字_老名字"),
             @ApiImplicitParam(paramType = "query", name = "beginTime", value = "开始时间(yyyy-MM-dd HH:mm:ss)"),
             @ApiImplicitParam(paramType = "query", name = "endTime", value = "结束时间(yyyy-MM-dd HH:mm:ss)")
 

+ 5 - 1
src/main/java/com/iden/bms/service/TraceService.java

@@ -86,6 +86,10 @@ public class TraceService {
                 if (!ImgUtil.isImg(fileName)) {
                     throw new BDException("图像文件必须是图片格式!");
                 }
+                //验证文件名是否合格
+                if (fileName.contains("_")) {
+                    throw new BDException("图像文件名称不能有下划线!");
+                }
                 String saveFileName = DateUtils.getCurrYyyyMMddHHmmssDate() + "_" + UUID.randomUUID().toString()  + fileName.substring(fileName.lastIndexOf("."));
                 String picFullFileName = idenRoot + "data/final/trace/" + saveFileName;
                 FileOutputStream fos = new FileOutputStream(picFullFileName);
@@ -126,7 +130,7 @@ public class TraceService {
         File[] imgFiles = new File[imageNameArr.length];
         for (int i = 0; i < imageNameArr.length; i++) {
             if (StringUtils.isNotEmpty(imageNameArr[i])){
-                imgFiles[i] = new File(idenRoot + "data/final/trace/" + imageNameArr[i]);
+                imgFiles[i] = new File(idenRoot + "data/final/trace/" + imageNameArr[i].substring(0,imageNameArr[i].indexOf("_")));
             }
         }
 

+ 70 - 2
src/main/java/com/iden/common/videotool/VideoTool.java

@@ -3,7 +3,13 @@ package com.iden.common.videotool;
 import com.iden.common.util.DateUtils;
 import lombok.extern.slf4j.Slf4j;
 
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.InputStreamReader;
 import java.util.Date;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 @Slf4j
 public class VideoTool {
@@ -20,9 +26,10 @@ public class VideoTool {
         Long endTime = shootEndTime.getTime() ;
 
         //ms
-        long duration = 0L;
+        long duration = getVideoTime(ffmpegPath,videoFilePath) * 1000;
 
-        String cmd = ffmpegPath +  " -y -i " + videoFilePath + " -r 1 " + saveDir + "%04d.jpg";
+
+        String cmd = ffmpegPath +  " -y -i " + videoFilePath + " -r 1 " + saveDir + "%08d.jpg";
 
         log.info("开始运行视频分解为图片命令:" + cmd);
         Runtime.getRuntime().exec(cmd);
@@ -51,4 +58,65 @@ public class VideoTool {
         log.info("视频剪辑完成");
 
     }
+
+    /**
+     * 获取视频总时间
+     * @param ffmpeg_path	ffmpeg路径
+     * @param video_path    视频路径
+     * @return
+     */
+    public static int getVideoTime( String ffmpeg_path,String video_path) {
+        List<String> commands = new java.util.ArrayList<String>();
+        commands.add(ffmpeg_path);
+        commands.add("-i");
+        commands.add(video_path);
+        try {
+            ProcessBuilder builder = new ProcessBuilder();
+            builder.command(commands);
+            final Process p = builder.start();
+
+            //从输入流中读取视频信息
+            BufferedReader br = new BufferedReader(new InputStreamReader(p.getErrorStream()));
+            StringBuffer sb = new StringBuffer();
+            String line = "";
+            while ((line = br.readLine()) != null) {
+                sb.append(line);
+            }
+            br.close();
+
+            //从视频信息中解析时长
+            String regexDuration = "Duration: (.*?), start: (.*?), bitrate: (\\d*) kb\\/s";
+            Pattern pattern = Pattern.compile(regexDuration);
+            Matcher m = pattern.matcher(sb.toString());
+            if (m.find()) {
+                int time = getTimelen(m.group(1));
+                System.out.println(video_path+",视频时长:"+time+", 开始时间:"+m.group(2)+",比特率:"+m.group(3)+"kb/s");
+                return time;
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+
+        return 0;
+    }
+
+    //格式:"00:00:10.68"
+    private static int getTimelen(String timelen){
+        int min=0;
+        String strs[] = timelen.split(":");
+        if (strs[0].compareTo("0") > 0) {
+            min+=Integer.valueOf(strs[0])*60*60;//秒
+        }
+        if(strs[1].compareTo("0")>0){
+            min+=Integer.valueOf(strs[1])*60;
+        }
+        if(strs[2].compareTo("0")>0){
+            min+=Math.round(Float.valueOf(strs[2]));
+        }
+        return min;
+    }
+
+    public static void main(String[] args) {
+        System.out.println(getVideoTime("D:\\program\\ffmpeg\\ffmpeg-4.3.1-2021-01-01-essentials_build\\bin\\ffmpeg.exe","e:/output99.mp4"));
+    }
 }

+ 3 - 4
src/test/java/com/face/video/cmd/videoCmd.java

@@ -12,7 +12,7 @@ public class videoCmd {
             String ffmpegPath = "D:\\program\\ffmpeg\\ffmpeg-4.3.1-2021-01-01-essentials_build\\bin\\ffmpeg.exe";
 
            // String cmd = ffmpegPath + " -ss 0:05 -i e:/20211223123223_76639ced-6400-11ec-b8f9-fa163e4e1e9f.mp4 -t 10 -c:v copy -c:a copy e:/555.mp4 ";
-            String cmd = ffmpegPath +  " -y -i e:/20211223123223_76639ced-6400-11ec-b8f9-fa163e4e1e9f.mp4 -r 24 e:/aa/%04d.jpg";
+            String cmd = ffmpegPath +  " -y -i e:/20211223123223_76639ced-6400-11ec-b8f9-fa163e4e1e9f.mp4 -r 1 e:/aa/%08d.jpg";
 
 
                     // 执行命令
@@ -25,9 +25,8 @@ public class videoCmd {
         }
     }
     public static void main(String[] args) {
-        File saveDir= new File("e;/aa/");
-        System.out.println(saveDir);
-       //new videoCmd().exe();
+
+       new videoCmd().exe();
     }