diff --git a/apps/api/app/api/routes/templates_editor/clips.py b/apps/api/app/api/routes/templates_editor/clips.py index 41a05f6f1..515e3eaa4 100755 --- a/apps/api/app/api/routes/templates_editor/clips.py +++ b/apps/api/app/api/routes/templates_editor/clips.py @@ -46,6 +46,9 @@ from .schemas import ( logger = logging.getLogger(__name__) router = APIRouter(tags=["Template Editor"]) +# 编辑器默认片段时长(秒) +_DEFAULT_EDITOR_CLIP_DURATION = 5.0 + def _clip_to_response(clip, asset_url: str | None = None) -> EditorClipResponse: """统一构造片段响应 — 与 edit_plan_clips 表字段完全对齐""" @@ -368,8 +371,7 @@ def create_clips_from_assets_editor( """ _, plan_svc = services - _DEFAULT_CLIP_DURATION = 5.0 - required_count = body.required_clips_count if body.required_clips_count else len(body.asset_ids) + required_count = body.required_clips_count if body.required_clips_count is not None else len(body.asset_ids) # 去重后批量获取素材实际时长,避免重复查询 unique_asset_ids = list(dict.fromkeys(body.asset_ids)) @@ -380,8 +382,9 @@ def create_clips_from_assets_editor( asset_durations[asset_id] = float(asset.duration or 0.0) # 计算追加起始 order:当前 plan 已有片段的最大 order + 1 - existing_clips = plan_svc.list_clips(plan_id) - next_order = max((c.order for c in existing_clips), default=-1) + 1 + # list_clips 返回 List[EditPlanClip] + existing_clips_list = plan_svc.list_clips(plan_id) + next_order = max((c.order for c in existing_clips_list), default=-1) + 1 used_segments: dict[str, list[tuple[float, float]]] = {} clips = [] @@ -393,9 +396,9 @@ def create_clips_from_assets_editor( # 素材时长不足时缩短 clip duration asset_total = asset_durations.get(asset_id, 0.0) if asset_total > 0: - clip_duration = min(_DEFAULT_CLIP_DURATION, asset_total) + clip_duration = min(_DEFAULT_EDITOR_CLIP_DURATION, asset_total) else: - clip_duration = _DEFAULT_CLIP_DURATION + clip_duration = _DEFAULT_EDITOR_CLIP_DURATION # 计算随机 start_time,避开已使用的时间段 start_time = _calc_random_start_time(asset_id, clip_duration, asset_durations, used_segments)