refactor(tik): 使用 ICE 原生 AdaptMode 替换自定义裁剪逻辑
Some checks failed
Build and Deploy / deploy (push) Has been cancelled

- 移除手动计算裁剪参数的 `calculateCropParams` 方法
- 利用 ICE 的 `AdaptMode` 属性实现素材自适应,支持 `Cover`(裁切填充)和 `Fill`(拉伸填充)模式
- 简化代码并提高对不同分辨率素材的兼容性
This commit is contained in:
2026-06-04 22:21:23 +08:00
parent 2ea88316d2
commit 0224c8b3e3

View File

@@ -177,19 +177,19 @@ public class BatchProduceAlignment {
videoClip.put("TimelineIn", timelinePos); videoClip.put("TimelineIn", timelinePos);
videoClip.put("TimelineOut", timelinePos + actualDuration); videoClip.put("TimelineOut", timelinePos + actualDuration);
// 裁剪效果 // 使用 ICE 原生 AdaptMode 自动适配不同分辨率的素材
if (cropMode != null && !"fill".equals(cropMode)) { // Cover: 保持宽高比裁剪填满(无黑边,等同于之前的 center 模式)
Map<String, Integer> cropParams = calculateCropParams(1920, 1080, cropMode); // Fill: 拉伸填充(等同于之前的 fill 模式)
JSONArray effects = new JSONArray(); // 必须同时设置 Width 和 HeightAdaptMode 才会生效
JSONObject cropEffect = new JSONObject(); videoClip.put("Width", 720);
cropEffect.put("Type", "Crop"); videoClip.put("Height", 1280);
cropEffect.put("X", cropParams.get("X")); if ("fill".equals(cropMode)) {
cropEffect.put("Y", cropParams.get("Y")); videoClip.put("AdaptMode", "Fill");
cropEffect.put("Width", cropParams.get("Width")); } else {
cropEffect.put("Height", cropParams.get("Height")); // center / smart 均使用 Cover保持宽高比裁剪填满无黑边
effects.add(cropEffect); videoClip.put("AdaptMode", "Cover");
videoClip.put("Effects", effects);
} }
log.debug("[ICE][自适应模式] cropMode={}, AdaptMode={}", cropMode, videoClip.get("AdaptMode"));
videoClipArray.add(videoClip); videoClipArray.add(videoClip);
@@ -259,32 +259,4 @@ public class BatchProduceAlignment {
return mixDirectory + "/" + dateDir + "/" + targetFileName + ".mp4"; return mixDirectory + "/" + dateDir + "/" + targetFileName + ".mp4";
} }
private Map<String, Integer> calculateCropParams(int sourceWidth, int sourceHeight, String cropMode) {
Map<String, Integer> 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;
}
} }