mirror of
https://github.com/Hommy-master/capcut-mate.git
synced 2026-08-30 17:48:47 +08:00
添加图片时,如果没有给默认高,宽,则使用默认值。
This commit is contained in:
+30
-15
@@ -35,8 +35,8 @@ def add_images(
|
||||
[
|
||||
{
|
||||
"image_url": "https://s.coze.cn/t/XpufYwc2_u4/", // [必选] 图片文件URL
|
||||
"width": 1024, // [必选] 图片宽度(像素)
|
||||
"height": 1024, // [必选] 图片高度(像素)
|
||||
"width": 1920, // [可选] 图片宽度(像素),不传则使用草稿画布尺寸
|
||||
"height": 1080, // [可选] 图片高度(像素),不传则使用草稿画布尺寸
|
||||
"start": 0, // [必选] 显示开始时间(微秒)
|
||||
"end": 1000000, // [必选] 显示结束时间(微秒)
|
||||
"in_animation": "", // [可选] 入场动画类型
|
||||
@@ -289,8 +289,8 @@ def add_image_to_draft(
|
||||
draft_image_dir: 图片资源目录
|
||||
image: 图片信息字典,包含以下字段:
|
||||
image_url: 图片URL
|
||||
width: 图片宽度(像素)
|
||||
height: 图片高度(像素)
|
||||
width: 图片宽度(像素,可选,不传则使用草稿画布尺寸)
|
||||
height: 图片高度(像素,可选,不传则使用草稿画布尺寸)
|
||||
start: 显示开始时间(微秒)
|
||||
end: 显示结束时间(微秒)
|
||||
in_animation: 入场动画类型(可选)
|
||||
@@ -330,7 +330,12 @@ def add_image_to_draft(
|
||||
# 获取草稿的宽高用于transform坐标转换
|
||||
draft_width = script.width
|
||||
draft_height = script.height
|
||||
logger.info(f"draft size: {draft_width}x{draft_height}, image size: {image['width']}x{image['height']}, transform_x: {transform_x}, transform_y: {transform_y}")
|
||||
image_width = int(image.get("width") or draft_width)
|
||||
image_height = int(image.get("height") or draft_height)
|
||||
logger.info(
|
||||
f"draft size: {draft_width}x{draft_height}, image size: {image_width}x{image_height}, "
|
||||
f"transform_x: {transform_x}, transform_y: {transform_y}"
|
||||
)
|
||||
|
||||
# 创建图像调节设置
|
||||
clip_settings = draft.ClipSettings(
|
||||
@@ -424,7 +429,7 @@ def add_image_to_draft(
|
||||
logger.warning(f"Failed to add transition '{image['transition']}': {str(e)}")
|
||||
|
||||
logger.info(f"Created image segment, material_id: {video_segment.material_instance.material_id}")
|
||||
logger.info(f"Image segment details - start: {image['start']}, duration: {segment_duration}, size: {image['width']}x{image['height']}")
|
||||
logger.info(f"Image segment details - start: {image['start']}, duration: {segment_duration}, size: {image_width}x{image_height}")
|
||||
|
||||
# 5. 向指定轨道添加片段
|
||||
script.add_segment(video_segment, track_name)
|
||||
@@ -497,8 +502,8 @@ def parse_image_data(json_str: str) -> List[Dict[str, Any]]:
|
||||
[
|
||||
{
|
||||
"image_url": "https://s.coze.cn/t/XpufYwc2_u4/", // [必选] 图片文件URL
|
||||
"width": 1024, // [必选] 图片宽度(像素)
|
||||
"height": 1024, // [必选] 图片高度(像素)
|
||||
"width": 1920, // [可选] 图片宽度(像素),不传则使用草稿画布尺寸
|
||||
"height": 1080, // [可选] 图片高度(像素),不传则使用草稿画布尺寸
|
||||
"start": 0, // [必选] 显示开始时间(微秒)
|
||||
"end": 1000000, // [必选] 显示结束时间(微秒)
|
||||
"in_animation": "", // [可选] 入场动画类型
|
||||
@@ -538,19 +543,25 @@ def parse_image_data(json_str: str) -> List[Dict[str, Any]]:
|
||||
logger.error(f"The {i}th item should be a dict")
|
||||
raise CustomException(CustomError.INVALID_IMAGE_INFO, f"the {i}th item should be a dict")
|
||||
|
||||
# 检查必选字段
|
||||
required_fields = ["image_url", "width", "height", "start", "end"]
|
||||
# 检查必选字段(width/height 可选,未传时在 add_image_to_draft 中使用草稿画布尺寸)
|
||||
required_fields = ["image_url", "start", "end"]
|
||||
missing_fields = [field for field in required_fields if field not in item]
|
||||
|
||||
if missing_fields:
|
||||
logger.error(f"The {i}th item is missing required fields: {', '.join(missing_fields)}")
|
||||
raise CustomException(CustomError.INVALID_IMAGE_INFO, f"the {i}th item is missing required fields: {', '.join(missing_fields)}")
|
||||
|
||||
width = item.get("width")
|
||||
height = item.get("height")
|
||||
if width is not None:
|
||||
width = int(width)
|
||||
if height is not None:
|
||||
height = int(height)
|
||||
# 创建处理后的对象,设置默认值
|
||||
processed_item = {
|
||||
"image_url": item["image_url"],
|
||||
"width": int(item["width"]),
|
||||
"height": int(item["height"]),
|
||||
"width": width,
|
||||
"height": height,
|
||||
"start": int(item["start"]),
|
||||
"end": int(item["end"]),
|
||||
"in_animation": item.get("in_animation", None), # 默认无入场动画
|
||||
@@ -563,9 +574,13 @@ def parse_image_data(json_str: str) -> List[Dict[str, Any]]:
|
||||
"transition_duration": item.get("transition_duration", 500000) # 默认转场时长500000微秒
|
||||
}
|
||||
|
||||
# 验证数值范围
|
||||
if processed_item["width"] <= 0 or processed_item["height"] <= 0:
|
||||
logger.error(f"Invalid image dimensions: width={processed_item['width']}, height={processed_item['height']}")
|
||||
# 验证数值范围(仅校验显式传入的尺寸)
|
||||
if (processed_item["width"] is not None and processed_item["width"] <= 0) or (
|
||||
processed_item["height"] is not None and processed_item["height"] <= 0
|
||||
):
|
||||
logger.error(
|
||||
f"Invalid image dimensions: width={processed_item['width']}, height={processed_item['height']}"
|
||||
)
|
||||
raise CustomException(CustomError.INVALID_IMAGE_INFO, f"the {i}th item has invalid image dimensions")
|
||||
|
||||
if processed_item["start"] < 0 or processed_item["end"] <= processed_item["start"]:
|
||||
|
||||
Reference in New Issue
Block a user