From 0224c8b3e3e8cc91ee5336dba336f0aaf40cf0e8 Mon Sep 17 00:00:00 2001 From: sion123 <450702724@qq.com> Date: Thu, 4 Jun 2026 22:21:23 +0800 Subject: [PATCH] =?UTF-8?q?refactor(tik):=20=E4=BD=BF=E7=94=A8=20ICE=20?= =?UTF-8?q?=E5=8E=9F=E7=94=9F=20AdaptMode=20=E6=9B=BF=E6=8D=A2=E8=87=AA?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=E8=A3=81=E5=89=AA=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 移除手动计算裁剪参数的 `calculateCropParams` 方法 - 利用 ICE 的 `AdaptMode` 属性实现素材自适应,支持 `Cover`(裁切填充)和 `Fill`(拉伸填充)模式 - 简化代码并提高对不同分辨率素材的兼容性 --- .../tik/media/BatchProduceAlignment.java | 52 +++++-------------- 1 file changed, 12 insertions(+), 40 deletions(-) diff --git a/yudao-module-tik/src/main/java/cn/iocoder/yudao/module/tik/media/BatchProduceAlignment.java b/yudao-module-tik/src/main/java/cn/iocoder/yudao/module/tik/media/BatchProduceAlignment.java index ff7906d949..6a2b231c44 100644 --- a/yudao-module-tik/src/main/java/cn/iocoder/yudao/module/tik/media/BatchProduceAlignment.java +++ b/yudao-module-tik/src/main/java/cn/iocoder/yudao/module/tik/media/BatchProduceAlignment.java @@ -177,19 +177,19 @@ public class BatchProduceAlignment { videoClip.put("TimelineIn", timelinePos); videoClip.put("TimelineOut", timelinePos + actualDuration); - // 裁剪效果 - if (cropMode != null && !"fill".equals(cropMode)) { - Map cropParams = calculateCropParams(1920, 1080, cropMode); - JSONArray effects = new JSONArray(); - JSONObject cropEffect = new JSONObject(); - cropEffect.put("Type", "Crop"); - cropEffect.put("X", cropParams.get("X")); - cropEffect.put("Y", cropParams.get("Y")); - cropEffect.put("Width", cropParams.get("Width")); - cropEffect.put("Height", cropParams.get("Height")); - effects.add(cropEffect); - videoClip.put("Effects", effects); + // 使用 ICE 原生 AdaptMode 自动适配不同分辨率的素材 + // Cover: 保持宽高比裁剪填满(无黑边,等同于之前的 center 模式) + // Fill: 拉伸填充(等同于之前的 fill 模式) + // 必须同时设置 Width 和 Height,AdaptMode 才会生效 + videoClip.put("Width", 720); + videoClip.put("Height", 1280); + if ("fill".equals(cropMode)) { + videoClip.put("AdaptMode", "Fill"); + } else { + // center / smart 均使用 Cover,保持宽高比裁剪填满,无黑边 + videoClip.put("AdaptMode", "Cover"); } + log.debug("[ICE][自适应模式] cropMode={}, AdaptMode={}", cropMode, videoClip.get("AdaptMode")); videoClipArray.add(videoClip); @@ -259,32 +259,4 @@ public class BatchProduceAlignment { return mixDirectory + "/" + dateDir + "/" + targetFileName + ".mp4"; } - private Map calculateCropParams(int sourceWidth, int sourceHeight, String cropMode) { - Map cropParams = new HashMap<>(); - - if ("fill".equals(cropMode)) { - cropParams.put("X", 0); - cropParams.put("Y", 0); - cropParams.put("Width", sourceWidth); - cropParams.put("Height", sourceHeight); - return cropParams; - } - - if ("smart".equals(cropMode)) { - log.info("[裁剪模式] smart 模式暂未开放,降级为 center"); - } - - double targetRatio = 9.0 / 16.0; - double cropHeight = sourceHeight; - double cropWidth = cropHeight * targetRatio; - int cropX = (int) Math.round((sourceWidth - cropWidth) / 2); - - cropParams.put("X", cropX); - cropParams.put("Y", 0); - cropParams.put("Width", (int) Math.round(cropWidth)); - cropParams.put("Height", (int) Math.round(cropHeight)); - - log.debug("[裁剪计算] 源尺寸={}x{}, 模式={}, 参数={}", sourceWidth, sourceHeight, cropMode, cropParams); - return cropParams; - } }