mirror of
https://github.com/Hommy-master/capcut-mate.git
synced 2026-09-01 15:32:57 +08:00
优化imgs_infos接口。
This commit is contained in:
+59
-51
@@ -46,60 +46,17 @@ def imgs_infos(
|
||||
if len(imgs) != len(timelines):
|
||||
raise ValueError(f"imgs length ({len(imgs)}) does not match timelines length ({len(timelines)})")
|
||||
|
||||
# 处理可能包含多个动画的参数,用 | 分隔
|
||||
in_animations = []
|
||||
if in_animation and isinstance(in_animation, str):
|
||||
in_animations = [anim.strip() for anim in in_animation.split('|') if anim.strip()]
|
||||
|
||||
out_animations = []
|
||||
if out_animation and isinstance(out_animation, str):
|
||||
out_animations = [anim.strip() for anim in out_animation.split('|') if anim.strip()]
|
||||
|
||||
loop_animations = []
|
||||
if loop_animation and isinstance(loop_animation, str):
|
||||
loop_animations = [anim.strip() for anim in loop_animation.split('|') if anim.strip()]
|
||||
# 解析动画参数
|
||||
parsed_animations = _parse_animation_params(in_animation, out_animation, loop_animation, transition)
|
||||
in_animations, out_animations, loop_animations, transition_animations = parsed_animations
|
||||
|
||||
# 构建图片信息列表
|
||||
infos = []
|
||||
for i, (img_url, timeline) in enumerate(zip(imgs, timelines)):
|
||||
info = {
|
||||
"image_url": img_url,
|
||||
"start": timeline["start"],
|
||||
"end": timeline["end"]
|
||||
}
|
||||
|
||||
# 添加可选参数
|
||||
if height is not None:
|
||||
info["height"] = height
|
||||
|
||||
if width is not None:
|
||||
info["width"] = width
|
||||
|
||||
# 循环分配动画,如果动画列表为空则跳过
|
||||
if in_animations:
|
||||
info["in_animation"] = in_animations[i % len(in_animations)]
|
||||
|
||||
if in_animation_duration is not None:
|
||||
info["in_animation_duration"] = in_animation_duration
|
||||
|
||||
if loop_animations:
|
||||
info["loop_animation"] = loop_animations[i % len(loop_animations)]
|
||||
|
||||
if loop_animation_duration is not None:
|
||||
info["loop_animation_duration"] = loop_animation_duration
|
||||
|
||||
if out_animations:
|
||||
info["out_animation"] = out_animations[i % len(out_animations)]
|
||||
|
||||
if out_animation_duration is not None:
|
||||
info["out_animation_duration"] = out_animation_duration
|
||||
|
||||
if transition is not None:
|
||||
info["transition"] = transition
|
||||
|
||||
if transition_duration is not None:
|
||||
info["transition_duration"] = transition_duration
|
||||
|
||||
info = _build_image_info(img_url, timeline, height, width, i,
|
||||
in_animations, out_animations, loop_animations, transition_animations,
|
||||
in_animation_duration, out_animation_duration,
|
||||
loop_animation_duration, transition_duration)
|
||||
infos.append(info)
|
||||
logger.info(f"Processed image info {i+1}: {info}")
|
||||
|
||||
@@ -107,4 +64,55 @@ def imgs_infos(
|
||||
infos_json = json.dumps(infos, ensure_ascii=False)
|
||||
logger.info(f"Generated image infos JSON with {len(infos)} items")
|
||||
|
||||
return infos_json
|
||||
return infos_json
|
||||
|
||||
|
||||
def _parse_animation_params(in_animation, out_animation, loop_animation, transition):
|
||||
"""解析动画参数,将用|分隔的字符串转换为列表"""
|
||||
def parse_single_param(param):
|
||||
if param is not None and isinstance(param, str):
|
||||
return [anim.strip() for anim in param.split('|') if anim.strip()]
|
||||
return []
|
||||
|
||||
in_animations = parse_single_param(in_animation)
|
||||
out_animations = parse_single_param(out_animation)
|
||||
loop_animations = parse_single_param(loop_animation)
|
||||
transition_animations = parse_single_param(transition)
|
||||
|
||||
return in_animations, out_animations, loop_animations, transition_animations
|
||||
|
||||
|
||||
def _build_image_info(img_url, timeline, height, width, i,
|
||||
in_animations, out_animations, loop_animations, transition_animations,
|
||||
in_animation_duration, out_animation_duration,
|
||||
loop_animation_duration, transition_duration):
|
||||
"""构建单个图片信息字典"""
|
||||
info = {
|
||||
"image_url": img_url,
|
||||
"start": timeline["start"],
|
||||
"end": timeline["end"]
|
||||
}
|
||||
|
||||
# 添加尺寸参数
|
||||
if height is not None:
|
||||
info["height"] = height
|
||||
if width is not None:
|
||||
info["width"] = width
|
||||
|
||||
# 添加动画参数,只有当动画存在时才添加对应的duration
|
||||
_add_animation_if_exists(info, "in_animation", in_animations, i, in_animation_duration)
|
||||
_add_animation_if_exists(info, "out_animation", out_animations, i, out_animation_duration)
|
||||
_add_animation_if_exists(info, "loop_animation", loop_animations, i, loop_animation_duration)
|
||||
_add_animation_if_exists(info, "transition", transition_animations, i, transition_duration)
|
||||
|
||||
return info
|
||||
|
||||
|
||||
def _add_animation_if_exists(info, animation_key, animations, index, duration):
|
||||
"""如果动画存在,则添加动画和对应的duration"""
|
||||
if animations and index < len(animations):
|
||||
info[animation_key] = animations[index]
|
||||
# 只有当动画存在时才添加对应的duration
|
||||
if duration is not None:
|
||||
duration_key = animation_key + "_duration"
|
||||
info[duration_key] = duration
|
||||
Reference in New Issue
Block a user