From aa9b3cef2985f09d50c8d1798e477d2e7fefe5b4 Mon Sep 17 00:00:00 2001 From: salt Date: Tue, 5 Mar 2024 06:00:23 -0800 Subject: [PATCH] add comfy launcer and formatter script --- workflows/comfy/ComfyLauncher/ComfyRunner.py | 82 ++++++++++++++++ workflows/comfy/ComfyLauncher/prompt/.gitkeep | 1 + ...t, eventually I'll have stuff in here..txt | 0 workflows/comfy/format_video.py | 95 +++++++++++++++++++ workflows/comfy/repos.json | 2 +- 5 files changed, 179 insertions(+), 1 deletion(-) create mode 100644 workflows/comfy/ComfyLauncher/ComfyRunner.py create mode 100644 workflows/comfy/ComfyLauncher/prompt/.gitkeep create mode 100644 workflows/comfy/ComfyLauncher/web/extensions/FizzleDorf/Folder here to satisfy init, eventually I'll have stuff in here..txt create mode 100644 workflows/comfy/format_video.py diff --git a/workflows/comfy/ComfyLauncher/ComfyRunner.py b/workflows/comfy/ComfyLauncher/ComfyRunner.py new file mode 100644 index 0000000..c56d93d --- /dev/null +++ b/workflows/comfy/ComfyLauncher/ComfyRunner.py @@ -0,0 +1,82 @@ +import asyncio +import json +import os +import sys +import threading +import signal + +import aiohttp + +import argparse + +def parse_args(): + parser = argparse.ArgumentParser(description='Run Comfy') + parser.add_argument('--prompt', type=str, help='location of the prompt json', required=True) + return parser.parse_args() + +# current_script_dir = os.path.dirname(os.path.realpath(__file__)) +# file_path = os.path.join(current_script_dir, 'prompt', 'prompt.json') + +args = parse_args() + +file_path = args.prompt + +print(f'Loading prompt json from path: {file_path}') + +prompt = json.load(open(file_path)) + +comfy_path = os.path.dirname(os.path.abspath(__file__)) +comfy_path = os.path.join(comfy_path, '..', 'ComfyUI') + +# change to ComfyUI directory +os.chdir(comfy_path) + +# add ComfyUI to path +sys.path.append(comfy_path) + + +from server import PromptServer +from execution import PromptQueue +from main import init_custom_nodes, cuda_malloc_warning, prompt_worker +import folder_paths + + +async def run(): + loop = asyncio.new_event_loop() + asyncio.set_event_loop(loop) + server = PromptServer(loop) + q = PromptQueue(server) + + init_custom_nodes() + + cuda_malloc_warning() + + server.add_routes() + + threading.Thread(target=prompt_worker, daemon=True, args=(q, server,)).start() + + folder_paths.add_model_folder_path("checkpoints", os.path.join(folder_paths.get_output_directory(), "checkpoints")) + folder_paths.add_model_folder_path("clip", os.path.join(folder_paths.get_output_directory(), "clip")) + folder_paths.add_model_folder_path("vae", os.path.join(folder_paths.get_output_directory(), "vae")) + + await server.start('127.0.0.1', 8188) + + async def send_prompt_async(): + # Load prompt + p = {"prompt": prompt} + data = json.dumps(p).encode('utf-8') + # Send HTTP post request + endpoint = "http://127.0.0.1:8188/prompt" + async with aiohttp.ClientSession() as session: + async with session.post(endpoint, data=data) as resp: + print( + await resp.text()) + + + await send_prompt_async() + + while server.prompt_queue.currently_running: + await asyncio.sleep(0.05) + +if __name__ == "__main__": + asyncio.run(run()) diff --git a/workflows/comfy/ComfyLauncher/prompt/.gitkeep b/workflows/comfy/ComfyLauncher/prompt/.gitkeep new file mode 100644 index 0000000..8d1c8b6 --- /dev/null +++ b/workflows/comfy/ComfyLauncher/prompt/.gitkeep @@ -0,0 +1 @@ + diff --git a/workflows/comfy/ComfyLauncher/web/extensions/FizzleDorf/Folder here to satisfy init, eventually I'll have stuff in here..txt b/workflows/comfy/ComfyLauncher/web/extensions/FizzleDorf/Folder here to satisfy init, eventually I'll have stuff in here..txt new file mode 100644 index 0000000..e69de29 diff --git a/workflows/comfy/format_video.py b/workflows/comfy/format_video.py new file mode 100644 index 0000000..733d1c9 --- /dev/null +++ b/workflows/comfy/format_video.py @@ -0,0 +1,95 @@ +import os +import subprocess +import json +import sys +from pathlib import Path + + +def get_video_resolution(input_video): + command = [ + 'ffprobe', + '-v', 'error', + '-select_streams', 'v:0', + '-show_entries', 'stream=width,height', + '-of', 'json', + input_video + ] + result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, check=True) + video_info = json.loads(result.stdout) + + width = video_info['streams'][0]['width'] + height = video_info['streams'][0]['height'] + + return width, height + +def calculate_new_resolution(width, height, max_resolution): + aspect_ratio = width / height + max_width, max_height = map(int, max_resolution.split('x')) + + if width > max_width or height > max_height: + if (max_width / aspect_ratio) <= max_height: + new_width = max_width + new_height = round(new_width / aspect_ratio) + else: + new_height = max_height + new_width = round(new_height * aspect_ratio) + else: + new_width, new_height = width, height + + new_width = 8 * round(new_width / 8) + new_height = 8 * round(new_height / 8) + + return new_width, new_height + +def format_timestamp(ms): + hours, ms = divmod(ms, 3600000) + minutes, ms = divmod(ms, 60000) + seconds, ms = divmod(ms, 1000) + return f"{int(hours):02d}:{int(minutes):02d}:{int(seconds):02d}.{int(ms):03d}" + +def process_video(input_video, max_resolution, start_ms, end_ms, framerate): + width, height = get_video_resolution(input_video) + print(f"Original resolution: {width}x{height}") + + # new_width, new_height = calculate_new_resolution(width, height, max_resolution) + # print(f"New resolution: {new_width}x{new_height}") + + start_time = format_timestamp(start_ms) + duration_ms = end_ms - start_ms + duration = format_timestamp(duration_ms) + + # get input video directory + input_vid_dir = Path(input_video).absolute().parent + output_video = input_vid_dir.joinpath(f"input.mp4") + # os.makedirs(os.path.dirname(output_video), exist_ok=True) + + command = [ + 'ffmpeg', + '-y', + '-i', input_video, + '-ss', start_time, + '-t', duration, + # '-vf', f'scale={new_width}:{new_height}', + '-r', str(framerate), + '-c:v', 'libx264', '-preset', 'fast', '-crf', '22', + '-c:a', 'aac', '-b:a', '128k', + output_video + ] + + subprocess.run(command, check=True) + print(f"Video processed successfully. Output saved as {output_video}") + +if __name__ == "__main__": + if len(sys.argv) < 5: + # print("Usage: python script.py ") + print("Usage: python script.py ") + sys.exit(1) + + input_video = sys.argv[1] + # max_resolution = sys.argv[2] + max_resolution = None + start_ms = int(sys.argv[2]) + end_ms = int(sys.argv[3]) + framerate = float(sys.argv[4]) + + process_video(input_video, max_resolution, start_ms, end_ms, framerate) diff --git a/workflows/comfy/repos.json b/workflows/comfy/repos.json index 344778a..334ae94 100644 --- a/workflows/comfy/repos.json +++ b/workflows/comfy/repos.json @@ -36,7 +36,7 @@ }, { "url": "https://github.com/WASasquatch/was-node-suite-comfyui.git", - "commit": "596fc345ddf2edcda7cf444354afe586d630cddf", + "commit": "6c3fed70655b737dc9b59da1cadb3c373c08d8ed", "folder": "ComfyUI/custom_nodes/was-node-suite-comfyui" }, {