suntianwu vor 3 Jahren
Ursprung
Commit
f9098ac4bf

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

@@ -130,4 +130,26 @@ public class TraceController {
             log.error("轨迹管理: 轨迹下载出现异常",e);
         }
     }
+
+    @GetMapping("/cutVideo")
+    @ApiOperation(value = "剪辑视频 ")
+    @ApiImplicitParams(value = {
+            @ApiImplicitParam(paramType = "query", name = "videoId", value = "视频ID",required = true),
+            @ApiImplicitParam(paramType = "query", name = "beginTime", value = "开始时间(HH:mm:ss)",required = true),
+            @ApiImplicitParam(paramType = "query", name = "endTime", value = "结束时间(HH:mm:ss)",required = true)
+
+    })
+    public Result<String> cutVideo(HttpServletRequest request, @RequestHeader(value = "token") String token,
+                                                                   @RequestParam(value = "videoId", required = true) Long videoId,
+                                                                   @RequestParam(value = "beginTime", required = true) String beginTime,
+                                                                   @RequestParam(value = "endTime", required = true) String endTime){
+        try {
+            UserLoginedConvertVO loginUser = WebPageUtils.getCurrentLoginedUser(request);
+            String datas = this.traceService.cutVideo(videoId,beginTime,endTime,loginUser);
+            return Result.success("剪辑成功",datas);
+        }catch (Exception e) {
+            log.error("轨迹管理: 剪辑视频出现异常",e);
+            return Result.error( "剪辑视频失败");
+        }
+    }
 }

+ 45 - 0
src/main/java/com/iden/bms/service/TraceService.java

@@ -25,6 +25,7 @@ import com.iden.common.util.ByteUtil;
 import com.iden.common.util.DateUtils;
 import com.iden.common.util.ImgUtil;
 import com.iden.common.vo.*;
+import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
@@ -44,6 +45,7 @@ import java.util.*;
  * @author makejava
  * @since 2021-05-21 00:08:38
  */
+@Slf4j
 @Service
 public class TraceService {
 
@@ -62,6 +64,9 @@ public class TraceService {
     @Value("${file.url:#{null}}")
     private String fileUrl;
 
+    @Value("${ffmpeg.path:#{null}}")
+    private String ffmpegPath;
+
     /**
      * 上传图像
      * @param file
@@ -266,4 +271,44 @@ public class TraceService {
     }
 
 
+    public String cutVideo(Long videoId,String beginTime,String ednTime,UserLoginedConvertVO loginUser) {
+        String videoUrl = null;
+        try {
+            IdenCameraVideo idenCameraVideo = this.idenCameraVideoService.getById(videoId);
+            if(idenCameraVideo == null){
+                return null;
+            }
+
+            IdenCamera idenCamera = this.idenCameraService.getById(idenCameraVideo.getCameraId());
+            if (idenCamera == null){
+                return null;
+            }
+
+            Long duration = DateUtils.betweenTimeSS(beginTime,ednTime); //s
+
+            String videoFileName = idenCameraVideo.getVideoUrl().substring(idenCameraVideo.getVideoUrl().lastIndexOf("/"));
+            String videoFileExt = videoFileName.substring(videoFileName.lastIndexOf("."));
+            String outFileName =  UUID.randomUUID().toString() + "." + videoFileExt;
+
+            String videoFilepath = idenRoot + "data/final/camera/video/" + idenCamera.getCode() + "/" + videoFileName;
+            //String videoFilepath = "e:/20211223123223_76639ced-6400-11ec-b8f9-fa163e4e1e9f.mp4";
+            String ountFilePath = idenRoot + "data/final/tmp/" + idenCamera.getCode() + "/" + outFileName;
+            //String ountFilePath = "e:/xx9.mp4";
+            videoUrl = fileUrl + "tmp/" +  idenCamera.getCode() + "/" + outFileName;
+
+            String cmd = ffmpegPath + " -ss " + beginTime + " -i " + videoFilepath
+                    + " -t " + duration + " -c:v copy -c:a copy " + ountFilePath;
+
+            log.info("开始运行视频剪辑命令:" + cmd);
+             Runtime.getRuntime().exec(cmd);
+            log.info("视频剪辑完成");
+
+        } catch (IOException e) {
+            e.printStackTrace();
+
+        }
+
+        return videoUrl;
+    }
+
 }

+ 18 - 0
src/main/java/com/iden/common/util/DateUtils.java

@@ -127,6 +127,24 @@ public class DateUtils {
     public static long betweenDateSSS(Date startDate, Date endDate) {
         return null == startDate || null == endDate ? 0 : endDate.getTime() - startDate.getTime();
     }
+
+    /**
+     * 俩时间段之间的间隔秒数
+     *
+     * @param startTime(HH:mm:ss)
+     * @param enTime(HH:mm:ss)
+     * @return
+     */
+    public static long betweenTimeSS(String startTime, String enTime) {
+        String dateStr = getCurrYyyy_MM_ddDate();
+        String start = dateStr + " " + startTime;
+        String end = dateStr + " " + enTime;
+        Date startDate = strToDate(start,"yyyy-MM-dd HH:mm:ss");
+        Date endDate = strToDate(end,"yyyy-MM-dd HH:mm:ss");
+
+        return (endDate.getTime() - startDate.getTime()) /1000;
+    }
+
     /**
      * 判断一个字符串是不是一个合法的日期格式
      *

+ 4 - 0
src/main/resources/application-dev.properties

@@ -36,3 +36,7 @@ file.url=http://124.70.58.209:17778/
 
 ##是否初始化加载人脸识别库,是:1,不:0
 init.face.dataset=1
+
+#视频编辑程序所在路径
+ffmpeg.path=/data/iden/ffmpeg-3.1.3/ffmpeg
+

+ 3 - 0
src/main/resources/application-prod.properties

@@ -39,3 +39,6 @@ image.url=http://124.70.58.209:17778/
 
 ##是否初始化加载人脸识别库,是:1,不:0
 init.face.dataset=1
+
+#视频编辑程序所在路径
+ffmpeg.path=/data/iden/ffmpeg-3.1.3/ffmpeg