Parcourir la source

Merge branch 'tl-develop' into develop

allen il y a 8 mois
Parent
commit
70d3e2c225

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

@@ -485,7 +485,7 @@ public class DataAugmentationController extends BaseController {
     public CommonResult<VideoUrl> getVideoUrl(@PathVariable Long id) {
         DataAugmentation byId = dataAugmentationService.getById(id);
         String localPath = TaaisConfig.getProfile();
-        List<String> extensions = Arrays.asList(".mp4", ".avi", ".mkv", ".mov", ".flv", ".wmv");
+        List<String> extensions = Arrays.asList(".mp4");
         String firstInputVideoFile = findFirstVideoFile(byId.getInputPath(), extensions);
         if (firstInputVideoFile == null) {
             return CommonResult.fail(byId.getInputPath() + "目录下不存在输入视频文件!");

+ 95 - 0
taais-modules/taais-biz/src/main/java/com/taais/biz/controller/PublicController.java

@@ -2,6 +2,7 @@ package com.taais.biz.controller;
 
 import com.alibaba.fastjson2.JSON;
 import com.taais.biz.constant.BizConstant;
+import com.taais.biz.domain.DataAugmentation;
 import com.taais.biz.domain.TaskTrackResultBo;
 import com.taais.biz.domain.bo.*;
 import com.taais.biz.domain.dto.TaskResultDTO;
@@ -22,10 +23,18 @@ import org.apache.commons.lang3.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.core.io.InputStreamResource;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.MediaType;
+import org.springframework.http.ResponseEntity;
 import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.*;
 
 import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.nio.file.Path;
+import java.nio.file.Paths;
 import java.util.Arrays;
 import java.util.Date;
 
@@ -194,4 +203,90 @@ public class PublicController extends BaseController {
             return CommonResult.fail("业务类型不支持");
         }
     }
+    @GetMapping("/video/input/{id}")
+    public ResponseEntity<InputStreamResource> streamVideo(@PathVariable Long id) throws IOException {
+        // 视频文件存储路径
+
+
+
+        DataAugmentation byId = dataAugmentationService.getById(id);
+        System.out.println("dataaa:"+byId);
+        String inputPath = byId.getInputPath();
+        Path path = Paths.get(inputPath, "Project_Test.avi");
+        String filename = path.toString();
+        File videoFile = new File(filename);
+        if (!videoFile.exists()) {
+            return ResponseEntity.notFound().build();
+        }
+
+        // 获取视频文件的扩展名来判断 MIME 类型
+        String fileExtension = getFileExtension(filename);
+        MediaType mediaType = getMediaType(fileExtension);
+
+        // 打开视频文件流
+        FileInputStream videoStream = new FileInputStream(videoFile);
+
+        // 返回响应流
+        return ResponseEntity.ok()
+            .contentType(mediaType)
+            .header(HttpHeaders.CONTENT_DISPOSITION, "inline; filename=\"" + filename + "\"")  // 可以直接播放
+            .body(new InputStreamResource(videoStream));
+    }
+
+    @GetMapping("/video/output/{id}")
+    public ResponseEntity<InputStreamResource> streamOutputVideo(@PathVariable Long id) throws IOException {
+        // 视频文件存储路径
+
+        DataAugmentation byId = dataAugmentationService.getById(id);
+        String inputPath = byId.getOutputPath();
+        Path path = Paths.get(inputPath, "result.avi");
+        String filename = path.toString();
+        File videoFile = new File(filename);
+        if (!videoFile.exists()) {
+            return ResponseEntity.notFound().build();
+        }
+
+        // 获取视频文件的扩展名来判断 MIME 类型
+        String fileExtension = getFileExtension(filename);
+        MediaType mediaType = getMediaType(fileExtension);
+
+        // 打开视频文件流
+        FileInputStream videoStream = new FileInputStream(videoFile);
+
+        // 返回响应流
+        return ResponseEntity.ok()
+            .contentType(mediaType)
+            .header(HttpHeaders.CONTENT_DISPOSITION, "inline; filename=\"" + filename + "\"")  // 可以直接播放
+            .body(new InputStreamResource(videoStream));
+    }
+
+    // 获取文件扩展名
+    private String getFileExtension(String filename) {
+        int dotIndex = filename.lastIndexOf('.');
+        return (dotIndex > 0) ? filename.substring(dotIndex + 1) : "";
+    }
+
+    // 根据文件扩展名返回正确的 MIME 类型
+    private MediaType getMediaType(String extension) {
+        switch (extension.toLowerCase()) {
+            case "mp4":
+                return MediaType.valueOf("video/mp4");
+            case "avi":
+                return MediaType.valueOf("video/x-msvideo");
+            case "mov":
+                return MediaType.valueOf("video/quicktime");
+            case "webm":
+                return MediaType.valueOf("video/webm");
+            case "flv":
+                return MediaType.valueOf("video/x-flv");
+            case "ogg":
+                return MediaType.valueOf("video/ogg");
+            default:
+                return MediaType.valueOf("application/octet-stream"); // 默认二进制流
+        }
+    }
+    // 根据文件扩展名返回正确的 MIME 类型
+
+
+
 }