Browse Source

feat: 实现视频转图片开始接口

WANGKANG 10 months ago
parent
commit
b4a6f2e67f

+ 12 - 1
taais-modules/taais-biz/src/main/java/com/taais/biz/controller/Video2imageController.java

@@ -90,7 +90,6 @@ public class Video2imageController extends BaseController {
         newVideo2imageBo.setName(video2imageBo.getName());
         newVideo2imageBo.setFileId(video2imageBo.getFileId());
         newVideo2imageBo.setFps(video2imageBo.getFps());
-        newVideo2imageBo.setStartTime(new Date());
         newVideo2imageBo.setStatus(NOT_START);
         newVideo2imageBo.setPath(Paths.get(UPLOAD_DIR, video2imageBo.getFileId()).toString());
         newVideo2imageBo.setOutPath(Paths.get(UPLOAD_DIR, video2imageBo.getName(), "images").toString());
@@ -161,4 +160,16 @@ public class Video2imageController extends BaseController {
             return CommonResult.success("上传失败");
         }
     }
+    @GetMapping("/start/{id}")
+    public CommonResult startToImage(@PathVariable("id") Long id) {
+        /**
+         * 传VideoImage的ID
+         */
+        boolean start = video2imageService.startToImage(id);
+        if (start) {
+            return CommonResult.success("操作成功!");
+        } else {
+            return CommonResult.fail("操作失败!");
+        }
+    }
 }

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

@@ -3,6 +3,7 @@ package com.taais.biz.service;
 import com.taais.biz.domain.Video2image;
 import com.taais.biz.domain.bo.Video2imageBo;
 import com.taais.biz.domain.vo.Video2imageVo;
+import com.taais.common.core.core.domain.CommonResult;
 import com.taais.common.core.core.page.PageResult;
 import com.taais.common.orm.core.service.IBaseService;
 
@@ -63,4 +64,5 @@ public interface IVideo2imageService extends IBaseService<Video2image> {
      */
     boolean deleteByIds(Long[] ids);
 
+    boolean startToImage(Long id);
 }

+ 54 - 0
taais-modules/taais-biz/src/main/java/com/taais/biz/service/impl/Video2imageServiceImpl.java

@@ -1,6 +1,7 @@
 package com.taais.biz.service.impl;
 
 import java.util.Arrays;
+import java.util.Date;
 import java.util.List;
 
 import cn.hutool.core.util.ObjectUtil;
@@ -11,6 +12,7 @@ import com.taais.biz.domain.bo.Video2imageBo;
 import com.taais.biz.domain.vo.Video2imageVo;
 import com.taais.biz.mapper.Video2imageMapper;
 import com.taais.biz.service.IVideo2imageService;
+import com.taais.biz.utils.VideoCapture;
 import com.taais.common.core.utils.MapstructUtils;
 import com.taais.common.orm.core.page.PageQuery;
 import com.taais.common.core.core.page.PageResult;
