From 63a8f453cdd9d161fda1399256ce968154b3c394 Mon Sep 17 00:00:00 2001 From: shenrongliang <1328040932@qq.com> Date: 星期四, 17 四月 2025 14:46:02 +0800 Subject: [PATCH] 异步处理合成片头片尾 --- yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/framework/file/core/utils/FileTypeUtils.java | 28 ++++++++++++- yudao-module-digitalcourse/yudao-module-digitalcourse-biz/src/main/java/cn/iocoder/yudao/module/digitalcourse/service/coursemedia/CourseMediaServiceUtil.java | 48 +++++++++++++++++------- 2 files changed, 59 insertions(+), 17 deletions(-) diff --git a/yudao-module-digitalcourse/yudao-module-digitalcourse-biz/src/main/java/cn/iocoder/yudao/module/digitalcourse/service/coursemedia/CourseMediaServiceUtil.java b/yudao-module-digitalcourse/yudao-module-digitalcourse-biz/src/main/java/cn/iocoder/yudao/module/digitalcourse/service/coursemedia/CourseMediaServiceUtil.java index 99bf6b9..85a90bd 100644 --- a/yudao-module-digitalcourse/yudao-module-digitalcourse-biz/src/main/java/cn/iocoder/yudao/module/digitalcourse/service/coursemedia/CourseMediaServiceUtil.java +++ b/yudao-module-digitalcourse/yudao-module-digitalcourse-biz/src/main/java/cn/iocoder/yudao/module/digitalcourse/service/coursemedia/CourseMediaServiceUtil.java @@ -43,6 +43,7 @@ import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.*; +import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; @@ -696,7 +697,7 @@ } @Async - public CommonResult createCompositeVideo(CourseMediaSubtitlesReqVO courseMediaSubtitlesReqVO) { + public void createCompositeVideo(CourseMediaSubtitlesReqVO courseMediaSubtitlesReqVO) { //鐗囧ご鍦板潃 String titles = courseMediaSubtitlesReqVO.getTitles(); titles = configApi.getConfigValueByKey("easegen.url") + titles.substring(titles.lastIndexOf("/")); @@ -734,30 +735,49 @@ //鍘绘帀updateReqVO.getName()涓殑绌烘牸鍜岀壒娈婂瓧绗� String newFileName = courseMediaSubtitlesReqVO.getCourseName().replaceAll("[\\s\\p{Punct}]", ""); ProcessBuilder builder = new ProcessBuilder( - "ffmpeg", "-f", "concat", "-safe","0", "-i",fileListPath , "-c", "copy", configApi.getConfigValueByKey(HEYGEM_FACE2FACE)+"/compositeVideo/"+newFileName+".mp4" -// q + "ffmpeg", "-f", "concat", "-safe", "0", "-i", fileListPath, "-c", "copy", + configApi.getConfigValueByKey(HEYGEM_FACE2FACE) + "/compositeVideo/" + newFileName + ".mp4" ); builder.redirectErrorStream(true); Process process = null; try { process = builder.start(); - // 璇诲彇 FFmpeg 杈撳嚭锛堝彲閫夛級 - BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); - System.out.println(builder.command()); - System.out.println("鏈�缁堣棰戝凡鐢熸垚"); - } catch (IOException e) { - throw new RuntimeException(e); + // 浣跨敤 try-with-resources 纭繚娴佸叧闂� + try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) { + String line; + while ((line = reader.readLine()) != null) { + System.out.println(line); + } + } + + // 绛夊緟 FFmpeg 杩涚▼瀹屾垚 + int exitCode = process.waitFor(); + if (exitCode != 0) { + throw new RuntimeException("FFmpeg 鎵ц澶辫触锛岄��鍑虹爜锛�" + exitCode); + } + + System.out.println("鏈�缁堣棰戝凡鐢熸垚"); + } catch (IOException | InterruptedException e) { + throw new RuntimeException("FFmpeg 鎵ц寮傚父", e); + } finally { + // 纭繚 Process 鐨勮緭鍏�/閿欒娴佽鍏抽棴 + if (process != null) { + try { + process.getInputStream().close(); + process.getErrorStream().close(); + process.getOutputStream().close(); + } catch (IOException e) { + e.printStackTrace(); + } + } } byte[] bytes = FileUtil.readBytes(FileUtil.file(configApi.getConfigValueByKey(HEYGEM_FACE2FACE) +"/compositeVideo/"+newFileName+".mp4")); String compositeVideo = fileApi.createFile(bytes); CourseMediaDO courseMediaDO = new CourseMediaDO(); courseMediaDO.setId(courseMediaSubtitlesReqVO.getId()); courseMediaDO.setCompositeVideo(compositeVideo); - int i = courseMediaMapper.updateById(courseMediaDO); - if (i>0){ - return CommonResult.success("瑙嗛鍚堟垚鎴愬姛"); - } - return CommonResult.error(BAD_REQUEST.getCode(),"瑙嗛鍚堟垚澶辫触"); + courseMediaMapper.updateById(courseMediaDO); + System.out.println(); } } diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/framework/file/core/utils/FileTypeUtils.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/framework/file/core/utils/FileTypeUtils.java index 8970c00..925c525 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/framework/file/core/utils/FileTypeUtils.java +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/framework/file/core/utils/FileTypeUtils.java @@ -8,6 +8,7 @@ import org.apache.tika.Tika; import java.io.IOException; +import java.io.OutputStream; import java.net.URLEncoder; /** @@ -65,12 +66,33 @@ response.setContentType(contentType); // 閽堝 video 鐨勭壒娈婂鐞嗭紝瑙e喅瑙嗛鍦板潃鍦ㄧЩ鍔ㄧ鎾斁鐨勫吋瀹规�ч棶棰� if (StrUtil.containsIgnoreCase(contentType, "video")) { - response.setHeader("Content-Length", String.valueOf(content.length - 1)); - response.setHeader("Content-Range", String.valueOf(content.length - 1)); + long contentLength = content.length; + response.setHeader("Content-Length", String.valueOf(contentLength)); + response.setHeader("Content-Range", "bytes 0-" + (contentLength - 1) + "/" + contentLength); response.setHeader("Accept-Ranges", "bytes"); } // 杈撳嚭闄勪欢 - IoUtil.write(response.getOutputStream(), false, content); + OutputStream outputStream = null; + try { + outputStream = response.getOutputStream(); + IoUtil.write(outputStream, false, content); + } catch (IOException e) { + if ("Connection reset by peer".equals(e.getMessage())) { + System.out.println("瀹㈡埛绔腑鏂繛鎺�: {}"); + System.out.println(e.getMessage()); + } else { + throw e; + } + } finally { + if (outputStream != null) { + try { + outputStream.close(); + } catch (IOException e) { + System.out.println("鍏抽棴杈撳嚭娴佸け璐�: {}"); + System.out.println(e.getMessage()); + } + } + } } } -- Gitblit v1.9.3