add subtitle and add sticker api

This commit is contained in:
sun-guannan
2025-07-13 16:21:17 +08:00
parent 4eed1e1487
commit 8174d84100
2 changed files with 63 additions and 63 deletions

View File

@@ -10,63 +10,63 @@ def add_subtitle_impl(
draft_id: str = None,
track_name: str = "subtitle",
time_offset: float = 0,
# 字体样式参数
# Font style parameters
font_size: float = 8.0,
bold: bool = False,
italic: bool = False,
underline: bool = False,
font_color: str = "#FFFFFF",
# 描边参数
# Border parameters
border_alpha: float = 1.0,
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_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_resource_id: Optional[str] = None,
# 文本花字
# Text effect
effect_effect_id: Optional[str] = None,
# 图像调节参数
# Image adjustment parameters
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_y: float = 1.0,
rotation: float = 0.0,
style_reference: draft.Text_segment = None,
vertical: bool = True, # 新增参数:是否垂直显示
vertical: bool = True, # New parameter: whether to display vertically
alpha: float = 0.4,
width: int = 1080, # 新增参数
height: int = 1920 # 新增参数
width: int = 1080, # New parameter
height: int = 1920 # New parameter
):
"""
向草稿添加字幕
:param srt_path: 字幕文件路径或URL或SRT文本内容
:param draft_id: 草稿ID如果为None则创建新草稿
:param track_name: 轨道名称,默认为"subtitle"
:param time_offset: 时间偏移量,默认为"0"s
:param text_style: 文本样式,默认为None
:param clip_settings: 片段设置,默认为None
:param style_reference: 样式参考,默认为None
:return: 草稿信息
Add subtitles to draft
:param srt_path: Subtitle file path or URL or SRT text content
:param draft_id: Draft ID, if None, create a new draft
:param track_name: Track name, default is "subtitle"
:param time_offset: Time offset, default is "0"s
:param text_style: Text style, default is None
:param clip_settings: Clip settings, default is None
:param style_reference: Style reference, default is None
:return: Draft information
"""
# 获取或创建草稿
# Get or create draft
draft_id, script = get_or_create_draft(
draft_id=draft_id,
width=width,
height=height
)
# 处理字幕内容
# Process subtitle content
srt_content = None
# 检查是否为URL
# Check if it's a URL
if srt_path.startswith(('http://', 'https://')):
try:
response = requests.get(srt_path)
@@ -75,18 +75,18 @@ def add_subtitle_impl(
response.encoding = 'utf-8'
srt_content = response.text
except Exception as e:
raise Exception(f"下载字幕文件失败: {str(e)}")
raise Exception(f"Failed to download subtitle file: {str(e)}")
else:
# 如果不是URL直接使用内容
# If not a URL, use content directly
srt_content = srt_path
# 处理可能包含的转义字符
# Handle possible escape characters
srt_content = srt_content.replace('\\n', '\n').replace('/n', '\n')
# 导入字幕
# 转换十六进制颜色为RGB
# Import subtitles
# Convert hexadecimal color to RGB
rgb_color = hex_to_rgb(font_color)
# 创建text_border (描边)
# Create text_border
text_border = None
if border_width > 0:
text_border = draft.Text_border(
@@ -95,7 +95,7 @@ def add_subtitle_impl(
width=border_width
)
# 创建text_background (背景)
# Create text_background
text_background = None
if background_alpha > 0:
text_background = draft.Text_background(
@@ -104,19 +104,19 @@ def add_subtitle_impl(
alpha=background_alpha
)
# 创建text_style
# Create text_style
text_style = draft.Text_style(
size=font_size,
bold=bold,
italic=italic,
underline=underline,
color=rgb_color,
align=1, # 保持居中对齐
vertical=vertical, # 使用传入的vertical参数
alpha=alpha # 使用传入的alpha参数
align=1, # Keep center alignment
vertical=vertical, # Use the passed vertical parameter
alpha=alpha # Use the passed alpha parameter
)
# 创建气泡效果
# Create bubble effect
text_bubble = None
if bubble_effect_id and bubble_resource_id:
text_bubble = TextBubble(
@@ -124,7 +124,7 @@ def add_subtitle_impl(
resource_id=bubble_resource_id
)
# 创建花字效果
# Create text effect
text_effect = None
if effect_effect_id:
text_effect = TextEffect(
@@ -132,7 +132,7 @@ def add_subtitle_impl(
resource_id=effect_effect_id
)
# 创建clip_settings
# Create clip_settings
clip_settings = draft.Clip_settings(
transform_x=transform_x,
transform_y=transform_y,
@@ -144,7 +144,7 @@ def add_subtitle_impl(
script.import_srt(
srt_content,
track_name=track_name,
time_offset=int(time_offset * 1000000), # 将秒转换为微秒
time_offset=int(time_offset * 1000000), # Convert seconds to microseconds
text_style=text_style,
clip_settings=clip_settings,
style_reference=style_reference,