refactor(tik): 使用 ICE 原生 AdaptMode 替换自定义裁剪逻辑
Some checks are pending
Build and Deploy / deploy (push) Waiting to run
Some checks are pending
Build and Deploy / deploy (push) Waiting to run
- 移除手动计算裁剪参数的 `calculateCropParams` 方法 - 利用 ICE 的 `AdaptMode` 属性实现素材自适应,支持 `Cover`(裁切填充)和 `Fill`(拉伸填充)模式 - 简化代码并提高对不同分辨率素材的兼容性
This commit is contained in:
@@ -177,19 +177,19 @@ public class BatchProduceAlignment {
|
||||
videoClip.put("TimelineIn", timelinePos);
|
||||
videoClip.put("TimelineOut", timelinePos + actualDuration);
|
||||
|
||||
// 裁剪效果
|
||||
if (cropMode != null && !"fill".equals(cropMode)) {
|
||||
Map<String, Integer> 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<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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user