mirror of
https://github.com/sun-guannan/CapCutAPI.git
synced 2025-11-25 03:15:00 +08:00
add subtitle and add sticker api
This commit is contained in:
@@ -24,44 +24,44 @@ def add_sticker_impl(
|
|||||||
height: int = 1920
|
height: int = 1920
|
||||||
) -> Dict[str, str]:
|
) -> Dict[str, str]:
|
||||||
"""
|
"""
|
||||||
向指定草稿添加贴纸
|
Add sticker to specified draft
|
||||||
:param resource_id: 贴纸资源ID
|
:param resource_id: Sticker resource ID
|
||||||
:param start: 开始时间(秒)
|
:param start: Start time (seconds)
|
||||||
:param end: 结束时间(秒)
|
:param end: End time (seconds)
|
||||||
:param draft_id: 草稿ID(可选,默认None则创建新草稿)
|
:param draft_id: Draft ID (optional, default None creates a new draft)
|
||||||
:param transform_y: Y轴位置(默认0,屏幕中间)
|
:param transform_y: Y-axis position (default 0, screen center)
|
||||||
:param transform_x: X轴位置(默认0,屏幕中间)
|
:param transform_x: X-axis position (default 0, screen center)
|
||||||
:param alpha: 图像不透明度,范围0-1(默认1.0,完全不透明)
|
:param alpha: Image opacity, range 0-1 (default 1.0, completely opaque)
|
||||||
:param flip_horizontal: 是否水平翻转(默认False)
|
:param flip_horizontal: Whether to flip horizontally (default False)
|
||||||
:param flip_vertical: 是否垂直翻转(默认False)
|
:param flip_vertical: Whether to flip vertically (default False)
|
||||||
:param rotation: 顺时针旋转的角度,可正可负(默认0.0)
|
:param rotation: Clockwise rotation angle, can be positive or negative (default 0.0)
|
||||||
:param scale_x: 水平缩放比例(默认1.0)
|
:param scale_x: Horizontal scale ratio (default 1.0)
|
||||||
:param scale_y: 垂直缩放比例(默认1.0)
|
:param scale_y: Vertical scale ratio (default 1.0)
|
||||||
:param track_name: 轨道名称
|
:param track_name: Track name
|
||||||
:param relative_index: 相对(同类型轨道的)图层位置,越高越接近前景(默认0)
|
:param relative_index: Relative layer position (of the same track type), higher is closer to foreground (default 0)
|
||||||
:param width: 视频宽度,默认1080
|
:param width: Video width, default 1080
|
||||||
:param height: 视频高度,默认1920
|
:param height: Video height, default 1920
|
||||||
:return: 更新后的草稿信息
|
:return: Updated draft information
|
||||||
"""
|
"""
|
||||||
# 获取或创建草稿
|
# Get or create draft
|
||||||
draft_id, script = get_or_create_draft(
|
draft_id, script = get_or_create_draft(
|
||||||
draft_id=draft_id,
|
draft_id=draft_id,
|
||||||
width=width,
|
width=width,
|
||||||
height=height
|
height=height
|
||||||
)
|
)
|
||||||
|
|
||||||
# 添加贴纸轨道
|
# Add sticker track
|
||||||
if track_name is not None:
|
if track_name is not None:
|
||||||
try:
|
try:
|
||||||
imported_track = script.get_imported_track(draft.Track_type.sticker, name=track_name)
|
imported_track = script.get_imported_track(draft.Track_type.sticker, name=track_name)
|
||||||
# 如果没有抛出异常,说明轨道已存在
|
# If no exception is thrown, the track already exists
|
||||||
except exceptions.TrackNotFound:
|
except exceptions.TrackNotFound:
|
||||||
# 轨道不存在,创建新轨道
|
# Track doesn't exist, create a new track
|
||||||
script.add_track(draft.Track_type.sticker, track_name=track_name, relative_index=relative_index)
|
script.add_track(draft.Track_type.sticker, track_name=track_name, relative_index=relative_index)
|
||||||
else:
|
else:
|
||||||
script.add_track(draft.Track_type.sticker, relative_index=relative_index)
|
script.add_track(draft.Track_type.sticker, relative_index=relative_index)
|
||||||
|
|
||||||
# 创建贴纸片段
|
# Create sticker segment
|
||||||
sticker_segment = draft.Sticker_segment(
|
sticker_segment = draft.Sticker_segment(
|
||||||
resource_id,
|
resource_id,
|
||||||
trange(f"{start}s", f"{end-start}s"),
|
trange(f"{start}s", f"{end-start}s"),
|
||||||
@@ -77,7 +77,7 @@ def add_sticker_impl(
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
# 添加贴纸片段到轨道
|
# Add sticker segment to track
|
||||||
script.add_segment(sticker_segment, track_name=track_name)
|
script.add_segment(sticker_segment, track_name=track_name)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
@@ -10,63 +10,63 @@ def add_subtitle_impl(
|
|||||||
draft_id: str = None,
|
draft_id: str = None,
|
||||||
track_name: str = "subtitle",
|
track_name: str = "subtitle",
|
||||||
time_offset: float = 0,
|
time_offset: float = 0,
|
||||||
# 字体样式参数
|
# Font style parameters
|
||||||
font_size: float = 8.0,
|
font_size: float = 8.0,
|
||||||
bold: bool = False,
|
bold: bool = False,
|
||||||
italic: bool = False,
|
italic: bool = False,
|
||||||
underline: bool = False,
|
underline: bool = False,
|
||||||
font_color: str = "#FFFFFF",
|
font_color: str = "#FFFFFF",
|
||||||
|
|
||||||
# 描边参数
|
# Border parameters
|
||||||
border_alpha: float = 1.0,
|
border_alpha: float = 1.0,
|
||||||
border_color: str = "#000000",
|
border_color: str = "#000000",
|
||||||
border_width: float = 0.0, # 默认不显示描边
|
border_width: float = 0.0, # Default no border display
|
||||||
|
|
||||||
# 背景参数
|
# Background parameters
|
||||||
background_color: str = "#000000",
|
background_color: str = "#000000",
|
||||||
background_style: int = 1,
|
background_style: int = 1,
|
||||||
background_alpha: float = 0.0, # 默认不显示背景
|
background_alpha: float = 0.0, # Default no background display
|
||||||
|
|
||||||
# 气泡效果
|
# Bubble effect
|
||||||
bubble_effect_id: Optional[str] = None,
|
bubble_effect_id: Optional[str] = None,
|
||||||
bubble_resource_id: Optional[str] = None,
|
bubble_resource_id: Optional[str] = None,
|
||||||
|
|
||||||
# 文本花字
|
# Text effect
|
||||||
effect_effect_id: Optional[str] = None,
|
effect_effect_id: Optional[str] = None,
|
||||||
# 图像调节参数
|
# Image adjustment parameters
|
||||||
transform_x: float = 0.0,
|
transform_x: float = 0.0,
|
||||||
transform_y: float = -0.8, # 字幕默认位置在底部
|
transform_y: float = -0.8, # Default subtitle position at bottom
|
||||||
scale_x: float = 1.0,
|
scale_x: float = 1.0,
|
||||||
scale_y: float = 1.0,
|
scale_y: float = 1.0,
|
||||||
rotation: float = 0.0,
|
rotation: float = 0.0,
|
||||||
style_reference: draft.Text_segment = None,
|
style_reference: draft.Text_segment = None,
|
||||||
vertical: bool = True, # 新增参数:是否垂直显示
|
vertical: bool = True, # New parameter: whether to display vertically
|
||||||
alpha: float = 0.4,
|
alpha: float = 0.4,
|
||||||
width: int = 1080, # 新增参数
|
width: int = 1080, # New parameter
|
||||||
height: int = 1920 # 新增参数
|
height: int = 1920 # New parameter
|
||||||
):
|
):
|
||||||
"""
|
"""
|
||||||
向草稿添加字幕
|
Add subtitles to draft
|
||||||
:param srt_path: 字幕文件路径或URL或SRT文本内容
|
:param srt_path: Subtitle file path or URL or SRT text content
|
||||||
:param draft_id: 草稿ID,如果为None则创建新草稿
|
:param draft_id: Draft ID, if None, create a new draft
|
||||||
:param track_name: 轨道名称,默认为"subtitle"
|
:param track_name: Track name, default is "subtitle"
|
||||||
:param time_offset: 时间偏移量,默认为"0"s
|
:param time_offset: Time offset, default is "0"s
|
||||||
:param text_style: 文本样式,默认为None
|
:param text_style: Text style, default is None
|
||||||
:param clip_settings: 片段设置,默认为None
|
:param clip_settings: Clip settings, default is None
|
||||||
:param style_reference: 样式参考,默认为None
|
:param style_reference: Style reference, default is None
|
||||||
:return: 草稿信息
|
:return: Draft information
|
||||||
"""
|
"""
|
||||||
# 获取或创建草稿
|
# Get or create draft
|
||||||
draft_id, script = get_or_create_draft(
|
draft_id, script = get_or_create_draft(
|
||||||
draft_id=draft_id,
|
draft_id=draft_id,
|
||||||
width=width,
|
width=width,
|
||||||
height=height
|
height=height
|
||||||
)
|
)
|
||||||
|
|
||||||
# 处理字幕内容
|
# Process subtitle content
|
||||||
srt_content = None
|
srt_content = None
|
||||||
|
|
||||||
# 检查是否为URL
|
# Check if it's a URL
|
||||||
if srt_path.startswith(('http://', 'https://')):
|
if srt_path.startswith(('http://', 'https://')):
|
||||||
try:
|
try:
|
||||||
response = requests.get(srt_path)
|
response = requests.get(srt_path)
|
||||||
@@ -75,18 +75,18 @@ def add_subtitle_impl(
|
|||||||
response.encoding = 'utf-8'
|
response.encoding = 'utf-8'
|
||||||
srt_content = response.text
|
srt_content = response.text
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise Exception(f"下载字幕文件失败: {str(e)}")
|
raise Exception(f"Failed to download subtitle file: {str(e)}")
|
||||||
else:
|
else:
|
||||||
# 如果不是URL,直接使用内容
|
# If not a URL, use content directly
|
||||||
srt_content = srt_path
|
srt_content = srt_path
|
||||||
# 处理可能包含的转义字符
|
# Handle possible escape characters
|
||||||
srt_content = srt_content.replace('\\n', '\n').replace('/n', '\n')
|
srt_content = srt_content.replace('\\n', '\n').replace('/n', '\n')
|
||||||
|
|
||||||
# 导入字幕
|
# Import subtitles
|
||||||
# 转换十六进制颜色为RGB
|
# Convert hexadecimal color to RGB
|
||||||
rgb_color = hex_to_rgb(font_color)
|
rgb_color = hex_to_rgb(font_color)
|
||||||
|
|
||||||
# 创建text_border (描边)
|
# Create text_border
|
||||||
text_border = None
|
text_border = None
|
||||||
if border_width > 0:
|
if border_width > 0:
|
||||||
text_border = draft.Text_border(
|
text_border = draft.Text_border(
|
||||||
@@ -95,7 +95,7 @@ def add_subtitle_impl(
|
|||||||
width=border_width
|
width=border_width
|
||||||
)
|
)
|
||||||
|
|
||||||
# 创建text_background (背景)
|
# Create text_background
|
||||||
text_background = None
|
text_background = None
|
||||||
if background_alpha > 0:
|
if background_alpha > 0:
|
||||||
text_background = draft.Text_background(
|
text_background = draft.Text_background(
|
||||||
@@ -104,19 +104,19 @@ def add_subtitle_impl(
|
|||||||
alpha=background_alpha
|
alpha=background_alpha
|
||||||
)
|
)
|
||||||
|
|
||||||
# 创建text_style
|
# Create text_style
|
||||||
text_style = draft.Text_style(
|
text_style = draft.Text_style(
|
||||||
size=font_size,
|
size=font_size,
|
||||||
bold=bold,
|
bold=bold,
|
||||||
italic=italic,
|
italic=italic,
|
||||||
underline=underline,
|
underline=underline,
|
||||||
color=rgb_color,
|
color=rgb_color,
|
||||||
align=1, # 保持居中对齐
|
align=1, # Keep center alignment
|
||||||
vertical=vertical, # 使用传入的vertical参数
|
vertical=vertical, # Use the passed vertical parameter
|
||||||
alpha=alpha # 使用传入的alpha参数
|
alpha=alpha # Use the passed alpha parameter
|
||||||
)
|
)
|
||||||
|
|
||||||
# 创建气泡效果
|
# Create bubble effect
|
||||||
text_bubble = None
|
text_bubble = None
|
||||||
if bubble_effect_id and bubble_resource_id:
|
if bubble_effect_id and bubble_resource_id:
|
||||||
text_bubble = TextBubble(
|
text_bubble = TextBubble(
|
||||||
@@ -124,7 +124,7 @@ def add_subtitle_impl(
|
|||||||
resource_id=bubble_resource_id
|
resource_id=bubble_resource_id
|
||||||
)
|
)
|
||||||
|
|
||||||
# 创建花字效果
|
# Create text effect
|
||||||
text_effect = None
|
text_effect = None
|
||||||
if effect_effect_id:
|
if effect_effect_id:
|
||||||
text_effect = TextEffect(
|
text_effect = TextEffect(
|
||||||
@@ -132,7 +132,7 @@ def add_subtitle_impl(
|
|||||||
resource_id=effect_effect_id
|
resource_id=effect_effect_id
|
||||||
)
|
)
|
||||||
|
|
||||||
# 创建clip_settings
|
# Create clip_settings
|
||||||
clip_settings = draft.Clip_settings(
|
clip_settings = draft.Clip_settings(
|
||||||
transform_x=transform_x,
|
transform_x=transform_x,
|
||||||
transform_y=transform_y,
|
transform_y=transform_y,
|
||||||
@@ -144,7 +144,7 @@ def add_subtitle_impl(
|
|||||||
script.import_srt(
|
script.import_srt(
|
||||||
srt_content,
|
srt_content,
|
||||||
track_name=track_name,
|
track_name=track_name,
|
||||||
time_offset=int(time_offset * 1000000), # 将秒转换为微秒
|
time_offset=int(time_offset * 1000000), # Convert seconds to microseconds
|
||||||
text_style=text_style,
|
text_style=text_style,
|
||||||
clip_settings=clip_settings,
|
clip_settings=clip_settings,
|
||||||
style_reference=style_reference,
|
style_reference=style_reference,
|
||||||
|
|||||||
Reference in New Issue
Block a user