@@ -19,6 +21,8 @@ import jakarta.annotation.Resource;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
+import static com.taais.biz.constant.BizConstant.VideoStatus.END;
+import static com.taais.biz.constant.BizConstant.VideoStatus.RUNNING;
 import static com.taais.biz.domain.table.Video2imageTableDef.VIDEO2IMAGE;
 
 /**
@@ -141,4 +145,54 @@ public class Video2imageServiceImpl extends BaseServiceImpl<Video2imageMapper, V
         return this.removeByIds(Arrays.asList(ids));
     }
 
+
+    public void updateStatusToEnd(Long id, String log) {
+        Video2image video2image = this.getById(id);
+        video2image.setStatus(END);
+        video2image.setLog(log);
+        video2image.setEndTime(new Date());
+        video2image.setCostSecond((video2image.getEndTime().getTime() - video2image.getStartTime().getTime()) / 1000);
+        this.updateById(video2image);
+    }
+    @Override
+    public boolean startToImage(Long id) {
+        Video2image video2image = this.getById(id);
+        if (ObjectUtil.isNull(video2image)) {
+            System.out.println("ObjectUtil.isNotNull(video2image)...");
+            return false;
+        }
+        System.out.println("开始截取图片...");
+
+        String videoPath = video2image.getPath();
+        String outPath = video2image.getOutPath();
+        Long fps = video2image.getFps();
+
+        System.out.println("videoPath: " + videoPath);
+        System.out.println("outPath: " + outPath);
+        System.out.println("fps: " + fps);
+
+        video2image.setStatus(RUNNING);
+        video2image.setStartTime(new Date());
+        this.updateById(video2image);
+
+        System.out.println("开始截取图片子线程...");
+
+        new Thread(() -> {
+            String log = "开始截取图片...\n";
+            System.out.println("开始截取图片...");
+            try {
+                VideoCapture.startCaputre(videoPath, outPath, fps);
+                System.out.println("视频转图片结束...");
+                log += "视频转图片结束...\n";
+            } catch (Exception e) {
+                e.printStackTrace();
+                log += "视频转图片出现位置错误...\n";
+                log += e.getMessage();
+            }
+            updateStatusToEnd(id, log);
+            System.out.println("视频转图片子线程结束...");
+        }).start();
+
+        return true;
+    }
 }

+ 42 - 11
taais-modules/taais-biz/src/main/java/com/taais/biz/utils/VideoCapture.java

@@ -7,39 +7,52 @@
  * @brief : 视频转图片工具类
  */
 
-
 package com.taais.biz.utils;
 
-import java.io.IOException;
 
 import org.bytedeco.javacv.FFmpegFrameGrabber;
-
-import java.io.File;
-
 import org.bytedeco.javacv.Frame;
+import org.bytedeco.javacv.Java2DFrameConverter;
 
+import java.io.IOException;
+import java.io.File;
 import javax.imageio.ImageIO;
 import java.awt.image.BufferedImage;
 
-import org.bytedeco.javacv.Java2DFrameConverter;
 
+import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
 
+import java.util.stream.Stream;
+
 public class VideoCapture {
     /**
      * 截取视频帧并保存为图片
      * 设置唯一视频名称,保存到指定的路径下
      *
-     * @param filePath 视频文件路径
-     * @param savePath 图片保存路径
-     * @param fps      帧率
+     * @param filePath   视频文件路径
+     * @param savePath__ 图片保存路径
+     * @param fps        帧率
      * @throws IOException
      * @throws InterruptedException
      */
-    public static void startCaputre(String filePath, String savePath, int fps)
+    public static void startCaputre(String filePath, String savePath__, Long fps)
         throws IOException {
         FFmpegFrameGrabber grabber = FFmpegFrameGrabber.createDefault(filePath);
+
+        Path savePath = Paths.get(savePath__);
+        if (Files.exists(savePath)) {
+            System.out.println("目录已存在: " + savePath);
+            System.out.println("清空里面的内容");
+            try {
+                deleteDirectory(savePath);
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
+        }
+        Files.createDirectories(savePath);
+
         System.out.println("--- 开始截图 ---");
         grabber.start();
         long current_time_stamp = 0;
@@ -52,7 +65,7 @@ public class VideoCapture {
                 if (frame.timestamp >= current_time_stamp) {
                     idx += 1;
                     current_time_stamp += current_time_stamp_step;
-                    saveImage(frame, savePath, idx);
+                    saveImage(frame, savePath__, idx);
                 }
             } else {
                 break;
@@ -100,4 +113,22 @@ public class VideoCapture {
         grabber.setFrameNumber(frame);
         return grabber.getTimestamp();
     }
+
+    // 递归删除目录及其内容
+    private static void deleteDirectory(Path directory) throws Exception {
+        if (Files.isDirectory(directory)) {
+            // 遍历目录中的所有文件和子目录
+            try (Stream<Path> stream = Files.list(directory)) {
+                stream.forEach(path -> {
+                    try {
+                        deleteDirectory(path);
+                    } catch (Exception e) {
+                        e.printStackTrace();
+                    }
+                });
+            }
+        }
+        // 删除文件或空目录
+        Files.delete(directory);
+    }
 }