feat(editor): from-assets支持同素材多片段+随机起始时间+去重+required_clips_count #1512
Reference in New Issue
Block a user
Delete Branch "fix/editor-clips-random-start-time"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
问题
create_clips_from_assets_editor接口存在多个问题:_calc_random_start_timeused_segments追踪已使用区间order有 bug(使用了不存在的body.start_order,hasattr 永远 False,回退到 i)修复
Schema
ClipsFromAssetsRequest新增required_clips_count: Optional[int]端点逻辑
asset_repo.get()获取实际 durationasset_ids[i % len(asset_ids)],素材不足时同一素材切多个片段_calc_random_start_time,used_segments追踪已用区间clip_duration = min(5.0, asset_total)测试(7个)
test_creates_exactly_required_clips_count:2素材×4片段,验证轮询分配test_orders_are_sequential:order 0,1,2 连续test_backward_compatible_default_count:不传参数时片段数=素材数test_clip_duration_shortened_for_short_assets:3s素材→duration=3.0test_start_time_passed_to_create_clip:验证随机start_time传入test_asset_durations_fetched_from_db:验证从DB获取时长test_used_segments_passed_to_calc:验证used_segments逐步累积fix(editor): from-assets端点接入随机起始时间+去重+素材时长自适应to feat(editor): from-assets支持同素材多片段+随机起始时间+去重+required_clips_count🚀 预览环境已部署
代码审查结果 - PR #1512
⚠️ 问题(2个需要修改)
apps/api/app/api/routes/templates_editor/clips.py 第373-387行:并发竞态条件导致数据冲突
list_clips计算最大order,随后在循环中使用order=next_order + i创建片段。在高并发场景下,两个请求可能同时读取到相同的最大order,导致创建的片段order重复(主键冲突或业务逻辑混乱)。SELECT ... FOR UPDATE锁定 plan 记录)或利用数据库的自增/序列机制来保证order的唯一性和连续性,避免在应用层计算。apps/api/app/api/routes/templates_editor/clips.py 第357-397行:随机去重逻辑遗漏已存在片段
used_segments仅用于记录本次请求中新生成的片段时间段,未包含existing_clips_list中已存在的片段。如果素材已被用于创建片段,再次调用该接口时,新计算的随机start_time可能与旧片段的时间段重叠。used_segments时,应先遍历existing_clips_list,将已存在的同素材片段时间段填入used_segments,确保全局去重。💡 建议(2个可选)
apps/api/app/api/routes/templates_editor/clips.py 第373行:性能优化建议
plan_svc.list_clips(plan_id)会拉取该 plan 下的所有片段对象,仅为了计算max(order)。当片段数量较多时,这会造成不必要的内存和查询开销。建议改为直接执行SELECT MAX(order) FROM ...查询。apps/api/app/api/routes/templates_editor/clips.py 第159、273、303行:代码可读性建议
❌ 格式检查通过 | ❌ 逻辑审查需修改 | ⚠️ 建议关注性能
🤖 由 AI 代码审查机器人自动生成 | 2026-08-26 14:13:52 | 模型:
🗑️ 预览环境已清理
PR #1512 已关闭或合并,对应的预览环境已被清理。