mirror of
https://github.com/Hommy-master/capcut-mate.git
synced 2026-08-30 17:48:47 +08:00
还原改动。
This commit is contained in:
@@ -790,43 +790,8 @@ class ScriptFile:
|
||||
track_list.sort(key=lambda track: track.render_index)
|
||||
self.content["tracks"] = [track.export_json() for track in track_list]
|
||||
|
||||
# 处理平台信息,确保跨平台兼容性
|
||||
self._process_platform_info()
|
||||
|
||||
return json.dumps(self.content, ensure_ascii=False, indent=4)
|
||||
|
||||
def _process_platform_info(self) -> None:
|
||||
"""处理平台信息,确保跨平台兼容性"""
|
||||
import platform
|
||||
current_os = platform.system().lower()
|
||||
if current_os == "darwin": # macOS
|
||||
os_name = "mac"
|
||||
elif current_os == "windows":
|
||||
os_name = "windows"
|
||||
else:
|
||||
os_name = "windows" # 默认为windows
|
||||
|
||||
# 更新平台信息
|
||||
if "platform" in self.content:
|
||||
self.content["platform"]["os"] = os_name
|
||||
else:
|
||||
self.content["platform"] = {
|
||||
"app_id": 3704,
|
||||
"app_source": "lv",
|
||||
"app_version": "5.9.0",
|
||||
"os": os_name
|
||||
}
|
||||
|
||||
if "last_modified_platform" in self.content:
|
||||
self.content["last_modified_platform"]["os"] = os_name
|
||||
else:
|
||||
self.content["last_modified_platform"] = {
|
||||
"app_id": 3704,
|
||||
"app_source": "lv",
|
||||
"app_version": "5.9.0",
|
||||
"os": os_name
|
||||
}
|
||||
|
||||
def dump(self, file_path: str) -> None:
|
||||
"""将草稿文件内容写入文件"""
|
||||
with open(file_path, "w", encoding="utf-8") as f:
|
||||
@@ -838,20 +803,6 @@ class ScriptFile:
|
||||
Raises:
|
||||
`ValueError`: 没有设置保存路径
|
||||
"""
|
||||
# 处理素材路径,将绝对路径替换为跨平台兼容的占位符
|
||||
if "materials" in self.content:
|
||||
for material_type in ["videos", "audios"]:
|
||||
if material_type in self.content["materials"]:
|
||||
for material in self.content["materials"][material_type]:
|
||||
if "path" in material:
|
||||
# 生成唯一的占位符ID
|
||||
import uuid
|
||||
placeholder_id = uuid.uuid4().hex
|
||||
# 用占位符替换实际路径
|
||||
placeholder_path = f"##_draftpath_placeholder_{placeholder_id}_##/{os.path.basename(material['path'])}"
|
||||
# 保留文件名部分,用占位符替换路径部分
|
||||
material["path"] = placeholder_path
|
||||
|
||||
if self.save_path is None:
|
||||
raise ValueError("没有设置保存路径, 可能不在模板模式下")
|
||||
self.dump(self.save_path)
|
||||
|
||||
+31
-26
@@ -193,41 +193,46 @@ def add_image_to_draft(
|
||||
transform_y=transform_y / image['height'] # 转换为半画布高单位
|
||||
)
|
||||
|
||||
# 创建视频素材实例(图片使用VideoMaterial)
|
||||
video_material = draft.VideoMaterial(
|
||||
path=image_path, # 使用绝对路径以确保文件能被正确解析
|
||||
material_name=os.path.basename(image_path)
|
||||
)
|
||||
|
||||
# 创建视频片段(图片使用VideoSegment)
|
||||
video_segment = draft.VideoSegment(
|
||||
material=video_material, # 使用VideoMaterial实例而不是路径字符串
|
||||
material=image_path,
|
||||
target_timerange=trange(start=image['start'], duration=segment_duration),
|
||||
clip_settings=clip_settings
|
||||
)
|
||||
|
||||
# 在添加到script之前,修改video_material的路径为相对路径
|
||||
# 获取草稿目录,用于计算相对路径
|
||||
draft_dir = os.path.dirname(os.path.dirname(script.save_path)) if script.save_path else draft_image_dir
|
||||
# 3. 添加动画效果(如果指定了)
|
||||
# 注意:由于动画相关的枚举类型较复杂,这里先预留接口
|
||||
if image.get('in_animation'):
|
||||
try:
|
||||
logger.info(f"In animation '{image['in_animation']}' specified but not implemented yet")
|
||||
# 这里可以根据需要添加具体的入场动画
|
||||
# 例如:video_segment.add_animation(IntroType.XXX, duration=image.get('in_animation_duration'))
|
||||
except Exception as e:
|
||||
logger.warning(f"Failed to add in animation '{image['in_animation']}': {str(e)}")
|
||||
|
||||
# 计算相对于草稿目录的相对路径
|
||||
relative_path = os.path.relpath(image_path, draft_dir)
|
||||
if image.get('out_animation'):
|
||||
try:
|
||||
logger.info(f"Out animation '{image['out_animation']}' specified but not implemented yet")
|
||||
# 这里可以根据需要添加具体的出场动画
|
||||
# 例如:video_segment.add_animation(OutroType.XXX, duration=image.get('out_animation_duration'))
|
||||
except Exception as e:
|
||||
logger.warning(f"Failed to add out animation '{image['out_animation']}': {str(e)}")
|
||||
|
||||
# 验证相对路径是否正确(确保文件存在)
|
||||
resolved_path = os.path.join(draft_dir, relative_path) if draft_dir else relative_path
|
||||
# 标准化路径,确保使用正确的分隔符
|
||||
resolved_path = os.path.normpath(resolved_path)
|
||||
relative_path = os.path.normpath(relative_path) # 标准化相对路径
|
||||
|
||||
if os.path.exists(resolved_path):
|
||||
# 修改素材实例的路径为相对路径,以确保跨平台兼容
|
||||
# 将反斜杠替换为正斜杠,确保跨平台兼容
|
||||
normalized_relative_path = relative_path.replace(os.sep, '/')
|
||||
video_material.path = normalized_relative_path
|
||||
else:
|
||||
logger.warning(f"Relative path does not resolve to existing file: {resolved_path}, keeping absolute path")
|
||||
# 如果相对路径无法解析到现有文件,则保留绝对路径
|
||||
if image.get('loop_animation'):
|
||||
try:
|
||||
logger.info(f"Loop animation '{image['loop_animation']}' specified but not implemented yet")
|
||||
# 循环动画可能需要特殊处理
|
||||
except Exception as e:
|
||||
logger.warning(f"Failed to add loop animation '{image['loop_animation']}': {str(e)}")
|
||||
|
||||
# 4. 添加转场效果(如果指定了)
|
||||
if image.get('transition'):
|
||||
try:
|
||||
logger.info(f"Transition '{image['transition']}' specified but not implemented yet")
|
||||
# 例如:video_segment.add_transition(TransitionType.XXX, duration=image.get('transition_duration'))
|
||||
except Exception as e:
|
||||
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']}")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user