lyhzzz 3 yıl önce
ebeveyn
işleme
96aff1b4fd

+ 1 - 1
src/main/java/com/fdkankan/fusion/common/util/OBJToGLBUtil.java

@@ -18,7 +18,7 @@ public class OBJToGLBUtil {
         log.info("obj转换glb开始");
         String command = "obj2gltf " + objPath + " " + glbPath;
         log.info("执行obj转换glb命令路径-{}", command);
-        CreateObjUtil.callshell(command);
+        ShellUtil.execCmd(command);
         log.info("obj转换glb完毕:" + command);
     }
 

+ 66 - 0
src/main/java/com/fdkankan/fusion/common/util/ShellUtil.java

@@ -0,0 +1,66 @@
+package com.fdkankan.fusion.common.util;
+
+import lombok.extern.slf4j.Slf4j;
+
+import java.io.BufferedReader;
+import java.io.Closeable;
+import java.io.InputStreamReader;
+
+@Slf4j
+public class ShellUtil {
+    /**
+     * 执行系统命令, 返回执行结果
+     * @param cmd 需要执行的命令
+     */
+    public static void execCmd(String cmd) {
+        StringBuilder result = new StringBuilder();
+
+        Process process = null;
+        BufferedReader bufrIn = null;
+        BufferedReader bufrError = null;
+        long startTime = System.currentTimeMillis();
+
+        try {
+            String[] commond = {"/bin/sh","-c",cmd};
+            // 执行命令, 返回一个子进程对象(命令在子进程中执行)
+            log.info("执行cmd:{}",commond);
+            process = Runtime.getRuntime().exec(commond);
+            // 方法阻塞, 等待命令执行完成(成功会返回0)
+            process.waitFor();
+            // 获取命令执行结果, 有两个结果: 正常的输出 和 错误的输出(PS: 子进程的输出就是主进程的输入)
+            bufrIn = new BufferedReader(new InputStreamReader(process.getInputStream(), "UTF-8"));
+            bufrError = new BufferedReader(new InputStreamReader(process.getErrorStream(), "UTF-8"));
+            // 读取输出
+            String line = null;
+            while ((line = bufrIn.readLine()) != null) {
+                result.append(line).append('\n');
+            }
+            while ((line = bufrError.readLine()) != null) {
+                result.append(line).append('\n');
+            }
+
+        }catch (Exception e){
+            e.printStackTrace();
+        }finally {
+            closeStream(bufrIn);
+            closeStream(bufrError);
+            // 销毁子进程
+            if (process != null) {
+                process.destroy();
+            }
+        }
+        // 返回执行结果
+        log.info("执行cmd:{},结果:{},耗时:{}", cmd,result.toString(),System.currentTimeMillis() -startTime);
+    }
+
+    private static void closeStream(Closeable stream) {
+        if (stream != null) {
+            try {
+                stream.close();
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
+        }
+    }
+
+}

+ 24 - 3
src/main/java/com/fdkankan/fusion/common/util/VideoUtil.java

@@ -77,7 +77,9 @@ public class VideoUtil {
         if(!tagFile.exists()){
             tagFile.mkdirs();
         }
-        String fileName =  UUID.randomUUID().toString().replace("-","")+".mp4";
+        String fame =  UUID.randomUUID().toString().replace("-","");
+        String fileName =  fame+".mp4";
+        String imgName =  fame+".jpg";
         try {
             List<String> voidTS = new ArrayList<>();
 
@@ -85,7 +87,7 @@ public class VideoUtil {
                 String format = "%s -y -i %s -vcodec copy -bsf:v h264_mp4toannexb -f mpegts %s";
                 String name = UUID.randomUUID().toString().replace("-","");
                 String command = String.format(format, "ffmpeg", fromVideoFile, newVideoFile +"/"+name + ".ts");
-                CreateObjUtil.callshell(command);
+                ShellUtil.execCmd(command);
                 voidTS.add(newVideoFile +"/"+name + ".ts");
             }
             StringBuilder tsPath = new StringBuilder();
@@ -106,7 +108,8 @@ public class VideoUtil {
             tsPath.append(" -movflags ");
             tsPath.append(" +faststart ");
             tsPath.append(newVideoFile).append("/").append(fileName);
-            CreateObjUtil.callshell(tsPath.toString());
+            ShellUtil.execCmd(tsPath.toString());
+            ffmpegVideo(newVideoFile +"/"+ fileName,newVideoFile +"/"+ imgName,"200","200");
             //删除生成的ts文件
             for (String filePath : voidTS) {
                 File file = new File(filePath);
@@ -120,5 +123,23 @@ public class VideoUtil {
         }
     }
 
+    /**
+     *
+     * @param sourceFile 原文件路径
+     * @param thumbFile 目标文件路径
+     * @param thumbWidth 宽度
+     * @param thumbHigh 高度
+     * @return
+     * ffmpeg -i bb.mp4 -y -vframes 1 -vf scale=100:100/a thumb.jpg
+     */
+    public static boolean ffmpegVideo(String sourceFile, String thumbFile, String thumbWidth, String thumbHigh){
+        String cmd = " ffmpeg -i " + sourceFile + " -y -vframes 1 -vf scale=" + thumbWidth + ":" + thumbHigh + "/a " + thumbFile;
+        ShellUtil.execCmd(cmd);
+        File file = new File(thumbFile);
+        if(!file.exists()){
+            return false;
+        }
+        return true;
+    }
 
 }

+ 3 - 0
src/main/java/com/fdkankan/fusion/service/impl/CaseVideoServiceImpl.java

@@ -106,10 +106,13 @@ public class CaseVideoServiceImpl extends ServiceImpl<ICaseVideoMapper, CaseVide
         String mergeLocalPath =  FilePath.VIDEO_LOCAL_PATH +"/"+mergeLocalName;
         String ossKey =  String.format(FilePath.VIDEO_OSS_PATH,videoFolder.getVideoFolderId()) +"/"+mergeLocalName;
         uploadToOssUtil.upload(mergeLocalPath,ossKey);
+        uploadToOssUtil.upload(mergeLocalPath,ossKey.replace(".mp4",".jpg"));
+
         videoFolder.setVideoMergeUrl(queryPath + ossKey);
         videoFolder.setVideoFolderCover(null);
         videoFolderService.updateById(videoFolder);
         FileUtil.del(mergeLocalPath);
+        FileUtil.del(mergeLocalPath.replace(".mp4",".jpg"));
     }