fix: template

This commit is contained in:
root 2025-07-12 23:54:20 +08:00
parent bfe9f1e425
commit 326c0178b8
1 changed files with 80 additions and 23 deletions

View File

@ -175,28 +175,39 @@ def get_template_detail(
# 处理轨道信息
tracks_data = draft_content.get('tracks', [])
materials_data = draft_content.get('materials', [])
materials_data = draft_content.get('materials', {})
# 确保 tracks_data 和 materials_data 是列表
# 确保 tracks_data 是列表
if not isinstance(tracks_data, list):
logger.warning(f"tracks_data is not a list: {type(tracks_data)}")
tracks_data = []
if not isinstance(materials_data, list):
logger.warning(f"materials_data is not a list: {type(materials_data)}")
materials_data = []
# 创建素材查找表
# 创建素材查找表 - 处理真实的 draft_content 结构
materials_lookup = {}
for material in materials_data:
# 确保 material 是字典
if not isinstance(material, dict):
logger.warning(f"material is not a dict: {type(material)}")
continue
material_id = material.get('id', '')
if material_id:
materials_lookup[material_id] = material
# 如果 materials_data 是字典(真实结构),遍历各个类型
if isinstance(materials_data, dict):
for material_type in ['videos', 'audios', 'images', 'texts', 'stickers']:
material_list = materials_data.get(material_type, [])
if isinstance(material_list, list):
for material in material_list:
if isinstance(material, dict):
material_id = material.get('id', '')
if material_id:
# 添加类型信息
material_with_type = material.copy()
material_with_type['material_type'] = material_type
materials_lookup[material_id] = material_with_type
# 如果 materials_data 是列表(简化结构),直接处理
elif isinstance(materials_data, list):
for material in materials_data:
if isinstance(material, dict):
material_id = material.get('id', '')
if material_id:
materials_lookup[material_id] = material
else:
logger.warning(f"materials_data is neither dict nor list: {type(materials_data)}")
# 处理轨道
for idx, track_data in enumerate(tracks_data):
@ -228,23 +239,69 @@ def get_template_detail(
logger.warning(f"segment_data is not a dict: {type(segment_data)}")
continue
# 获取时间信息
start_time = segment_data.get('start', 0) / 1000.0 # 转换为秒
end_time = segment_data.get('end', 0) / 1000.0
duration = end_time - start_time
# 获取时间信息 - 真实的 draft_content 使用 target_timerange单位是微秒
target_timerange = segment_data.get('target_timerange', {})
if target_timerange:
# 使用真实的 draft_content 结构
start_time = target_timerange.get('start', 0) / 1000000.0 # 微秒转换为秒
duration_us = target_timerange.get('duration', 0)
duration = duration_us / 1000000.0 # 微秒转换为秒
end_time = start_time + duration
else:
# 兼容简化的结构(毫秒)
start_time = segment_data.get('start', 0) / 1000.0 # 毫秒转换为秒
end_time = segment_data.get('end', 0) / 1000.0
duration = end_time - start_time
# 获取素材信息
# 获取素材信息 - 真实的 draft_content 使用 material_id
material_id = segment_data.get('material_id', '')
material = materials_lookup.get(material_id, {})
# 如果没有找到素材,尝试从 materials 中查找对应的素材
if not material and material_id:
# 在 materials 的各个类别中查找
for material_type in ['videos', 'audios', 'images', 'texts', 'stickers']:
material_list = draft_content.get('materials', {}).get(material_type, [])
for mat in material_list:
if isinstance(mat, dict) and mat.get('id') == material_id:
material = mat
break
if material:
break
# 确定片段类型和名称
segment_type = 'video' # 默认类型
segment_name = f'片段 {len(track["segments"]) + 1}'
resource_path = ''
if material:
# 从素材获取信息
material_type = material.get('material_type', material.get('type', 'video'))
if material_type == 'videos':
segment_type = 'video'
elif material_type == 'audios':
segment_type = 'audio'
elif material_type == 'images':
segment_type = 'image'
elif material_type == 'texts':
segment_type = 'text'
elif material_type == 'stickers':
segment_type = 'sticker'
else:
segment_type = material_type
# 获取素材名称和路径
segment_name = material.get('material_name', material.get('name', segment_name))
resource_path = material.get('path', '')
segment = {
'id': segment_data.get('id', f'segment_{len(track["segments"])}'),
'type': segment_data.get('type', material.get('type', 'video')),
'name': segment_data.get('name', material.get('name', f'片段 {len(track["segments"]) + 1}')),
'type': segment_type,
'name': segment_name,
'start_time': start_time,
'end_time': end_time,
'duration': duration,
'resource_path': material.get('path', ''),
'resource_path': resource_path,
'properties': segment_data.get('properties', {}),
'effects': segment_data.get('effects', [])
}