add comfy launcer and formatter script

This commit is contained in:
salt
2024-03-05 06:00:23 -08:00
parent 386613acda
commit aa9b3cef29
5 changed files with 179 additions and 1 deletions
@@ -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())
@@ -0,0 +1 @@
+95
View File
@@ -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 <input_video> <max_resolution> <start_ms> <end_ms> <framerate>")
print("Usage: python script.py <input_video> <start_ms> <end_ms> <framerate>")
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)
+1 -1
View File
@@ -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"
},
{