mirror of
https://github.com/sun-guannan/CapCutAPI.git
synced 2025-11-25 19:37:48 +08:00
add_audio_track add_effect add_image api
This commit is contained in:
@@ -22,130 +22,130 @@ def add_audio_track(
|
||||
sound_effects: Optional[List[Tuple[str, Optional[List[Optional[float]]]]]]=None,
|
||||
width: int = 1080,
|
||||
height: int = 1920,
|
||||
duration: Optional[float] = None # 新增 duration 参数
|
||||
duration: Optional[float] = None # Added duration parameter
|
||||
) -> Dict[str, str]:
|
||||
"""
|
||||
向指定草稿添加音频轨道
|
||||
:param draft_folder: 草稿文件夹路径,可选参数
|
||||
:param audio_url: 音频URL
|
||||
:param start: 开始时间(秒),默认0
|
||||
:param end: 结束时间(秒),默认None(使用音频总时长)
|
||||
:param target_start: 目标轨道的插入位置(秒),默认0
|
||||
:param draft_id: 草稿ID,如果为None或找不到对应的zip文件,则创建新草稿
|
||||
:param volume: 音量大小,范围0.0-1.0,默认1.0
|
||||
:param track_name: 轨道名称,默认"audio_main"
|
||||
:param speed: 播放速度,默认1.0
|
||||
:param sound_effects: 场景音效果列表,每个元素是一个元组,包含效果类型名称和参数列表,默认None
|
||||
:return: 更新后的草稿信息
|
||||
Add an audio track to the specified draft
|
||||
:param draft_folder: Draft folder path, optional parameter
|
||||
:param audio_url: Audio URL
|
||||
:param start: Start time (seconds), default 0
|
||||
:param end: End time (seconds), default None (use total audio duration)
|
||||
:param target_start: Target track insertion position (seconds), default 0
|
||||
:param draft_id: Draft ID, if None or corresponding zip file not found, a new draft will be created
|
||||
:param volume: Volume level, range 0.0-1.0, default 1.0
|
||||
:param track_name: Track name, default "audio_main"
|
||||
:param speed: Playback speed, default 1.0
|
||||
:param sound_effects: Scene sound effect list, each element is a tuple containing effect type name and parameter list, default None
|
||||
:return: Updated draft information
|
||||
"""
|
||||
# 获取或创建草稿
|
||||
# Get or create draft
|
||||
draft_id, script = get_or_create_draft(
|
||||
draft_id=draft_id,
|
||||
width=width,
|
||||
height=height
|
||||
)
|
||||
|
||||
# 添加音频轨道(仅当轨道不存在时)
|
||||
# Add audio track (only when track doesn't exist)
|
||||
if track_name is not None:
|
||||
try:
|
||||
imported_track = script.get_imported_track(draft.Track_type.audio, name=track_name)
|
||||
# 如果没有抛出异常,说明轨道已存在
|
||||
# If no exception is thrown, the track already exists
|
||||
except exceptions.TrackNotFound:
|
||||
# 轨道不存在,创建新轨道
|
||||
# Track doesn't exist, create a new track
|
||||
script.add_track(draft.Track_type.audio, track_name=track_name)
|
||||
else:
|
||||
script.add_track(draft.Track_type.audio)
|
||||
|
||||
# 如果传递了 duration 参数,优先使用它;否则使用默认音频时长0秒,在下载时,才会获取真实时长
|
||||
# If duration parameter is provided, prioritize it; otherwise use default audio duration of 0 seconds, real duration will be obtained during download
|
||||
if duration is not None:
|
||||
# 使用传递进来的 duration,跳过时长获取和检查
|
||||
# Use the provided duration, skip duration retrieval and checking
|
||||
audio_duration = duration
|
||||
else:
|
||||
# 使用默认音频时长0秒,在下载草稿时,才会获取真实时长
|
||||
audio_duration = 0.0 # 默认音频时长为0秒
|
||||
# duration_result = get_video_duration(audio_url) # 复用视频时长获取函数
|
||||
# Use default audio duration of 0 seconds, real duration will be obtained when downloading the draft
|
||||
audio_duration = 0.0 # Default audio duration is 0 seconds
|
||||
# duration_result = get_video_duration(audio_url) # Reuse video duration retrieval function
|
||||
# if not duration_result["success"]:
|
||||
# print(f"获取音频时长失败: {duration_result['error']}")
|
||||
# print(f"Failed to get audio duration: {duration_result['error']}")
|
||||
|
||||
# # 检查音频时长是否超过10分钟
|
||||
# if duration_result["output"] > 600: # 600秒 = 10分钟
|
||||
# raise Exception(f"音频时长超过10分钟限制,当前时长: {duration_result['output']}秒")
|
||||
# # Check if audio duration exceeds 10 minutes
|
||||
# if duration_result["output"] > 600: # 600 seconds = 10 minutes
|
||||
# raise Exception(f"Audio duration exceeds 10-minute limit, current duration: {duration_result['output']} seconds")
|
||||
|
||||
# audio_duration = duration_result["output"]
|
||||
|
||||
# 下载音频到本地
|
||||
# Download audio to local
|
||||
# local_audio_path = download_audio(audio_url, draft_dir)
|
||||
|
||||
material_name = f"audio_{url_to_hash(audio_url)}.mp3" # 使用原始文件名+时间戳+固定mp3扩展名
|
||||
material_name = f"audio_{url_to_hash(audio_url)}.mp3" # Use original filename + timestamp + fixed mp3 extension
|
||||
|
||||
# 构建draft_audio_path
|
||||
# Build draft_audio_path
|
||||
draft_audio_path = None
|
||||
if draft_folder:
|
||||
if is_windows_path(draft_folder):
|
||||
# Windows路径处理
|
||||
# Windows path processing
|
||||
windows_drive, windows_path = re.match(r'([a-zA-Z]:)(.*)', draft_folder).groups()
|
||||
parts = [p for p in windows_path.split('\\') if p]
|
||||
draft_audio_path = os.path.join(windows_drive, *parts, draft_id, "assets", "audio", material_name)
|
||||
# 规范化路径(确保分隔符一致)
|
||||
# Normalize path (ensure consistent separators)
|
||||
draft_audio_path = draft_audio_path.replace('/', '\\')
|
||||
else:
|
||||
# macOS/Linux路径处理
|
||||
# macOS/Linux path processing
|
||||
draft_audio_path = os.path.join(draft_folder, draft_id, "assets", "audio", material_name)
|
||||
|
||||
# 设置音频结束时间的默认值
|
||||
# Set default value for audio end time
|
||||
audio_end = end if end is not None else audio_duration
|
||||
|
||||
# 计算音频时长
|
||||
# Calculate audio duration
|
||||
duration = audio_end - start
|
||||
|
||||
# 创建音频片段
|
||||
# Create audio segment
|
||||
if draft_audio_path:
|
||||
print('replace_path:', draft_audio_path)
|
||||
audio_material = draft.Audio_material(replace_path=draft_audio_path, remote_url=audio_url, material_name=material_name, duration=audio_duration)
|
||||
else:
|
||||
audio_material = draft.Audio_material(remote_url=audio_url, material_name=material_name, duration=audio_duration)
|
||||
audio_segment = draft.Audio_segment(
|
||||
audio_material, # 传入素材对象
|
||||
target_timerange=trange(f"{target_start}s", f"{duration}s"), # 使用target_start和duration
|
||||
audio_material, # Pass material object
|
||||
target_timerange=trange(f"{target_start}s", f"{duration}s"), # Use target_start and duration
|
||||
source_timerange=trange(f"{start}s", f"{duration}s"),
|
||||
speed=speed, # 设置播放速度
|
||||
volume=volume # 设置音量
|
||||
speed=speed, # Set playback speed
|
||||
volume=volume # Set volume
|
||||
)
|
||||
|
||||
# 添加场景音效果
|
||||
# Add scene sound effects
|
||||
if sound_effects:
|
||||
for effect_name, params in sound_effects:
|
||||
# 根据IS_CAPCUT_ENV选择不同的效果类型
|
||||
# Choose different effect types based on IS_CAPCUT_ENV
|
||||
effect_type = None
|
||||
|
||||
if IS_CAPCUT_ENV:
|
||||
# CapCut环境下,查找CapCut_Voice_filters_effect_type中的效果
|
||||
# In CapCut environment, look for effects in CapCut_Voice_filters_effect_type
|
||||
effect_type = getattr(CapCut_Voice_filters_effect_type, effect_name)
|
||||
# 如果在Voice_filters中没找到,可以继续在其他CapCut效果类型中查找
|
||||
# If not found in Voice_filters, continue searching in other CapCut effect types
|
||||
if effect_type is None:
|
||||
# 查找CapCut_Voice_characters_effect_type中的效果
|
||||
# Look for effects in CapCut_Voice_characters_effect_type
|
||||
effect_type = getattr(CapCut_Voice_characters_effect_type, effect_name)
|
||||
# 如果仍未找到,查找CapCut_Speech_to_song_effect_type中的效果
|
||||
# If still not found, look for effects in CapCut_Speech_to_song_effect_type
|
||||
if effect_type is None:
|
||||
effect_type = getattr(CapCut_Speech_to_song_effect_type, effect_name)
|
||||
else:
|
||||
# 剪映环境下,查找Audio_scene_effect_type中的效果
|
||||
# In JianYing environment, look for effects in Audio_scene_effect_type
|
||||
effect_type = getattr(Audio_scene_effect_type, effect_name)
|
||||
# 如果在Audio_scene_effect_type中没找到,可以继续在其他效果类型中查找
|
||||
# If not found in Audio_scene_effect_type, continue searching in other effect types
|
||||
if effect_type is None:
|
||||
effect_type = getattr(Tone_effect_type, effect_name)
|
||||
# 如果仍未找到,查找Speech_to_song_type中的效果
|
||||
# If still not found, look for effects in Speech_to_song_type
|
||||
if effect_type is None:
|
||||
effect_type = getattr(Speech_to_song_type, effect_name)
|
||||
# 这里可以根据需要添加其他效果类型的查找逻辑
|
||||
# Additional effect type lookup logic can be added here as needed
|
||||
|
||||
# 如果找到了对应的效果类型,则添加到音频片段
|
||||
# If corresponding effect type is found, add it to the audio segment
|
||||
if effect_type:
|
||||
audio_segment.add_effect(effect_type, params)
|
||||
else:
|
||||
print(f"警告: 未找到名为 {effect_name} 的音频效果")
|
||||
print(f"Warning: Audio effect named {effect_name} not found")
|
||||
|
||||
# 添加音频片段到轨道
|
||||
# Add audio segment to track
|
||||
script.add_segment(audio_segment, track_name=track_name)
|
||||
|
||||
return {
|
||||
|
||||
@@ -6,7 +6,7 @@ from util import generate_draft_url
|
||||
from settings import IS_CAPCUT_ENV
|
||||
|
||||
def add_effect_impl(
|
||||
effect_type: str, # 修改为字符串类型
|
||||
effect_type: str, # Changed to string type
|
||||
start: float = 0,
|
||||
end: float = 3.0,
|
||||
draft_id: Optional[str] = None,
|
||||
@@ -16,36 +16,36 @@ def add_effect_impl(
|
||||
height: int = 1920
|
||||
) -> Dict[str, str]:
|
||||
"""
|
||||
向指定草稿添加特效
|
||||
:param effect_type: 特效类型名称,将从Video_scene_effect_type或Video_character_effect_type中匹配
|
||||
:param start: 开始时间(秒),默认0
|
||||
:param end: 结束时间(秒),默认3秒
|
||||
:param draft_id: 草稿ID,如果为None或找不到对应的zip文件,则创建新草稿
|
||||
:param track_name: 轨道名称,当特效轨道仅有一条时可省略
|
||||
:param params: 特效参数列表,参数列表中未提供或为None的项使用默认值
|
||||
:param width: 视频宽度,默认1080
|
||||
:param height: 视频高度,默认1920
|
||||
:return: 更新后的草稿信息
|
||||
Add an effect to the specified draft
|
||||
:param effect_type: Effect type name, will be matched from Video_scene_effect_type or Video_character_effect_type
|
||||
:param start: Start time (seconds), default 0
|
||||
:param end: End time (seconds), default 3 seconds
|
||||
:param draft_id: Draft ID, if None or corresponding zip file not found, a new draft will be created
|
||||
:param track_name: Track name, can be omitted when there is only one effect track
|
||||
:param params: Effect parameter list, items not provided or None in the parameter list will use default values
|
||||
:param width: Video width, default 1080
|
||||
:param height: Video height, default 1920
|
||||
:return: Updated draft information
|
||||
"""
|
||||
# 获取或创建草稿
|
||||
# Get or create draft
|
||||
draft_id, script = get_or_create_draft(
|
||||
draft_id=draft_id,
|
||||
width=width,
|
||||
height=height
|
||||
)
|
||||
|
||||
# 计算时间范围
|
||||
# Calculate time range
|
||||
duration = end - start
|
||||
t_range = trange(f"{start}s", f"{duration}s")
|
||||
|
||||
# 动态获取特效类型对象
|
||||
# Dynamically get effect type object
|
||||
if IS_CAPCUT_ENV:
|
||||
# 如果是CapCut环境,使用CapCut特效
|
||||
# If in CapCut environment, use CapCut effects
|
||||
effect_enum = CapCut_Video_scene_effect_type[effect_type]
|
||||
if effect_enum is None:
|
||||
effect_enum = CapCut_Video_character_effect_type[effect_type]
|
||||
else:
|
||||
# 默认使用剪映特效
|
||||
# Default to using JianYing effects
|
||||
effect_enum = Video_scene_effect_type[effect_type]
|
||||
if effect_enum is None:
|
||||
effect_enum = Video_character_effect_type[effect_type]
|
||||
@@ -53,18 +53,18 @@ def add_effect_impl(
|
||||
if effect_enum is None:
|
||||
raise ValueError(f"Unknown effect type: {effect_type}")
|
||||
|
||||
# 添加特效轨道(仅当轨道不存在时)
|
||||
# Add effect track (only when track doesn't exist)
|
||||
if track_name is not None:
|
||||
try:
|
||||
imported_track=script.get_imported_track(draft.Track_type.effect, name=track_name)
|
||||
# 如果没有抛出异常,说明轨道已存在
|
||||
# If no exception is thrown, the track already exists
|
||||
except exceptions.TrackNotFound:
|
||||
# 轨道不存在,创建新轨道
|
||||
# Track doesn't exist, create a new track
|
||||
script.add_track(draft.Track_type.effect, track_name=track_name)
|
||||
else:
|
||||
script.add_track(draft.Track_type.effect)
|
||||
|
||||
# 添加特效
|
||||
# Add effect
|
||||
script.add_effect(effect_enum, t_range, params=params[::-1], track_name=track_name)
|
||||
|
||||
return {
|
||||
|
||||
@@ -16,7 +16,7 @@ def add_image_impl(
|
||||
width: int = 1080,
|
||||
height: int = 1920,
|
||||
start: float = 0,
|
||||
end: float = 3.0, # 默认显示3秒
|
||||
end: float = 3.0, # Default display time: 3 seconds
|
||||
draft_id: Optional[str] = None,
|
||||
transform_y: float = 0,
|
||||
scale_x: float = 1,
|
||||
@@ -24,123 +24,123 @@ def add_image_impl(
|
||||
transform_x: float = 0,
|
||||
track_name: str = "main",
|
||||
relative_index: int = 0,
|
||||
animation: Optional[str] = None, # 入场动画参数(向后兼容)
|
||||
animation_duration: float = 0.5, # 入场动画持续时间参数,默认0.5秒
|
||||
intro_animation: Optional[str] = None, # 新的入场动画参数,优先级高于animation
|
||||
intro_animation_duration: float = 0.5, # 新的入场动画持续时间参数,默认0.5秒
|
||||
outro_animation: Optional[str] = None, # 出场动画参数
|
||||
outro_animation_duration: float = 0.5, # 出场动画持续时间参数,默认0.5秒
|
||||
combo_animation: Optional[str] = None, # 组合动画参数
|
||||
combo_animation_duration: float = 0.5, # 组合动画持续时间参数,默认0.5秒
|
||||
transition: Optional[str] = None, # 转场类型参数
|
||||
transition_duration: Optional[float] = 0.5, # 转场持续时间参数(秒),默认0.5秒
|
||||
# 蒙版相关参数
|
||||
mask_type: Optional[str] = None, # 蒙版类型:线性、镜面、圆形、矩形、爱心、星形
|
||||
mask_center_x: float = 0.0, # 蒙版中心点X坐标
|
||||
mask_center_y: float = 0.0, # 蒙版中心点Y坐标
|
||||
mask_size: float = 0.5, # 蒙版主要尺寸
|
||||
mask_rotation: float = 0.0, # 蒙版旋转角度
|
||||
mask_feather: float = 0.0, # 蒙版羽化参数(0-100)
|
||||
mask_invert: bool = False, # 是否反转蒙版
|
||||
mask_rect_width: Optional[float] = None, # 矩形蒙版宽度(仅矩形蒙版)
|
||||
mask_round_corner: Optional[float] = None # 矩形蒙版圆角(仅矩形蒙版,0-100)
|
||||
animation: Optional[str] = None, # Entrance animation parameter (backward compatibility)
|
||||
animation_duration: float = 0.5, # Entrance animation duration parameter, default 0.5 seconds
|
||||
intro_animation: Optional[str] = None, # New entrance animation parameter, higher priority than animation
|
||||
intro_animation_duration: float = 0.5, # New entrance animation duration parameter, default 0.5 seconds
|
||||
outro_animation: Optional[str] = None, # Exit animation parameter
|
||||
outro_animation_duration: float = 0.5, # Exit animation duration parameter, default 0.5 seconds
|
||||
combo_animation: Optional[str] = None, # Combo animation parameter
|
||||
combo_animation_duration: float = 0.5, # Combo animation duration parameter, default 0.5 seconds
|
||||
transition: Optional[str] = None, # Transition type parameter
|
||||
transition_duration: Optional[float] = 0.5, # Transition duration parameter (seconds), default 0.5 seconds
|
||||
# Mask related parameters
|
||||
mask_type: Optional[str] = None, # Mask type: Linear, Mirror, Circle, Rectangle, Heart, Star
|
||||
mask_center_x: float = 0.0, # Mask center X coordinate
|
||||
mask_center_y: float = 0.0, # Mask center Y coordinate
|
||||
mask_size: float = 0.5, # Mask main size
|
||||
mask_rotation: float = 0.0, # Mask rotation angle
|
||||
mask_feather: float = 0.0, # Mask feather parameter (0-100)
|
||||
mask_invert: bool = False, # Whether to invert the mask
|
||||
mask_rect_width: Optional[float] = None, # Rectangle mask width (rectangle mask only)
|
||||
mask_round_corner: Optional[float] = None # Rectangle mask rounded corner (rectangle mask only, 0-100)
|
||||
) -> Dict[str, str]:
|
||||
"""
|
||||
向指定草稿添加图片轨道
|
||||
:param animation: 入场动画名称,支持的动画包括:缩小、渐显、放大、旋转、Kira游动、抖动下降、镜像翻转、旋转开幕、折叠开幕、漩涡旋转、跳转开幕等
|
||||
:param animation_duration: 入场动画持续时间(秒),默认0.5秒
|
||||
:param intro_animation: 新的入场动画参数,优先级高于animation
|
||||
:param intro_animation_duration: 新的入场动画持续时间(秒),默认0.5秒
|
||||
:param outro_animation: 出场动画参数
|
||||
:param outro_animation_duration: 出场动画持续时间(秒),默认0.5秒
|
||||
:param combo_animation: 组合动画参数
|
||||
:param combo_animation_duration: 组合动画持续时间(秒),默认0.5秒
|
||||
:param transition: 转场类型,支持的转场包括:叠化、上移、下移、左移、右移、分割、压缩、动漫云朵、动漫漩涡等
|
||||
:param transition_duration: 转场持续时间(秒),默认0.5秒
|
||||
:param draft_folder: 草稿文件夹路径,可选参数
|
||||
:param image_url: 图片URL
|
||||
:param width: 视频宽度,默认1080
|
||||
:param height: 视频高度,默认1920
|
||||
:param start: 开始时间(秒),默认0
|
||||
:param end: 结束时间(秒),默认3秒
|
||||
:param draft_id: 草稿ID,如果为None或找不到对应的zip文件,则创建新草稿
|
||||
:param transform_y: Y轴变换,默认0
|
||||
:param scale_x: X轴缩放,默认1
|
||||
:param scale_y: Y轴缩放,默认1
|
||||
:param transform_x: X轴变换,默认0
|
||||
:param track_name: 当只有一个视频的时候,轨道名称可以省略
|
||||
:param relative_index: 轨道渲染顺序索引,默认0
|
||||
:param mask_type: 蒙版类型,支持:线性、镜面、圆形、矩形、爱心、星形
|
||||
:param mask_center_x: 蒙版中心点X坐标(以素材的像素为单位),默认设置在素材中心
|
||||
:param mask_center_y: 蒙版中心点Y坐标(以素材的像素为单位),默认设置在素材中心
|
||||
:param mask_size: 蒙版的主要尺寸,以占素材高度的比例表示,默认为0.5
|
||||
:param mask_rotation: 蒙版顺时针旋转的角度,默认不旋转
|
||||
:param mask_feather: 蒙版的羽化参数,取值范围0~100,默认无羽化
|
||||
:param mask_invert: 是否反转蒙版,默认不反转
|
||||
:param mask_rect_width: 矩形蒙版的宽度,仅在蒙版类型为矩形时允许设置,以占素材宽度的比例表示
|
||||
:param mask_round_corner: 矩形蒙版的圆角参数,仅在蒙版类型为矩形时允许设置,取值范围0~100
|
||||
:return: 更新后的草稿信息,包含draft_id和draft_url
|
||||
Add an image track to the specified draft
|
||||
:param animation: Entrance animation name, supported animations include: Zoom Out, Fade In, Zoom In, Rotate, Kira Float, Shake Down, Mirror Flip, Rotate Open, Fold Open, Vortex Rotate, Jump Open, etc.
|
||||
:param animation_duration: Entrance animation duration (seconds), default 0.5 seconds
|
||||
:param intro_animation: New entrance animation parameter, higher priority than animation
|
||||
:param intro_animation_duration: New entrance animation duration (seconds), default 0.5 seconds
|
||||
:param outro_animation: Exit animation parameter
|
||||
:param outro_animation_duration: Exit animation duration (seconds), default 0.5 seconds
|
||||
:param combo_animation: Combo animation parameter
|
||||
:param combo_animation_duration: Combo animation duration (seconds), default 0.5 seconds
|
||||
:param transition: Transition type, supported transitions include: Dissolve, Move Up, Move Down, Move Left, Move Right, Split, Compress, Anime Cloud, Anime Vortex, etc.
|
||||
:param transition_duration: Transition duration (seconds), default 0.5 seconds
|
||||
:param draft_folder: Draft folder path, optional parameter
|
||||
:param image_url: Image URL
|
||||
:param width: Video width, default 1080
|
||||
:param height: Video height, default 1920
|
||||
:param start: Start time (seconds), default 0
|
||||
:param end: End time (seconds), default 3 seconds
|
||||
:param draft_id: Draft ID, if None or corresponding zip file not found, a new draft will be created
|
||||
:param transform_y: Y-axis transformation, default 0
|
||||
:param scale_x: X-axis scaling, default 1
|
||||
:param scale_y: Y-axis scaling, default 1
|
||||
:param transform_x: X-axis transformation, default 0
|
||||
:param track_name: When there is only one video, track name can be omitted
|
||||
:param relative_index: Track rendering order index, default 0
|
||||
:param mask_type: Mask type, supports: Linear, Mirror, Circle, Rectangle, Heart, Star
|
||||
:param mask_center_x: Mask center X coordinate (in material pixels), default set at material center
|
||||
:param mask_center_y: Mask center Y coordinate (in material pixels), default set at material center
|
||||
:param mask_size: Main size of the mask, represented as a proportion of material height, default is 0.5
|
||||
:param mask_rotation: Clockwise rotation angle of the mask, default no rotation
|
||||
:param mask_feather: Mask feather parameter, range 0~100, default no feathering
|
||||
:param mask_invert: Whether to invert the mask, default not inverted
|
||||
:param mask_rect_width: Rectangle mask width, only allowed when mask type is rectangle, represented as a proportion of material width
|
||||
:param mask_round_corner: Rectangle mask rounded corner parameter, only allowed when mask type is rectangle, range 0~100
|
||||
:return: Updated draft information, including draft_id and draft_url
|
||||
"""
|
||||
# 获取或创建草稿
|
||||
# Get or create draft
|
||||
draft_id, script = get_or_create_draft(
|
||||
draft_id=draft_id,
|
||||
width=width,
|
||||
height=height
|
||||
)
|
||||
|
||||
# 检查是否存在视频轨道,如果不存在则添加一条默认视频轨道
|
||||
# Check if video track exists, if not, add a default video track
|
||||
try:
|
||||
script.get_track(draft.Track_type.video, track_name=None)
|
||||
except exceptions.TrackNotFound:
|
||||
script.add_track(draft.Track_type.video, relative_index=0)
|
||||
except NameError:
|
||||
# 如果存在多个视频轨道(NameError),则不做任何操作
|
||||
# If multiple video tracks exist (NameError), do nothing
|
||||
pass
|
||||
|
||||
# 添加视频轨道(仅当轨道不存在时)
|
||||
# Add video track (only when track doesn't exist)
|
||||
if track_name is not None:
|
||||
try:
|
||||
imported_track=script.get_imported_track(draft.Track_type.video, name=track_name)
|
||||
# 如果没有抛出异常,说明轨道已存在
|
||||
# If no exception is thrown, the track already exists
|
||||
except exceptions.TrackNotFound:
|
||||
# 轨道不存在,创建新轨道
|
||||
# Track doesn't exist, create a new track
|
||||
script.add_track(draft.Track_type.video, track_name=track_name, relative_index=relative_index)
|
||||
else:
|
||||
script.add_track(draft.Track_type.video, relative_index=relative_index)
|
||||
|
||||
# 生成material_name但不下载图片
|
||||
# Generate material_name but don't download the image
|
||||
material_name = f"image_{url_to_hash(image_url)}.png"
|
||||
|
||||
# 构建draft_image_path
|
||||
# Build draft_image_path
|
||||
draft_image_path = None
|
||||
if draft_folder:
|
||||
# 检测输入路径类型并处理
|
||||
# Detect input path type and process
|
||||
if is_windows_path(draft_folder):
|
||||
# Windows路径处理
|
||||
# Windows path processing
|
||||
windows_drive, windows_path = re.match(r'([a-zA-Z]:)(.*)', draft_folder).groups()
|
||||
parts = [p for p in windows_path.split('\\') if p] # 分割路径并过滤空部分
|
||||
parts = [p for p in windows_path.split('\\') if p] # Split path and filter empty parts
|
||||
draft_image_path = os.path.join(windows_drive, *parts, draft_id, "assets", "image", material_name)
|
||||
# 规范化路径(确保分隔符一致)
|
||||
# Normalize path (ensure consistent separators)
|
||||
draft_image_path = draft_image_path.replace('/', '\\')
|
||||
else:
|
||||
# macOS/Linux路径处理
|
||||
# macOS/Linux path processing
|
||||
draft_image_path = os.path.join(draft_folder, draft_id, "assets", "image", material_name)
|
||||
|
||||
# 打印路径信息
|
||||
# Print path information
|
||||
print('replace_path:', draft_image_path)
|
||||
|
||||
# 创建图片素材
|
||||
# Create image material
|
||||
if draft_image_path:
|
||||
image_material = draft.Video_material(path=None, material_type='photo', replace_path=draft_image_path, remote_url=image_url, material_name=material_name)
|
||||
else:
|
||||
image_material = draft.Video_material(path=None, material_type='photo', remote_url=image_url, material_name=material_name)
|
||||
|
||||
# 创建target_timerange(图片
|
||||
# Create target_timerange (image)
|
||||
duration = end - start
|
||||
target_timerange = trange(f"{start}s", f"{duration}s")
|
||||
source_timerange = trange(f"{0}s", f"{duration}s")
|
||||
|
||||
# 创建图片片段
|
||||
# Create image segment
|
||||
image_segment = draft.Video_segment(
|
||||
image_material,
|
||||
target_timerange=target_timerange,
|
||||
@@ -153,7 +153,7 @@ def add_image_impl(
|
||||
)
|
||||
)
|
||||
|
||||
# 添加入场动画(优先使用intro_animation,其次使用animation)
|
||||
# Add entrance animation (prioritize intro_animation, then use animation)
|
||||
intro_anim = intro_animation if intro_animation is not None else animation
|
||||
intro_animation_duration = intro_animation_duration if intro_animation_duration is not None else animation_duration
|
||||
if intro_anim:
|
||||
@@ -162,46 +162,46 @@ def add_image_impl(
|
||||
animation_type = getattr(draft.CapCut_Intro_type, intro_anim)
|
||||
else:
|
||||
animation_type = getattr(draft.Intro_type, intro_anim)
|
||||
image_segment.add_animation(animation_type, intro_animation_duration * 1e6) # 使用微秒单位的动画持续时间
|
||||
image_segment.add_animation(animation_type, intro_animation_duration * 1e6) # Use microsecond unit for animation duration
|
||||
except AttributeError:
|
||||
raise ValueError(f"警告:不支持的入场动画类型 {intro_anim},将忽略此参数")
|
||||
raise ValueError(f"Warning: Unsupported entrance animation type {intro_anim}, this parameter will be ignored")
|
||||
|
||||
# 添加出场动画
|
||||
# Add exit animation
|
||||
if outro_animation:
|
||||
try:
|
||||
if IS_CAPCUT_ENV:
|
||||
outro_type = getattr(draft.CapCut_Outro_type, outro_animation)
|
||||
else:
|
||||
outro_type = getattr(draft.Outro_type, outro_animation)
|
||||
image_segment.add_animation(outro_type, outro_animation_duration * 1e6) # 使用微秒单位的动画持续时间
|
||||
image_segment.add_animation(outro_type, outro_animation_duration * 1e6) # Use microsecond unit for animation duration
|
||||
except AttributeError:
|
||||
raise ValueError(f"警告:不支持的出场动画类型 {outro_animation},将忽略此参数")
|
||||
raise ValueError(f"Warning: Unsupported exit animation type {outro_animation}, this parameter will be ignored")
|
||||
|
||||
# 添加组合动画
|
||||
# Add combo animation
|
||||
if combo_animation:
|
||||
try:
|
||||
if IS_CAPCUT_ENV:
|
||||
combo_type = getattr(draft.CapCut_Group_animation_type, combo_animation)
|
||||
else:
|
||||
combo_type = getattr(draft.Group_animation_type, combo_animation)
|
||||
image_segment.add_animation(combo_type, combo_animation_duration * 1e6) # 使用微秒单位的动画持续时间
|
||||
image_segment.add_animation(combo_type, combo_animation_duration * 1e6) # Use microsecond unit for animation duration
|
||||
except AttributeError:
|
||||
raise ValueError(f"警告:不支持的组合动画类型 {combo_animation},将忽略此参数")
|
||||
raise ValueError(f"Warning: Unsupported combo animation type {combo_animation}, this parameter will be ignored")
|
||||
|
||||
# 添加转场效果
|
||||
# Add transition effect
|
||||
if transition:
|
||||
try:
|
||||
if IS_CAPCUT_ENV:
|
||||
transition_type = getattr(draft.CapCut_Transition_type, transition)
|
||||
else:
|
||||
transition_type = getattr(draft.Transition_type, transition)
|
||||
# 将秒转换为微秒(乘以1000000)
|
||||
# Convert seconds to microseconds (multiply by 1000000)
|
||||
duration_microseconds = int(transition_duration * 1000000) if transition_duration is not None else None
|
||||
image_segment.add_transition(transition_type, duration=duration_microseconds)
|
||||
except AttributeError:
|
||||
raise ValueError(f"警告:不支持的转场类型 {transition},将忽略此参数")
|
||||
raise ValueError(f"Warning: Unsupported transition type {transition}, this parameter will be ignored")
|
||||
|
||||
# 添加蒙版效果
|
||||
# Add mask effect
|
||||
if mask_type:
|
||||
try:
|
||||
if IS_CAPCUT_ENV:
|
||||
@@ -210,7 +210,7 @@ def add_image_impl(
|
||||
mask_type_enum = getattr(draft.Mask_type, mask_type)
|
||||
image_segment.add_mask(
|
||||
script,
|
||||
mask_type_enum, # 移除关键字名称,作为位置参数传递
|
||||
mask_type_enum, # Remove keyword name, pass as positional argument
|
||||
center_x=mask_center_x,
|
||||
center_y=mask_center_y,
|
||||
size=mask_size,
|
||||
@@ -221,9 +221,9 @@ def add_image_impl(
|
||||
round_corner=mask_round_corner
|
||||
)
|
||||
except:
|
||||
raise ValueError(f"不支持的蒙版类型 {mask_type},支持的类型包括:线性、镜面、圆形、矩形、爱心、星形")
|
||||
raise ValueError(f"Unsupported mask type {mask_type}, supported types include: Linear, Mirror, Circle, Rectangle, Heart, Star")
|
||||
|
||||
# 添加图片片段到轨道
|
||||
# Add image segment to track
|
||||
script.add_segment(image_segment, track_name=track_name)
|
||||
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user