浏览代码

修改音频文件合成调用方式

syh 5 年之前
父节点
当前提交
01d7b398f5

+ 42 - 2
jeecg-boot-module-system/src/main/java/org/jeecg/modules/opencv/service/impl/FfmpegServiceImpl.java

@@ -103,7 +103,8 @@ public class FfmpegServiceImpl implements IFfmpegService {
         Runtime runtime = Runtime.getRuntime();
         Process pro = null;
         try {
-            pro = runtime.exec(command.toString());
+            pro = runtime.exec(new String[]{"sh", "-c", command.toString()});
+            printProcessMsg(pro);
             int status = pro.waitFor();
             if (status == 0) {
                 log.info("音频文件合成成功:{}", command.toString());
@@ -116,6 +117,44 @@ public class FfmpegServiceImpl implements IFfmpegService {
     }
 
     /**
+     * 处理process输出流和错误流,防止进程阻塞,在process.waitFor();前调用
+     *
+     * @param exec
+     * @throws IOException
+     */
+    private void printProcessMsg(Process exec) throws IOException {
+        //防止ffmpeg进程塞满缓存造成死锁
+        InputStream error = exec.getErrorStream();
+        InputStream is = exec.getInputStream();
+
+        StringBuffer result = new StringBuffer();
+        String line = null;
+        try {
+            BufferedReader br = new BufferedReader(new InputStreamReader(error));
+            BufferedReader br2 = new BufferedReader(new InputStreamReader(is));
+
+            while ((line = br.readLine()) != null) {
+                result.append(line + "\n");
+            }
+            log.info("FFMPEG视频转换进程错误信息:" + result.toString());
+
+            result = new StringBuffer();
+            line = null;
+
+            while ((line = br2.readLine()) != null) {
+                result.append(line + "\n");
+            }
+            log.info("FFMPEG视频转换进程输出内容为:" + result.toString());
+        } catch (IOException e2) {
+            e2.printStackTrace();
+        } finally {
+            error.close();
+            is.close();
+        }
+
+    }
+
+    /**
      * 音频+视频合成视频
      *
      * @param srcVideoPath
@@ -140,7 +179,8 @@ public class FfmpegServiceImpl implements IFfmpegService {
         Runtime runtime = Runtime.getRuntime();
         Process pro = null;
         try {
-            pro = runtime.exec(command.toString());
+            pro = runtime.exec(new String[]{"sh", "-c", command.toString()});
+            printProcessMsg(pro);
             int status = pro.waitFor();
             if (status == 0) {
                 log.info("视频合成成功:{}", command.toString());