97 lines
2.8 KiB
Python
97 lines
2.8 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
给已有的 segments.json 添加 videoPath 和 videoType 字段
|
|
保留原有的 scene/voiceover/duration 不变
|
|
"""
|
|
|
|
import json
|
|
from pathlib import Path
|
|
|
|
MEIJIAKA_DIR = Path.home() / "Documents" / "Meijiaka"
|
|
PROJECTS_DIR = MEIJIAKA_DIR / "projects"
|
|
|
|
|
|
def add_video_path(project_id: str):
|
|
project_dir = PROJECTS_DIR / project_id
|
|
segments_file = project_dir / "segments.json"
|
|
videos_dir = project_dir / "videos"
|
|
|
|
if not segments_file.exists():
|
|
print(f"segments.json 不存在: {segments_file}")
|
|
return False
|
|
|
|
if not videos_dir.exists():
|
|
print(f"videos 目录不存在: {videos_dir}")
|
|
return False
|
|
|
|
# 读取现有的 segments
|
|
with open(segments_file, "r", encoding="utf-8") as f:
|
|
segments = json.load(f)
|
|
|
|
print(f"读取到 {len(segments)} 个分镜")
|
|
|
|
# 建立文件名映射
|
|
video_map = {}
|
|
for video_path in videos_dir.glob("*.mp4"):
|
|
stem = video_path.stem
|
|
# scene_1.mp4 -> 1
|
|
parts = stem.split("_")
|
|
for part in reversed(parts):
|
|
if part.isdigit():
|
|
shot_id = int(part)
|
|
video_map[shot_id] = stem
|
|
break
|
|
|
|
print(f"找到 {len(video_map)} 个视频文件")
|
|
|
|
# 更新每个分镜,添加 videoPath
|
|
updated = 0
|
|
for seg in segments:
|
|
shot_id = seg["id"]
|
|
if shot_id in video_map:
|
|
# 视频已经在本地,直接用 file:// URL
|
|
full_path = videos_dir / f"{video_map[shot_id]}.mp4"
|
|
abs_path = str(full_path.absolute())
|
|
if abs_path.startswith("/"):
|
|
video_url = "file://" + abs_path
|
|
else:
|
|
# Windows
|
|
normalized = abs_path.replace("\\", "/")
|
|
video_url = "file:///" + normalized
|
|
seg["videoPath"] = video_url
|
|
shot_type = seg.get("type", "segment")
|
|
seg["videoType"] = (
|
|
"text2video" if shot_type == "empty_shot" else "avatar-video"
|
|
)
|
|
updated += 1
|
|
print(f" ✓ 镜头 {shot_id} 添加 videoPath: {video_url}")
|
|
|
|
# 备份原文件
|
|
backup = segments_file.with_suffix(".json.backup2")
|
|
segments_file.replace(backup)
|
|
print(f"\n原文件备份到: {backup}")
|
|
|
|
# 保存更新后的
|
|
with open(segments_file, "w", encoding="utf-8") as f:
|
|
json.dump(segments, f, indent=2, ensure_ascii=False)
|
|
|
|
print(f"\n完成!更新了 {updated} 个分镜")
|
|
print(f"原文件备份在: {backup}")
|
|
return True
|
|
|
|
|
|
def main():
|
|
import sys
|
|
|
|
if len(sys.argv) != 2:
|
|
print(f"用法: python {sys.argv[0]} <project_id>")
|
|
print(f"示例: python {sys.argv[0]} proj_1775626705858_4ngn3k63z")
|
|
return
|
|
|
|
project_id = sys.argv[1]
|
|
add_video_path(project_id)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|