Browse Source

视频转文本

zhaoxian 1 year ago
parent
commit
32d80ac35d

+ 2 - 2
ruixuan-live/src/main/java/com/ruixuan/data/controller/VideoTextController.java

@@ -20,13 +20,13 @@ public class VideoTextController extends BaseController {
 
 
 
 
     @GetMapping(value = "getVideoText")
     @GetMapping(value = "getVideoText")
-    @ApiOperation(value = "添加视频")
+    @ApiOperation(value = "视频转文本")
     public ResultResponse getVideoText(@ApiParam("视频URL") @RequestParam(value = "url", required = true) String url) {
     public ResultResponse getVideoText(@ApiParam("视频URL") @RequestParam(value = "url", required = true) String url) {
         try {
         try {
             return videoTextService.getVideoText(url);
             return videoTextService.getVideoText(url);
         } catch (Exception e) {
         } catch (Exception e) {
             e.printStackTrace();
             e.printStackTrace();
-            return ResultResponse.error("添加异常");
+            return ResultResponse.error("转文本异常");
         }
         }
     }
     }
 }
 }

+ 21 - 3
ruixuan-live/src/main/java/com/ruixuan/data/service/impl/VideoTextServiceImpl.java

@@ -11,6 +11,8 @@ import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 import org.springframework.stereotype.Service;
 
 
+import java.io.File;
+import java.nio.file.Path;
 import java.nio.file.Paths;
 import java.nio.file.Paths;
 import java.util.Optional;
 import java.util.Optional;
 
 
@@ -34,12 +36,28 @@ public class VideoTextServiceImpl implements VideoTextService {
         //本地文件内容
         //本地文件内容
         String filePath = FileUtil.writeFiles(localPath.toString(), url, split[split.length - 1]);
         String filePath = FileUtil.writeFiles(localPath.toString(), url, split[split.length - 1]);
 
 
+        Path path = Paths.get(filePath);
+        Path pcmPath = Paths.get(localPath.toString());
+
         // 获取 PCM path
         // 获取 PCM path
-        Optional<String> paths = VideoToPCMTool.convertMP4toPCM(Paths.get(filePath), Paths.get(localPath.toString()));
+        Optional<String> paths = VideoToPCMTool.convertMP4toPCM(path, pcmPath);
+        String videoText = null;
         if (!Check.isNull(paths)) {
         if (!Check.isNull(paths)) {
-            return ResultResponse.success(VoiceTool.getVideoText(paths.get()));
+            videoText = VoiceTool.getVideoText(paths.get());
+        }
+        try {
+            File file = path.toFile();
+            if (file.exists()) {
+                file.delete();
+            }
+            File pcmFile = pcmPath.toFile();
+            if (pcmFile.exists()) {
+                pcmFile.delete();
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
         }
         }
-        return ResultResponse.error("转文本失败");
+        return ResultResponse.success(videoText);
     }
     }
 
 
 }
 }