优化兼容性问题,同时兼容windows和macos下剪映草稿格式。

This commit is contained in:
Hommy
2026-01-12 11:10:24 +08:00
parent ac4b824118
commit 73da4b9a8b
3 changed files with 41 additions and 7 deletions
+14 -2
View File
@@ -86,7 +86,16 @@ class DraftFolder:
# 创建草稿文件
script_file = ScriptFile(width, height, fps)
script_file.save_path = os.path.join(draft_path, "draft_content.json")
# 设置保存路径为 draft_content.json
draft_content_path = os.path.join(draft_path, "draft_content.json")
script_file.save_path = draft_content_path
# 启用双文件兼容模式
script_file.dual_file_compatibility = True
# 保存到 draft_content.json(会自动同步到 draft_info.json
script_file.save()
return script_file
@@ -122,7 +131,10 @@ class DraftFolder:
if not os.path.exists(draft_path):
raise FileNotFoundError(f"草稿文件夹 {draft_name} 不存在")
return ScriptFile.load_template(os.path.join(draft_path, "draft_content.json"))
script_file = ScriptFile.load_template(os.path.join(draft_path, "draft_content.json"))
# 启用双文件兼容模式,以便在保存时同时更新两个文件
script_file.dual_file_compatibility = True
return script_file
def duplicate_as_template(self, template_name: str, new_draft_name: str, allow_replace: bool = False) -> ScriptFile:
"""复制一份给定的草稿, 并在复制出的新草稿上进行编辑
+15
View File
@@ -182,6 +182,7 @@ class ScriptFile:
fps (int, optional): 视频帧率. 默认为30.
"""
self.save_path = None
self.dual_file_compatibility = False # 控制是否同时保存两个文件
self.width = width
self.height = height
@@ -809,4 +810,18 @@ class ScriptFile:
"""
if self.save_path is None:
raise ValueError("没有设置保存路径, 可能不在模板模式下")
# 保存到主要文件
self.dump(self.save_path)
# 如果启用了双文件兼容模式,同时保存到另一个文件
if self.dual_file_compatibility:
draft_dir = os.path.dirname(self.save_path)
if "draft_content.json" in self.save_path and "draft_info.json" not in self.save_path:
# 当前保存的是 draft_content.json,同时保存到 draft_info.json
alt_path = os.path.join(draft_dir, "draft_info.json")
self.dump(alt_path)
elif "draft_info.json" in self.save_path and "draft_content.json" not in self.save_path:
# 当前保存的是 draft_info.json,同时保存到 draft_content.json
alt_path = os.path.join(draft_dir, "draft_content.json")
self.dump(alt_path)
+12 -5
View File
@@ -37,14 +37,21 @@ def create_draft(width: int, height: int) -> str:
if os.path.exists(draft_path): shutil.rmtree(draft_path)
shutil.copytree(template_path, draft_path)
# 重命名并加载模板草稿,然后修改配置
old_path = os.path.join(draft_path, "draft_info.json")
new_path = os.path.join(draft_path, "draft_content.json")
os.rename(old_path, new_path)
script = draft.ScriptFile.load_template(new_path)
# 在创建草稿时,确保两个文件都存在且内容相同
draft_info_path = os.path.join(draft_path, "draft_info.json")
draft_content_path = os.path.join(draft_path, "draft_content.json")
# 加载模板草稿,然后修改配置
script = draft.ScriptFile.load_template(draft_info_path)
# 启用双文件兼容模式,这样保存时会自动同步两个文件
script.dual_file_compatibility = True
script.width, script.height = width, height
script.content["canvas_config"]["width"], script.content["canvas_config"]["height"] = width, height
# 保存修改后的草稿(会自动同步到两个文件)
script.save_path = draft_content_path
script.save()
# 添加空的主轨道(仅当没有主轨道时添加)
main_track_name = "main_track"
script.add_track(track_type=draft.TrackType.video, track_name=main_track_name, relative_index=0)