mirror of
https://github.com/Hommy-master/capcut-mate.git
synced 2026-08-31 02:03:16 +08:00
Merge pull request #34 from Hommy-master/dev
优化imgs_infos和captions_infos接口
This commit is contained in:
@@ -42,7 +42,7 @@ def caption_infos(
|
||||
str: JSON字符串格式的字幕信息
|
||||
|
||||
Raises:
|
||||
ValueError: 当texts和timelines长度不匹配时,或者keywords长度与texts不匹配时
|
||||
ValueError: 当texts和timelines长度不匹配时
|
||||
"""
|
||||
logger.info(f"caption_infos called with {len(texts)} texts and {len(timelines)} timelines")
|
||||
|
||||
@@ -50,57 +50,15 @@ def caption_infos(
|
||||
if len(texts) != len(timelines):
|
||||
raise ValueError(f"texts length ({len(texts)}) does not match timelines length ({len(timelines)})")
|
||||
|
||||
# 检查keywords长度是否匹配
|
||||
if keywords is not None and len(keywords) != len(texts):
|
||||
raise ValueError(f"keywords length ({len(keywords)}) does not match texts length ({len(texts)})")
|
||||
|
||||
# 构建字幕信息列表
|
||||
infos = []
|
||||
for i, (text, timeline) in enumerate(zip(texts, timelines)):
|
||||
info = {
|
||||
"start": timeline["start"],
|
||||
"end": timeline["end"],
|
||||
"text": text
|
||||
}
|
||||
|
||||
# 添加关键词信息
|
||||
if keywords is not None:
|
||||
info["keyword"] = keywords[i]
|
||||
|
||||
# 添加可选参数
|
||||
if keyword_color is not None:
|
||||
info["keyword_color"] = keyword_color
|
||||
|
||||
if keyword_font_size is not None:
|
||||
info["keyword_font_size"] = keyword_font_size
|
||||
|
||||
if font_size is not None:
|
||||
info["font_size"] = font_size
|
||||
|
||||
if in_animation is not None:
|
||||
info["in_animation"] = in_animation
|
||||
|
||||
if in_animation_duration is not None:
|
||||
info["in_animation_duration"] = in_animation_duration
|
||||
|
||||
if loop_animation is not None:
|
||||
info["loop_animation"] = loop_animation
|
||||
|
||||
if loop_animation_duration is not None:
|
||||
info["loop_animation_duration"] = loop_animation_duration
|
||||
|
||||
if out_animation is not None:
|
||||
info["out_animation"] = out_animation
|
||||
|
||||
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_caption_info(text, timeline, i, keywords,
|
||||
font_size, keyword_color, keyword_font_size,
|
||||
in_animation, in_animation_duration,
|
||||
loop_animation, loop_animation_duration,
|
||||
out_animation, out_animation_duration,
|
||||
transition, transition_duration)
|
||||
infos.append(info)
|
||||
logger.info(f"Processed caption info {i+1}: {info}")
|
||||
|
||||
@@ -108,4 +66,60 @@ def caption_infos(
|
||||
infos_json = json.dumps(infos, ensure_ascii=False)
|
||||
logger.info(f"Generated caption infos JSON with {len(infos)} items")
|
||||
|
||||
return infos_json
|
||||
return infos_json
|
||||
|
||||
|
||||
def _build_caption_info(text, timeline, index, keywords,
|
||||
font_size, keyword_color, keyword_font_size,
|
||||
in_animation, in_animation_duration,
|
||||
loop_animation, loop_animation_duration,
|
||||
out_animation, out_animation_duration,
|
||||
transition, transition_duration):
|
||||
"""构建单个字幕信息字典"""
|
||||
info = {
|
||||
"start": timeline["start"],
|
||||
"end": timeline["end"],
|
||||
"text": text
|
||||
}
|
||||
|
||||
# 添加关键词信息,如果keywords存在且有足够的关键词,则按索引分配,否则循环分配
|
||||
if keywords is not None and len(keywords) > 0:
|
||||
keyword = keywords[index % len(keywords)] # 循环使用关键词
|
||||
info["keyword"] = keyword
|
||||
|
||||
# 添加可选参数
|
||||
if keyword_color is not None:
|
||||
info["keyword_color"] = keyword_color
|
||||
|
||||
if keyword_font_size is not None:
|
||||
info["keyword_font_size"] = keyword_font_size
|
||||
|
||||
if font_size is not None:
|
||||
info["font_size"] = font_size
|
||||
|
||||
# 添加动画参数
|
||||
if in_animation is not None:
|
||||
info["in_animation"] = in_animation
|
||||
|
||||
if in_animation_duration is not None:
|
||||
info["in_animation_duration"] = in_animation_duration
|
||||
|
||||
if loop_animation is not None:
|
||||
info["loop_animation"] = loop_animation
|
||||
|
||||
if loop_animation_duration is not None:
|
||||
info["loop_animation_duration"] = loop_animation_duration
|
||||
|
||||
if out_animation is not None:
|
||||
info["out_animation"] = out_animation
|
||||
|
||||
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
|
||||
|
||||
return info
|
||||
|
||||
+60
-38
@@ -46,46 +46,17 @@ def imgs_infos(
|
||||
if len(imgs) != len(timelines):
|
||||
raise ValueError(f"imgs length ({len(imgs)}) does not match timelines length ({len(timelines)})")
|
||||
|
||||
# 解析动画参数
|
||||
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_animation is not None:
|
||||
info["in_animation"] = in_animation
|
||||
|
||||
if in_animation_duration is not None:
|
||||
info["in_animation_duration"] = in_animation_duration
|
||||
|
||||
if loop_animation is not None:
|
||||
info["loop_animation"] = loop_animation
|
||||
|
||||
if loop_animation_duration is not None:
|
||||
info["loop_animation_duration"] = loop_animation_duration
|
||||
|
||||
if out_animation is not None:
|
||||
info["out_animation"] = out_animation
|
||||
|
||||
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}")
|
||||
|
||||
@@ -93,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