mirror of
https://github.com/Hommy-master/capcut-mate.git
synced 2026-08-28 23:27:50 +08:00
{
"draft_url": "https://capcut-mate.jcaigc.cn/openapi/capcut-mate/v1/get_draft?draft_id=20251206191116a4fdff66", "captions": "[{\"start\":0,\"end\":10000000,\"text\":\"你好,剪映\",\"keyword\":\"好\",\"keyword_color\":\"#457616\",\"keyword_font_size\":15}]", "alignment": 1, "alpha": 1.0, "font_size": 6, "scale_x": 1.0, "scale_y": 1.0, "transform_x": 200, "transform_y": 200, "style_text": false, "underline": false, "italic": false, "bold": false } 解决指定font_size不生效的问题。
This commit is contained in:
@@ -95,6 +95,13 @@ captions是一个JSON字符串,包含字幕数组,每个字幕对象包含
|
||||
| 4 | 垂直左对齐 |
|
||||
| 5 | 垂直右对齐 |
|
||||
|
||||
#### 字体大小参数
|
||||
|
||||
- **font_size**: 普通文本(非关键词)的字体大小
|
||||
- 默认值:15(仅在caption项中未指定font_size时生效)
|
||||
- 建议范围:8-72
|
||||
- 注意:如果在caption项中明确指定了font_size,则使用caption项中的值;如果未指定,则使用接口级别的font_size参数值
|
||||
|
||||
#### 缩放参数
|
||||
|
||||
- **scale_x**: 字幕的水平缩放比例
|
||||
|
||||
@@ -221,8 +221,14 @@ def add_caption_to_draft(
|
||||
elif alignment == 2:
|
||||
align_value = 2
|
||||
|
||||
# 根据需求修改:只有当caption中明确指定了font_size时才使用,否则不设置默认值
|
||||
font_size_value = font_size
|
||||
if 'font_size' in caption and caption['font_size'] is not None:
|
||||
font_size_value = float(caption['font_size'])
|
||||
|
||||
# 创建TextStyle对象
|
||||
text_style = TextStyle(
|
||||
size=float(caption.get('font_size', font_size)),
|
||||
size=font_size_value,
|
||||
color=rgb_color,
|
||||
alpha=alpha,
|
||||
align=align_value,
|
||||
@@ -233,6 +239,7 @@ def add_caption_to_draft(
|
||||
italic=italic,
|
||||
bold=bold
|
||||
)
|
||||
logger.info(f"Created text style, text_style.size: {text_style.size}, font_size from caption: {font_size}")
|
||||
|
||||
# 4. 创建文本描边(如果提供了border_color)
|
||||
text_border = None
|
||||
@@ -429,7 +436,7 @@ def parse_captions_data(json_str: str) -> List[Dict[str, Any]]:
|
||||
"keyword": item.get("keyword", None),
|
||||
"keyword_color": item.get("keyword_color", "#ff7100"),
|
||||
"keyword_font_size": item.get("keyword_font_size", 15),
|
||||
"font_size": item.get("font_size", 15),
|
||||
"font_size": item.get("font_size", None),
|
||||
"in_animation": item.get("in_animation", None),
|
||||
"out_animation": item.get("out_animation", None),
|
||||
"loop_animation": item.get("loop_animation", None),
|
||||
@@ -451,9 +458,6 @@ def parse_captions_data(json_str: str) -> List[Dict[str, Any]]:
|
||||
logger.error(f"the {i}th item has invalid text: {processed_item['text']}")
|
||||
raise CustomException(CustomError.INVALID_CAPTION_INFO, f"the {i}th item has invalid text")
|
||||
|
||||
# 验证字体大小
|
||||
if not isinstance(processed_item["font_size"], (int, float)) or processed_item["font_size"] <= 0:
|
||||
processed_item["font_size"] = 15
|
||||
if not isinstance(processed_item["keyword_font_size"], (int, float)) or processed_item["keyword_font_size"] <= 0:
|
||||
processed_item["keyword_font_size"] = 15
|
||||
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
import sys
|
||||
import os
|
||||
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
|
||||
|
||||
from src.pyJianYingDraft.text_segment import TextStyle, TextSegment
|
||||
from src.pyJianYingDraft.time_util import Timerange
|
||||
|
||||
def test_font_size():
|
||||
"""测试字体大小设置"""
|
||||
|
||||
# 创建一个文本样式,指定字体大小为20
|
||||
text_style = TextStyle(
|
||||
size=20.0,
|
||||
color=(1.0, 1.0, 1.0), # 白色
|
||||
alpha=1.0,
|
||||
align=1, # 居中
|
||||
auto_wrapping=True
|
||||
)
|
||||
|
||||
print(f"创建的TextStyle字体大小: {text_style.size}")
|
||||
|
||||
# 创建一个时间范围
|
||||
timerange = Timerange(start=0, duration=5000000) # 5秒
|
||||
|
||||
# 创建文本片段
|
||||
text_segment = TextSegment(
|
||||
text="测试字体大小",
|
||||
timerange=timerange,
|
||||
style=text_style
|
||||
)
|
||||
|
||||
print(f"TextSegment的style.size: {text_segment.style.size}")
|
||||
|
||||
# 导出材质并检查字体大小
|
||||
material_json = text_segment.export_material()
|
||||
content = material_json["content"]
|
||||
print(f"导出的content: {content}")
|
||||
|
||||
# 解析content JSON
|
||||
import json
|
||||
content_data = json.loads(content)
|
||||
styles = content_data["styles"]
|
||||
base_style = styles[0]
|
||||
print(f"基础样式中的字体大小: {base_style['size']}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_font_size()
|
||||
@@ -0,0 +1,36 @@
|
||||
def test_font_size_logic():
|
||||
"""测试字体大小逻辑"""
|
||||
|
||||
# 模拟caption数据
|
||||
caption = {
|
||||
"start": 0,
|
||||
"end": 5000000,
|
||||
"text": "测试字体大小"
|
||||
}
|
||||
|
||||
# 模拟函数参数
|
||||
font_size = 15 # add_captions函数的默认值
|
||||
|
||||
# 模拟add_caption_to_draft中的逻辑
|
||||
size = float(caption.get('font_size', font_size))
|
||||
|
||||
print(f"caption: {caption}")
|
||||
print(f"font_size参数: {font_size}")
|
||||
print(f"计算出的字体大小: {size}")
|
||||
|
||||
# 测试当caption中有font_size时的情况
|
||||
caption_with_font_size = {
|
||||
"start": 0,
|
||||
"end": 5000000,
|
||||
"text": "测试字体大小",
|
||||
"font_size": 20
|
||||
}
|
||||
|
||||
size2 = float(caption_with_font_size.get('font_size', font_size))
|
||||
|
||||
print(f"\ncaption_with_font_size: {caption_with_font_size}")
|
||||
print(f"font_size参数: {font_size}")
|
||||
print(f"计算出的字体大小: {size2}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_font_size_logic()
|
||||
@@ -0,0 +1,48 @@
|
||||
import sys
|
||||
import os
|
||||
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
|
||||
|
||||
def test_font_size_logic():
|
||||
"""测试字体大小逻辑"""
|
||||
|
||||
# 模拟caption数据,不指定font_size
|
||||
caption_without_font_size = {
|
||||
"start": 0,
|
||||
"end": 5000000,
|
||||
"text": "测试字体大小"
|
||||
}
|
||||
|
||||
# 模拟函数参数
|
||||
font_size = 20 # add_captions函数的默认值
|
||||
|
||||
# 模拟add_caption_to_draft中的逻辑
|
||||
# 使用一个特殊值来表示未设置字体大小
|
||||
FONT_SIZE_NOT_SET = -1.0
|
||||
font_size_value = FONT_SIZE_NOT_SET
|
||||
if 'font_size' in caption_without_font_size and caption_without_font_size['font_size'] is not None:
|
||||
font_size_value = float(caption_without_font_size['font_size'])
|
||||
|
||||
print(f"caption_without_font_size: {caption_without_font_size}")
|
||||
print(f"font_size参数: {font_size}")
|
||||
print(f"计算出的字体大小值: {font_size_value}")
|
||||
print(f"最终使用的字体大小: {font_size_value if font_size_value != FONT_SIZE_NOT_SET else 8.0}")
|
||||
|
||||
# 测试当caption中有font_size时的情况
|
||||
caption_with_font_size = {
|
||||
"start": 0,
|
||||
"end": 5000000,
|
||||
"text": "测试字体大小",
|
||||
"font_size": 25
|
||||
}
|
||||
|
||||
font_size_value2 = FONT_SIZE_NOT_SET
|
||||
if 'font_size' in caption_with_font_size and caption_with_font_size['font_size'] is not None:
|
||||
font_size_value2 = float(caption_with_font_size['font_size'])
|
||||
|
||||
print(f"\ncaption_with_font_size: {caption_with_font_size}")
|
||||
print(f"font_size参数: {font_size}")
|
||||
print(f"计算出的字体大小值: {font_size_value2}")
|
||||
print(f"最终使用的字体大小: {font_size_value2}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_font_size_logic()
|
||||
@@ -0,0 +1,66 @@
|
||||
import sys
|
||||
import os
|
||||
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
|
||||
|
||||
from src.service.add_captions import add_caption_to_draft
|
||||
from src.pyJianYingDraft import ScriptFile
|
||||
|
||||
def test_font_size_not_set():
|
||||
"""测试未设置字体大小的情况"""
|
||||
|
||||
# 创建一个模拟的草稿文件对象
|
||||
script = ScriptFile()
|
||||
|
||||
# 创建一个字幕项,不指定font_size
|
||||
caption = {
|
||||
"start": 0,
|
||||
"end": 5000000, # 5秒
|
||||
"text": "测试未设置字体大小"
|
||||
}
|
||||
|
||||
try:
|
||||
# 调用add_caption_to_draft函数
|
||||
segment_id, text_id, segment_info = add_caption_to_draft(
|
||||
script=script,
|
||||
track_name="test_track",
|
||||
caption=caption,
|
||||
text_color="#ffffff",
|
||||
font_size=20 # 这是接口级别的默认值
|
||||
)
|
||||
|
||||
print(f"成功添加字幕,segment_id: {segment_id}")
|
||||
print(f"text_id: {text_id}")
|
||||
print(f"segment_info: {segment_info}")
|
||||
|
||||
# 检查导出的材质
|
||||
# 获取刚刚添加的文本片段
|
||||
text_segment = None
|
||||
for segment in script.segments.values():
|
||||
if segment.segment_id == segment_id:
|
||||
text_segment = segment
|
||||
break
|
||||
|
||||
if text_segment:
|
||||
material_json = text_segment.export_material()
|
||||
print(f"导出的材质: {material_json}")
|
||||
|
||||
# 检查content中的size字段
|
||||
import json
|
||||
content_data = json.loads(material_json["content"])
|
||||
styles = content_data["styles"]
|
||||
base_style = styles[0]
|
||||
|
||||
if "size" in base_style:
|
||||
print(f"基础样式中的字体大小: {base_style['size']}")
|
||||
else:
|
||||
print("基础样式中没有size字段,表示未设置字体大小")
|
||||
else:
|
||||
print("未找到添加的文本片段")
|
||||
|
||||
except Exception as e:
|
||||
print(f"添加字幕时出错: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_font_size_not_set()
|
||||
Reference in New Issue
Block a user