feat: 优化

This commit is contained in:
2026-03-02 01:32:02 +08:00
parent ce3d529f80
commit 668515a451
4 changed files with 60 additions and 54 deletions

View File

@@ -216,39 +216,32 @@ public class BatchProduceAlignment {
*/
private Map<String, Integer> calculateCropParams(int sourceWidth, int sourceHeight, String cropMode) {
Map<String, Integer> cropParams = new HashMap<>();
double targetRatio = 9.0 / 16.0; // 9:16竖屏比例
// 填充模式:不裁剪,保持原尺寸
if ("fill".equals(cropMode)) {
// 填充模式:不裁剪,保持原尺寸
cropParams.put("X", 0);
cropParams.put("Y", 0);
cropParams.put("Width", sourceWidth);
cropParams.put("Height", sourceHeight);
} else if ("smart".equals(cropMode)) {
// 智能裁剪功能暂未开放,自动降级为居中裁剪
log.info("[裁剪模式] smart模式暂未开放自动降级为center模式");
double cropHeight = sourceHeight;
double cropWidth = cropHeight * targetRatio;
int cropX = (int) Math.round((sourceWidth - cropWidth) / 2);
int cropY = 0;
cropParams.put("X", cropX);
cropParams.put("Y", cropY);
cropParams.put("Width", (int) Math.round(cropWidth));
cropParams.put("Height", (int) Math.round(cropHeight));
} else {
// center模式居中裁剪默认
double cropHeight = sourceHeight;
double cropWidth = cropHeight * targetRatio;
int cropX = (int) Math.round((sourceWidth - cropWidth) / 2);
int cropY = 0;
cropParams.put("X", cropX);
cropParams.put("Y", cropY);
cropParams.put("Width", (int) Math.round(cropWidth));
cropParams.put("Height", (int) Math.round(cropHeight));
log.debug("[裁剪计算] 源尺寸={}x{}, 模式=fill, 裁剪参数={}", sourceWidth, sourceHeight, cropParams);
return cropParams;
}
// center/smart模式居中裁剪smart暂未开放降级为center
if ("smart".equals(cropMode)) {
log.info("[裁剪模式] smart模式暂未开放自动降级为center模式");
}
double targetRatio = 9.0 / 16.0; // 9:16竖屏比例
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;
}

View File

@@ -589,6 +589,11 @@ public class MixTaskServiceImpl implements MixTaskService {
}
}
private static final int MIN_TOTAL_DURATION = 15;
private static final int MAX_TOTAL_DURATION = 30;
private static final int MIN_SEGMENT_DURATION = 3;
private static final int MAX_SEGMENT_DURATION = 5;
/**
* 验证新格式场景配置
*/
@@ -618,9 +623,7 @@ public class MixTaskServiceImpl implements MixTaskService {
}
// 场景时长验证
if (scene.getDuration() < 3 || scene.getDuration() > 5) {
throw new IllegalArgumentException("场景" + i + "时长需在3-5秒之间当前" + scene.getDuration() + "");
}
validateSegmentDuration(scene.getDuration(), "场景" + i);
// 候选素材验证
for (int j = 0; j < scene.getCandidates().size(); j++) {
@@ -634,18 +637,11 @@ public class MixTaskServiceImpl implements MixTaskService {
}
}
// 3. 计算总时长
// 3. 计算并校验总时长
int totalDuration = req.getScenes().stream()
.mapToInt(MixTaskSaveReqVO.SceneConfig::getDuration)
.sum();
// 4. 总时长校验15s-30s
if (totalDuration < 15) {
throw new IllegalArgumentException("总时长不能小于15秒当前" + totalDuration + "");
}
if (totalDuration > 30) {
throw new IllegalArgumentException("总时长不能超过30秒当前" + totalDuration + "");
}
validateTotalDuration(totalDuration);
log.info("[MixTask][新格式场景校验通过] totalDuration={}s, sceneCount={}", totalDuration, req.getScenes().size());
}
@@ -664,21 +660,35 @@ public class MixTaskServiceImpl implements MixTaskService {
.mapToInt(MixTaskSaveReqVO.MaterialItem::getDuration)
.sum();
// 3. 总时长校验15s-30s
if (totalDuration < 15) {
throw new IllegalArgumentException("总时长不能小于15秒当前" + totalDuration + "");
}
if (totalDuration > 30) {
throw new IllegalArgumentException("总时长不能超过30秒当前" + totalDuration + "");
}
// 3. 校验总时长
validateTotalDuration(totalDuration);
// 4. 单个素材时长校验3s-5s
// 4. 单个素材时长校验
for (MixTaskSaveReqVO.MaterialItem item : req.getMaterials()) {
if (item.getDuration() < 3 || item.getDuration() > 5) {
throw new IllegalArgumentException("单个素材时长需在3-5秒之间当前" + item.getDuration() + "");
}
validateSegmentDuration(item.getDuration(), "素材");
}
log.info("[MixTask][旧格式素材校验通过] totalDuration={}s, materialCount={}", totalDuration, req.getMaterials().size());
}
/**
* 校验总时长
*/
private void validateTotalDuration(int totalDuration) {
if (totalDuration < MIN_TOTAL_DURATION) {
throw new IllegalArgumentException("总时长不能小于" + MIN_TOTAL_DURATION + "秒,当前:" + totalDuration + "");
}
if (totalDuration > MAX_TOTAL_DURATION) {
throw new IllegalArgumentException("总时长不能超过" + MAX_TOTAL_DURATION + "秒,当前:" + totalDuration + "");
}
}
/**
* 校验单个片段时长
*/
private void validateSegmentDuration(int duration, String context) {
if (duration < MIN_SEGMENT_DURATION || duration > MAX_SEGMENT_DURATION) {
throw new IllegalArgumentException(context + "时长需在" + MIN_SEGMENT_DURATION + "-" + MAX_SEGMENT_DURATION + "秒之间,当前:" + duration + "");
}
}
}