From 2b19bca6bb8543ff453cea513ac3cd4fe77b2022 Mon Sep 17 00:00:00 2001 From: peter Date: Wed, 28 May 2025 23:37:35 +0800 Subject: [PATCH 1/4] feat(handler): add support for processing and uploading gif videos to S3 Add logic to handle gif video outputs from ComfyUI nodes, including uploading to S3 bucket and cleaning up local files. The implementation follows the same pattern as image handling but with video-specific content type handling. --- handler.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/handler.py b/handler.py index c9e84f32f..56218f058 100644 --- a/handler.py +++ b/handler.py @@ -733,6 +733,54 @@ def handler(job): error_msg = f"Failed to fetch image data for {filename} from /view endpoint." errors.append(error_msg) + if "gifs" in node_output: + print( + f"worker-comfyui - Node {node_id} contains {len(node_output['gifs'])} video(s)" + ) + for video_info in node_output["gifs"]: + print(f"worker-comfyui - video_info: {video_info}") + filename = video_info.get("filename") + if video_info.get("type") == "temp": + print( + f"worker-comfyui - Skipping video {filename} because type is 'temp'" + ) + continue + if os.environ.get("BUCKET_ENDPOINT_URL"): + file_location = video_info.get("fullpath") + try: + print(f"worker-comfyui - Uploading {filename} to S3...") + s3_url = rp_upload.upload_file_to_bucket( + file_name=filename, + file_location=file_location, + prefix=f"comfy/{job_id}", + extra_args={"ContentType": f"video/{filename.split('.')[-1]}"} + ) + os.remove(file_location) # Clean up local video file + print( + f"worker-comfyui - Uploaded {filename} to S3: {s3_url}" + ) + # Append dictionary with filename and URL + output_data.append( + { + "filename": filename, + "type": "s3_url", + "data": s3_url, + } + ) + except Exception as e: + error_msg = f"Error uploading {filename} to S3: {e}" + print(f"worker-comfyui - {error_msg}") + errors.append(error_msg) + if "file_location" in locals() and os.path.exists( + file_location + ): + try: + os.remove(file_location) + except OSError as rm_err: + print( + f"worker-comfyui - Error removing local video file {file_location}: {rm_err}" + ) + # Check for other output types other_keys = [k for k in node_output.keys() if k != "images"] if other_keys: From 8a7d3b257eadf5e2aece722aeba114c8e92e2629 Mon Sep 17 00:00:00 2001 From: peter Date: Fri, 30 May 2025 09:29:10 +0800 Subject: [PATCH 2/4] refactor(handler): separate image and video output data Split the output_data list into separate lists for images and videos to improve data organization and clarity. This allows for better handling of different media types in the final result. Update the final result structure to include separate "images" and "videos" fields when present, and modify the completion log message to reflect both media types. --- handler.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/handler.py b/handler.py index 56218f058..561d52acd 100644 --- a/handler.py +++ b/handler.py @@ -520,7 +520,8 @@ def handler(job): ws = None client_id = str(uuid.uuid4()) prompt_id = None - output_data = [] + output_data_images = [] + output_data_videos = [] errors = [] try: @@ -690,7 +691,7 @@ def handler(job): f"worker-comfyui - Uploaded {filename} to S3: {s3_url}" ) # Append dictionary with filename and URL - output_data.append( + output_data_images.append( { "filename": filename, "type": "s3_url", @@ -717,7 +718,7 @@ def handler(job): "utf-8" ) # Append dictionary with filename and base64 data - output_data.append( + output_data_images.append( { "filename": filename, "type": "base64", @@ -760,7 +761,7 @@ def handler(job): f"worker-comfyui - Uploaded {filename} to S3: {s3_url}" ) # Append dictionary with filename and URL - output_data.append( + output_data_videos.append( { "filename": filename, "type": "s3_url", @@ -814,9 +815,13 @@ def handler(job): ws.close() final_result = {} - - if output_data: - final_result["images"] = output_data + output_data = output_data_images + output_data_videos + + if output_data_images: + final_result["images"] = output_data_images + + if output_data_videos: + final_result["videos"] = output_data_videos if errors: final_result["errors"] = errors @@ -835,7 +840,7 @@ def handler(job): final_result["status"] = "success_no_images" final_result["images"] = [] - print(f"worker-comfyui - Job completed. Returning {len(output_data)} image(s).") + print(f"worker-comfyui - Job completed. Returning {len(output_data)} image/video(s).") return final_result From 0ff03ce8bb651854a990048852d2d80fb02fbc73 Mon Sep 17 00:00:00 2001 From: peter Date: Fri, 30 May 2025 09:31:57 +0800 Subject: [PATCH 3/4] fix: update error messages to include videos in output checks The error messages and status field were only referencing images when checking for empty output data. This change updates them to explicitly mention both images and videos to accurately reflect the possible output types and avoid confusion. --- handler.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/handler.py b/handler.py index 561d52acd..c313f657d 100644 --- a/handler.py +++ b/handler.py @@ -816,7 +816,7 @@ def handler(job): final_result = {} output_data = output_data_images + output_data_videos - + if output_data_images: final_result["images"] = output_data_images @@ -828,16 +828,16 @@ def handler(job): print(f"worker-comfyui - Job completed with errors/warnings: {errors}") if not output_data and errors: - print(f"worker-comfyui - Job failed with no output images.") + print(f"worker-comfyui - Job failed with no output images/videos.") return { "error": "Job processing failed", "details": errors, } elif not output_data and not errors: print( - f"worker-comfyui - Job completed successfully, but the workflow produced no images." + f"worker-comfyui - Job completed successfully, but the workflow produced no images/videos." ) - final_result["status"] = "success_no_images" + final_result["status"] = "success_no_images_or_videos" final_result["images"] = [] print(f"worker-comfyui - Job completed. Returning {len(output_data)} image/video(s).") From 07ecb28eb98e97937f412fc48d2baf01baa230f4 Mon Sep 17 00:00:00 2001 From: peter Date: Fri, 30 May 2025 09:33:01 +0800 Subject: [PATCH 4/4] fix: add missing videos field to final_result when no media present The handler function was not including an empty "videos" array in the final_result when no images or videos were processed, which could cause issues with clients expecting this field. This change ensures consistent response structure by initializing the field similar to how "images" is handled. --- handler.py | 1 + 1 file changed, 1 insertion(+) diff --git a/handler.py b/handler.py index c313f657d..0778903f1 100644 --- a/handler.py +++ b/handler.py @@ -839,6 +839,7 @@ def handler(job): ) final_result["status"] = "success_no_images_or_videos" final_result["images"] = [] + final_result["videos"] = [] print(f"worker-comfyui - Job completed. Returning {len(output_data)} image/video(s).") return final_result