Compare commits

...
3 Commits
Author SHA1 Message Date
FranckyB 68ab668373 Adding Extract workflow tool 2026-11-02 06:50:27 -05:00
FranckyB 43319e7330 Merge branch 'llama_test' 2026-11-01 11:06:25 -05:00
Francois Beaudry 54f99f67b7 Merge pull request #50 from FranckyB/llama_test
Fix for newer version of llama
2026-10-30 11:59:59 -04:00
2 changed files with 176 additions and 0 deletions
+41
View File
@@ -0,0 +1,41 @@
@echo off
setlocal
if "%~1"=="" (
echo Usage: Drag and drop a video file onto this BAT.
echo.
echo This will create a JSON file next to the video.
pause
exit /b 1
)
set "VIDEO=%~1"
set "OUT=%~dp1%~n1.json"
set "SCRIPT_DIR=%~dp0"
set "SCRIPT=%SCRIPT_DIR%extract_video_workflow.py"
if not exist "%SCRIPT%" (
echo Error: Python script not found:
echo %SCRIPT%
pause
exit /b 2
)
echo Extracting workflow...
echo Input : %VIDEO%
echo Output: %OUT%
echo.
py "%SCRIPT%" "%VIDEO%" --out "%OUT%"
if errorlevel 1 (
echo.
echo Extraction failed.
pause
exit /b 3
)
echo.
echo Done. Workflow saved to:
echo %OUT%
pause
exit /b 0
+135
View File
@@ -0,0 +1,135 @@
"""
Extract embedded ComfyUI workflow JSON from a video file.
Usage:
python extract_video_workflow.py <video_path>
python extract_video_workflow.py <video_path> --out workflow.json
Notes:
- Accepts only true workflow payloads (full Comfy workflow JSON).
- Ignores prompt-only metadata payloads.
"""
import argparse
import json
import subprocess
import sys
from pathlib import Path
def _run_ffprobe(video_path: Path) -> dict:
"""Return ffprobe JSON for format metadata."""
cmd = [
"ffprobe",
"-v",
"quiet",
"-print_format",
"json",
"-show_format",
str(video_path),
]
result = subprocess.run(cmd, capture_output=True, text=True)
if result.returncode != 0:
err = result.stderr.strip() or "ffprobe failed"
raise RuntimeError(err)
try:
return json.loads(result.stdout)
except json.JSONDecodeError as e:
raise RuntimeError(f"ffprobe returned invalid JSON: {e}") from e
def _json_load_maybe(value):
"""Parse JSON string if possible, otherwise return the input unchanged."""
if not isinstance(value, str):
return value
try:
return json.loads(value)
except json.JSONDecodeError:
return value
def _is_valid_workflow_dict(data):
"""Strict workflow check: reject prompt-only/API graph payloads."""
if not isinstance(data, dict):
return False
if isinstance(data.get("nodes"), list) and isinstance(data.get("links"), list):
return True
if isinstance(data.get("nodes"), list) and "last_node_id" in data:
return True
wrapped = data.get("workflow")
if isinstance(wrapped, dict):
return _is_valid_workflow_dict(wrapped)
return False
def extract_workflow(video_path: Path):
"""Extract true workflow object from Comfy video metadata payload."""
ffprobe_data = _run_ffprobe(video_path)
tags = ffprobe_data.get("format", {}).get("tags", {})
# Preferred: direct workflow tag.
workflow = _json_load_maybe(tags.get("workflow"))
if _is_valid_workflow_dict(workflow):
return workflow
# Fallback: workflow nested in comment JSON payload.
comment = _json_load_maybe(tags.get("comment"))
if isinstance(comment, dict):
workflow = _json_load_maybe(comment.get("workflow"))
if _is_valid_workflow_dict(workflow):
return workflow
return None
def main() -> int:
parser = argparse.ArgumentParser(
description="Extract embedded ComfyUI workflow JSON from a video file"
)
parser.add_argument("video_path", help="Path to the video file")
parser.add_argument(
"--out",
dest="output_path",
default=None,
help="Optional path to save extracted workflow JSON",
)
args = parser.parse_args()
video_path = Path(args.video_path)
if not video_path.exists() or not video_path.is_file():
print(f"Error: file not found: {video_path}", file=sys.stderr)
return 1
try:
workflow = extract_workflow(video_path)
except FileNotFoundError:
print("Error: ffprobe is not installed or not in PATH.", file=sys.stderr)
return 2
except Exception as e:
print(f"Error: {e}", file=sys.stderr)
return 3
if workflow is None:
print("No embedded workflow metadata found.", file=sys.stderr)
return 4
output_json = json.dumps(workflow, indent=2, ensure_ascii=False)
if args.output_path:
output_path = Path(args.output_path)
output_path.write_text(output_json, encoding="utf-8")
print(f"Workflow extracted to: {output_path}")
else:
print(output_json)
return 0
if __name__ == "__main__":
raise SystemExit(main())