From c18501202d4274b6d256ddce7295108c57715882 Mon Sep 17 00:00:00 2001 From: shenrongliang <1328040932@qq.com> Date: 星期四, 17 四月 2025 15:22:06 +0800 Subject: [PATCH] 异步处理合成片头片尾 --- yudao-module-digitalcourse/yudao-module-digitalcourse-biz/src/main/java/cn/iocoder/yudao/module/digitalcourse/service/coursemedia/CourseMediaServiceImpl.java | 94 ++++++++++++++++++++++ yudao-module-digitalcourse/yudao-module-digitalcourse-biz/src/main/java/cn/iocoder/yudao/module/digitalcourse/service/coursemedia/CourseMediaServiceUtil.java | 100 +----------------------- 2 files changed, 97 insertions(+), 97 deletions(-) diff --git a/yudao-module-digitalcourse/yudao-module-digitalcourse-biz/src/main/java/cn/iocoder/yudao/module/digitalcourse/service/coursemedia/CourseMediaServiceImpl.java b/yudao-module-digitalcourse/yudao-module-digitalcourse-biz/src/main/java/cn/iocoder/yudao/module/digitalcourse/service/coursemedia/CourseMediaServiceImpl.java index 9263772..c52a960 100644 --- a/yudao-module-digitalcourse/yudao-module-digitalcourse-biz/src/main/java/cn/iocoder/yudao/module/digitalcourse/service/coursemedia/CourseMediaServiceImpl.java +++ b/yudao-module-digitalcourse/yudao-module-digitalcourse-biz/src/main/java/cn/iocoder/yudao/module/digitalcourse/service/coursemedia/CourseMediaServiceImpl.java @@ -23,8 +23,10 @@ import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; import java.io.*; +import java.text.SimpleDateFormat; import java.time.LocalDateTime; import java.util.ArrayList; +import java.util.Date; import java.util.List; import static cn.iocoder.yudao.module.digitalcourse.enums.ErrorCodeConstants.COURSE_MEDIA_NOT_EXISTS; @@ -254,8 +256,94 @@ */ @Override public CommonResult createCompositeVideo(CourseMediaSubtitlesReqVO courseMediaSubtitlesReqVO) { - //寮傛鍚堟垚 - courseMediaServiceUtil.createCompositeVideo(courseMediaSubtitlesReqVO); - return CommonResult.success("瑙嗛鍚堟垚涓紝璇风◢鍚庢煡鐪�"); + // 鐢熸垚鏃堕棿鎴� + String timestamp = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()); + //鐗囧ご鍦板潃 + String titles = courseMediaSubtitlesReqVO.getTitles(); + titles = configApi.getConfigValueByKey("easegen.url") + titles.substring(titles.lastIndexOf("/")); + //鐗囧熬鍦板潃 + String trailer = courseMediaSubtitlesReqVO.getTrailer(); + trailer = configApi.getConfigValueByKey("easegen.url") + trailer.substring(trailer.lastIndexOf("/")); + String videoUrl = courseMediaSubtitlesReqVO.getVideoUrl(); + String previewUrl = courseMediaSubtitlesReqVO.getPreviewUrl(); + List<String> videoUrls = new ArrayList<>(); + videoUrls.add(titles); + if (videoUrl != null){ + videoUrl = configApi.getConfigValueByKey("easegen.url") + videoUrl.substring(videoUrl.lastIndexOf("/")); + videoUrls.add(videoUrl); + videoUrls.add(trailer); + } else if (previewUrl != null) { + previewUrl = configApi.getConfigValueByKey("easegen.url") + previewUrl.substring(previewUrl.lastIndexOf("/")); + videoUrls.add(previewUrl); + videoUrls.add(trailer); + } + //鍒ゆ柇鏂囦欢澶规槸鍚﹀瓨鍦紝濡傛灉涓嶅瓨鍦ㄥ氨鍒涘缓 + String filePath = configApi.getConfigValueByKey(HEYGEM_FACE2FACE) +"/compositeVideo/"; + File file = new File(filePath); + if (!file.exists()) { + file.mkdirs(); + } + String fileListPath = configApi.getConfigValueByKey(HEYGEM_FACE2FACE) +"/compositeVideo/"+timestamp+".txt"; + try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileListPath))) { + for (String path : videoUrls) { + writer.write("file '" + path + "'\n"); + } + System.out.println("鏂囦欢鍒楄〃宸茬敓鎴愶細" + fileListPath); + } catch (IOException e) { + e.printStackTrace(); + } + //鍘绘帀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/" + timestamp + ".mp4" + ); + builder.redirectErrorStream(true); + Process process = null; + try { + process = builder.start(); + + // 浣跨敤 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/"+timestamp+".mp4")); + String compositeVideo = fileApi.createFile(bytes); + CourseMediaDO courseMediaDO = new CourseMediaDO(); + courseMediaDO.setId(courseMediaSubtitlesReqVO.getId()); + courseMediaDO.setCompositeVideo(compositeVideo); + int i = courseMediaMapper.updateById(courseMediaDO); + FileUtil.del(configApi.getConfigValueByKey(HEYGEM_FACE2FACE) +"/compositeVideo/"+timestamp+".mp4"); + FileUtil.del(configApi.getConfigValueByKey(HEYGEM_FACE2FACE) +"/compositeVideo/"+timestamp+".txt"); + System.out.println(); + if (i>0){ + return CommonResult.success("鍚堟垚鎴愬姛"); + } + return CommonResult.error(BAD_REQUEST.getCode(),"鍚堟垚澶辫触"); } } 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 fa8259f..19403c3 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 @@ -171,14 +171,14 @@ builder = new ProcessBuilder( "ffmpeg", "-i", cover1, // 鑳屾櫙鍥� - "-i", substring1, // 瑙嗛 - "-i", cover, // PPT鍐呭 + "-i", cover, // PPT鍐呭 + "-i", substring1, // 浜哄儚瑙嗛 "-filter_complex", "[0:v]scale=" + Math.round(scene.getBackground().getWidth()) + ":" + Math.round(scene.getBackground().getHeight()) + "[bg];" + - "[1:v]scale=" + Math.round(scene.getComponents().get(0).getWidth()) + ":" + Math.round(scene.getComponents().get(0).getHeight()) + "[v1];" + - "[bg][v1]overlay=x=" + Math.round(scene.getComponents().get(0).getMarginLeft()) + ":y=" + Math.round(scene.getComponents().get(0).getTop()) + "[img];" + - "[2:v]scale=" + Math.round(scene.getComponents().get(1).getWidth()) + ":" + Math.round(scene.getComponents().get(1).getHeight()) + "[v2];" + - "[img][v2]overlay=x=" + Math.round(scene.getComponents().get(1).getMarginLeft()) + ":y=" + Math.round(scene.getComponents().get(1).getTop()), + "[1:v]scale=" + Math.round(scene.getComponents().get(1).getWidth()) + ":" + Math.round(scene.getComponents().get(1).getHeight()) + "[v1];" + + "[bg][v1]overlay=x=" + Math.round(scene.getComponents().get(1).getMarginLeft()) + ":y=" + Math.round(scene.getComponents().get(1).getTop()) + "[img];" + + "[2:v]scale=" + Math.round(scene.getComponents().get(0).getWidth()) + ":" + Math.round(scene.getComponents().get(0).getHeight()) + "[v2];" + + "[img][v2]overlay=x=" + Math.round(scene.getComponents().get(0).getMarginLeft()) + ":y=" + Math.round(scene.getComponents().get(0).getTop()), newFileName2 // 杈撳嚭鏂囦欢鍚� ); } @@ -696,92 +696,4 @@ } } - @Async - public void createCompositeVideo(CourseMediaSubtitlesReqVO courseMediaSubtitlesReqVO) { - // 鐢熸垚鏃堕棿鎴� - String timestamp = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()); - //鐗囧ご鍦板潃 - String titles = courseMediaSubtitlesReqVO.getTitles(); - titles = configApi.getConfigValueByKey("easegen.url") + titles.substring(titles.lastIndexOf("/")); - //鐗囧熬鍦板潃 - String trailer = courseMediaSubtitlesReqVO.getTrailer(); - trailer = configApi.getConfigValueByKey("easegen.url") + trailer.substring(trailer.lastIndexOf("/")); - String videoUrl = courseMediaSubtitlesReqVO.getVideoUrl(); - String previewUrl = courseMediaSubtitlesReqVO.getPreviewUrl(); - List<String> videoUrls = new ArrayList<>(); - videoUrls.add(titles); - if (videoUrl != null){ - videoUrl = configApi.getConfigValueByKey("easegen.url") + videoUrl.substring(videoUrl.lastIndexOf("/")); - videoUrls.add(videoUrl); - videoUrls.add(trailer); - } else if (previewUrl != null) { - previewUrl = configApi.getConfigValueByKey("easegen.url") + previewUrl.substring(previewUrl.lastIndexOf("/")); - videoUrls.add(previewUrl); - videoUrls.add(trailer); - } - //鍒ゆ柇鏂囦欢澶规槸鍚﹀瓨鍦紝濡傛灉涓嶅瓨鍦ㄥ氨鍒涘缓 - String filePath = configApi.getConfigValueByKey(HEYGEM_FACE2FACE) +"/compositeVideo/"; - File file = new File(filePath); - if (!file.exists()) { - file.mkdirs(); - } - String fileListPath = configApi.getConfigValueByKey(HEYGEM_FACE2FACE) +"/compositeVideo/"+timestamp+".txt"; - try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileListPath))) { - for (String path : videoUrls) { - writer.write("file '" + path + "'\n"); - } - System.out.println("鏂囦欢鍒楄〃宸茬敓鎴愶細" + fileListPath); - } catch (IOException e) { - e.printStackTrace(); - } - //鍘绘帀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/" + timestamp + ".mp4" - ); - builder.redirectErrorStream(true); - Process process = null; - try { - process = builder.start(); - - // 浣跨敤 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/"+timestamp+".mp4")); - String compositeVideo = fileApi.createFile(bytes); - CourseMediaDO courseMediaDO = new CourseMediaDO(); - courseMediaDO.setId(courseMediaSubtitlesReqVO.getId()); - courseMediaDO.setCompositeVideo(compositeVideo); - courseMediaMapper.updateById(courseMediaDO); - FileUtil.del(configApi.getConfigValueByKey(HEYGEM_FACE2FACE) +"/compositeVideo/"+timestamp+".mp4"); - FileUtil.del(configApi.getConfigValueByKey(HEYGEM_FACE2FACE) +"/compositeVideo/"+timestamp+".txt"); - System.out.println(); - } } -- Gitblit v1.9.3