From 73da4b9a8b47fab147fca4cfe825822ae0306eec Mon Sep 17 00:00:00 2001 From: Hommy <16620803786@163.com> Date: Mon, 12 Jan 2026 11:10:24 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=85=BC=E5=AE=B9=E6=80=A7?= =?UTF-8?q?=E9=97=AE=E9=A2=98=EF=BC=8C=E5=90=8C=E6=97=B6=E5=85=BC=E5=AE=B9?= =?UTF-8?q?windows=E5=92=8Cmacos=E4=B8=8B=E5=89=AA=E6=98=A0=E8=8D=89?= =?UTF-8?q?=E7=A8=BF=E6=A0=BC=E5=BC=8F=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pyJianYingDraft/draft_folder.py | 16 ++++++++++++++-- src/pyJianYingDraft/script_file.py | 15 +++++++++++++++ src/service/create_draft.py | 17 ++++++++++++----- 3 files changed, 41 insertions(+), 7 deletions(-) diff --git a/src/pyJianYingDraft/draft_folder.py b/src/pyJianYingDraft/draft_folder.py index 00c8e63..c2d82a3 100644 --- a/src/pyJianYingDraft/draft_folder.py +++ b/src/pyJianYingDraft/draft_folder.py @@ -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: """复制一份给定的草稿, 并在复制出的新草稿上进行编辑 diff --git a/src/pyJianYingDraft/script_file.py b/src/pyJianYingDraft/script_file.py index 6ccbe61..4bba398 100644 --- a/src/pyJianYingDraft/script_file.py +++ b/src/pyJianYingDraft/script_file.py @@ -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) diff --git a/src/service/create_draft.py b/src/service/create_draft.py index 21f2198..ca698cf 100644 --- a/src/service/create_draft.py +++ b/src/service/create_draft.py @@ -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)