Эх сурвалжийг харах

feat: 优化视频转图片功能, 添加终止接口,并初步调试通过

WANGKANG 9 сар өмнө
parent
commit
da67bfbf4a

+ 1 - 0
taais-modules/taais-biz/src/main/java/com/taais/biz/constant/BizConstant.java

@@ -11,6 +11,7 @@ public class BizConstant {
         public static final String RUNNING = "1";
         public static final String END = "2";
         public static final String FAILED = "3";
+        public static final String INTERRUPTED = "4";
     }
 
     public static final String TASK_FOLDER_PATH_HEAD = "/task/";

+ 14 - 0
taais-modules/taais-biz/src/main/java/com/taais/biz/controller/Video2imageController.java

@@ -127,6 +127,7 @@ public class Video2imageController extends BaseController {
 
     /**
      * 此函数弃用,改用系统自带的上传系统
+     *
      * @param file
      * @return
      */
@@ -173,6 +174,19 @@ public class Video2imageController extends BaseController {
         }
     }
 
+    @GetMapping("/stop/{id}")
+    public CommonResult stopToImage(@PathVariable("id") Long id) {
+        /**
+         * 传VideoImage的ID
+         */
+        boolean start = video2imageService.stopToImage(id);
+        if (start) {
+            return CommonResult.success("操作成功!");
+        } else {
+            return CommonResult.fail("操作失败!");
+        }
+    }
+
 
     @GetMapping("/zip/{id}")
     public ResponseEntity<org.springframework.core.io.Resource> zipFolder(@PathVariable("id") Long id) throws IOException {

+ 2 - 2
taais-modules/taais-biz/src/main/java/com/taais/biz/domain/bo/Video2imageBo.java

@@ -47,14 +47,14 @@ public class Video2imageBo extends BaseEntity{
      * 开始时间
      */
     // @NotNull(message = "开始时间不能为空")
-    @JsonFormat(pattern = "yyyy-MM-dd")
+    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
     private Date startTime;
 
     /**
      * 结束时间
      */
     // @NotNull(message = "结束时间不能为空")
-    @JsonFormat(pattern = "yyyy-MM-dd")
+    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
     private Date endTime;
 
     /**

+ 2 - 0
taais-modules/taais-biz/src/main/java/com/taais/biz/service/IVideo2imageService.java

@@ -68,4 +68,6 @@ public interface IVideo2imageService extends IBaseService<Video2image> {
     boolean startToImage(Long id);
 
     ResponseEntity<Resource> zipImages(Long id);
+
+    boolean stopToImage(Long id);
 }

+ 66 - 10
taais-modules/taais-biz/src/main/java/com/taais/biz/service/impl/Video2imageServiceImpl.java

@@ -7,6 +7,7 @@ import java.nio.file.Paths;
 import java.util.Arrays;
 import java.util.Date;
 import java.util.List;
+import java.util.Map;
 
 import cn.hutool.core.util.ObjectUtil;
 import com.mybatisflex.core.paginate.Page;
@@ -60,6 +61,8 @@ public class Video2imageServiceImpl extends BaseServiceImpl<Video2imageMapper, V
         return super.query().from(VIDEO2IMAGE);
     }
 
+    public Map<Long, Thread> threads = new java.util.concurrent.ConcurrentHashMap();
+
     private QueryWrapper buildQueryWrapper(Video2imageBo video2imageBo) {
         QueryWrapper queryWrapper = super.buildBaseQueryWrapper();
         queryWrapper.and(VIDEO2IMAGE.NAME.like
@@ -74,9 +77,9 @@ public class Video2imageServiceImpl extends BaseServiceImpl<Video2imageMapper, V
             (video2imageBo.getEndTime()));
         queryWrapper.and(VIDEO2IMAGE.COST_SECOND.eq
             (video2imageBo.getCostSecond()));
-        queryWrapper.and(VIDEO2IMAGE.LOG.eq
+        queryWrapper.and(VIDEO2IMAGE.LOG.like
             (video2imageBo.getLog()));
-        queryWrapper.and(VIDEO2IMAGE.REMARKS.eq
+        queryWrapper.and(VIDEO2IMAGE.REMARKS.like
             (video2imageBo.getRemarks()));
         queryWrapper.and(VIDEO2IMAGE.PATH.eq
             (video2imageBo.getPath()));
@@ -197,9 +200,9 @@ public class Video2imageServiceImpl extends BaseServiceImpl<Video2imageMapper, V
     }
 
 
-    public void updateStatusToEnd(Long id, String log) {
+    public void updateStatus(Long id, String log, String status) {
         Video2image video2image = this.getById(id);
-        video2image.setStatus(END);
+        video2image.setStatus(status);
         video2image.setLog(log);
         video2image.setEndTime(new Date());
         video2image.setCostSecond((video2image.getEndTime().getTime() - video2image.getStartTime().getTime()) / 1000);
@@ -231,15 +234,21 @@ public class Video2imageServiceImpl extends BaseServiceImpl<Video2imageMapper, V
 
         video2image.setStatus(RUNNING);
         video2image.setStartTime(new Date());
+        video2image.setEndTime(null);
+        video2image.setCostSecond(null);
+        video2image.setLog("");
         this.updateById(video2image);
 
-        new Thread(() -> {
+
+        Thread thread__ = new Thread(() -> {
             String log_str = "";
             log.info("开始截取图片子线程...");
             log.info("开始截取图片...");
 
             log_str += "开始截取图片子线程...\n";
             log_str += "开始截取图片...\n";
+            boolean success = false;
+            boolean interrupted = false;
             try {
                 VideoCapture.startCaputreFromUrl(videoPath, outPath, fps);
                 log.info("视频转图片结束...");
@@ -252,14 +261,36 @@ public class Video2imageServiceImpl extends BaseServiceImpl<Video2imageMapper, V
 
                 log_str += "压缩图片成功...\n";
                 log.info("压缩图片成功...");
+                success = true;
+            } catch (InterruptedException e) {
+                e.printStackTrace();
+                log_str += "视频转图片子线程中断...\n" + e.getMessage() + "\n";
+                log.error("视频转图片子线程中断...", e);
+                interrupted = true;
             } catch (Exception e) {
                 e.printStackTrace();
-                log_str += "视频转图片出现位置错误...\n" + e.getMessage() + "\n";
-                log.error("视频转图片出现位置错误...", e);
+                log_str += "视频转图片出现未知错误...\n" + e.getMessage() + "\n";
+                log.error("视频转图片出现未知错误...", e);
+            } finally {
+                if (success) {
+                    System.out.println("更新状态为结束");
+                    updateStatus(id, log_str, END);
+                } else if (interrupted) {
+                    System.out.println("更新状态为中断");
+                    updateStatus(id, log_str, INTERRUPTED); // 子线程中断,更新状态为结束
+                } else {
+                    System.out.println("更新状态为失败");
+                    updateStatus(id, log_str, FAILED); // 子线程中断,更新状态为结束
+                }
+                log.info("视频转图片子线程结束...");
+
+                threads.remove(id);
             }
-            updateStatusToEnd(id, log_str);
-            log.info("视频转图片子线程结束...");
-        }).start();
+        });
+
+        threads.put(id, thread__);
+
+        thread__.start();
 
         return true;
     }
@@ -286,4 +317,29 @@ public class Video2imageServiceImpl extends BaseServiceImpl<Video2imageMapper, V
             .header(HttpHeaders.CONTENT_TYPE, "application/octet-stream")
             .body(resource);
     }
+
+    @Override
+    public boolean stopToImage(Long id) {
+        if (threads.containsKey(id)) {
+            Thread thread = threads.get(id);
+            System.out.println("--------------------1");
+
+            System.out.println("stopToImage..." + thread.toString() + "   " + thread.isAlive());
+            if (ObjectUtil.isNotNull(thread)) {
+                System.out.println("--------------------2");
+
+                thread.interrupt();
+                try {
+                    Thread.sleep(1000);
+                } catch (InterruptedException e) {
+                    throw new RuntimeException(e);
+                }
+                System.out.println("stopToImage..." + thread.toString() + "   " + thread.isAlive());
+                threads.remove(id);
+
+                return true;
+            }
+        }
+        return false;
+    }
 }

+ 4 - 2
taais-modules/taais-biz/src/main/java/com/taais/biz/service/impl/VideoStableServiceImpl.java

@@ -74,6 +74,8 @@ public class VideoStableServiceImpl extends BaseServiceImpl<VideoStableMapper, V
         QueryWrapper queryWrapper = super.buildBaseQueryWrapper();
         queryWrapper.and(VIDEO_STABLE.NAME.like
             (videoStableBo.getName()));
+        queryWrapper.and(VIDEO_STABLE.STATUS.like
+            (videoStableBo.getStatus()));
         queryWrapper.and(VIDEO_STABLE.INPUT_OSS_ID.eq
             (videoStableBo.getInputOssId()));
         queryWrapper.and(VIDEO_STABLE.START_TIME.eq
@@ -82,9 +84,9 @@ public class VideoStableServiceImpl extends BaseServiceImpl<VideoStableMapper, V
             (videoStableBo.getEndTime()));
         queryWrapper.and(VIDEO_STABLE.COST_SECOND.eq
             (videoStableBo.getCostSecond()));
-        queryWrapper.and(VIDEO_STABLE.LOG.eq
+        queryWrapper.and(VIDEO_STABLE.LOG.like
             (videoStableBo.getLog()));
-        queryWrapper.and(VIDEO_STABLE.REMARKS.eq
+        queryWrapper.and(VIDEO_STABLE.REMARKS.like
             (videoStableBo.getRemarks()));
 
         return queryWrapper;

+ 14 - 3
taais-modules/taais-biz/src/main/java/com/taais/biz/utils/VideoCapture.java

@@ -77,7 +77,7 @@ public class VideoCapture {
     }
 
     public static void startCaputreFromUrl(String videoUrl, String savePath__, Long fps)
-        throws IOException {
+        throws IOException, InterruptedException {
         FFmpegFrameGrabber grabber = FFmpegFrameGrabber.createDefault(videoUrl);
         grabber.setOption("rtsp_transport", "tcp"); // 使用tcp的方式,不然会丢包很严重
 
@@ -98,7 +98,15 @@ public class VideoCapture {
         long current_time_stamp = 0;
         long current_time_stamp_step = 1000000 / fps;
         int idx = 0;
+
+        boolean isInterrupted = false;
         while (true) {
+            // 判断是否被中断
+            if (Thread.currentThread().isInterrupted()) {
+                System.out.println("############### 线程中断1 #################");
+                isInterrupted = true;
+                break;
+            }
             Frame frame = grabber.grabImage();
             if (frame != null) {
                 // System.out.println(frame.timestamp);
@@ -113,12 +121,15 @@ public class VideoCapture {
         }
         grabber.stop();
         grabber.release();
-        System.out.println("--- 截图完成---");
+
+        if (isInterrupted) {
+            throw new InterruptedException("线程中断");
+        }
     }
 
     public static boolean saveImage(Frame frame, String savePath, int idx) {
         try {
-            Path outPath =  Paths.get(savePath, String.format("%05d", idx) + /*"_" + System.currentTimeMillis() + */ ".jpg");
+            Path outPath = Paths.get(savePath, String.format("%05d", idx) + /*"_" + System.currentTimeMillis() + */ ".jpg");
             // System.out.println("图片已保存:" + outPath);
             File outPut = new File(outPath.toString());
             ImageIO.write(frameToBufferedImage(frame), "jpg", outPut);