from io import BytesIO import subprocess import time import uuid from custom_nodes.MemedeckComfyNodes.nodes_preprocessing import ffmpeg_process import folder_paths from comfy.cli_args import args import torch from PIL import Image import cairosvg from lxml import etree import numpy as np import json import os import logging from .lib import utils # setup logger logger = logging.getLogger(__name__) logger.setLevel(logging.INFO) WATERMARK = """ """ WATERMARK_SIZE = 28 class MD_SaveMP4: def __init__(self): self.output_dir = folder_paths.get_output_directory() self.type = "output" self.prefix_append = "" methods = {"default": 4, "fastest": 0, "slowest": 6} @classmethod def INPUT_TYPES(s): return { "required": { "images": ("IMAGE", ), "filename_prefix": ("STRING", {"default": "memedeck_video"}), "fps": ("FLOAT", {"default": 24.0, "min": 0.01, "max": 1000.0, "step": 0.01}), # "lossless": ("BOOLEAN", {"default": False}), # "quality": ("INT", {"default": 90, "min": 0, "max": 100}), # "method": (list(s.methods.keys()),), "crf": ("FLOAT",), "motion_prompt": ("STRING", ), "negative_prompt": ("STRING", ), }, "optional": { "seed_value": ("NOISE", ), "input_metadata": ("STRING", ), }, # "hidden": {"prompt": "PROMPT", "extra_pnginfo": "EXTRA_PNGINFO"}, } RETURN_TYPES = () FUNCTION = "save_images" OUTPUT_NODE = True CATEGORY = "MemeDeck" def save_images(self, images, fps, filename_prefix, crf=None, motion_prompt=None, negative_prompt=None, seed_value=None, input_metadata=None): start_time = time.time() filename_prefix += self.prefix_append full_output_folder, filename, counter, subfolder, filename_prefix = folder_paths.get_save_image_path(filename_prefix, self.output_dir, images[0].shape[1], images[0].shape[0]) results = [] # Vectorized conversion to PIL images pil_images = [Image.fromarray(np.clip(255. * image.cpu().numpy(), 0, 255).astype(np.uint8)) for image in images] first_image = pil_images[0] width = first_image.width height = first_image.height padding = 8 x = width - WATERMARK_SIZE - padding y = height - WATERMARK_SIZE - padding first_image_background_brightness = self.analyze_background_brightness(first_image, x, y, WATERMARK_SIZE) watermarked_images = [self.add_watermark_to_image(img, first_image_background_brightness) for img in pil_images] # metadata = pil_images[0].getexif() # num_frames = len(pil_images) logger.info(f"seed_value: {seed_value.seed}") logger.info(f"input_metadata: {input_metadata}") json_metadata = { "crf": crf, "motion_prompt": motion_prompt, "negative_prompt": negative_prompt, "seed": seed_value.seed, "input_metadata": json.loads(input_metadata), } # Use ffmpeg to create MP4 with watermark output_file = f"{filename}_{counter:05}_.mp4" output_path = os.path.join(full_output_folder, output_file) ffmpeg_cmd = [ utils.ffmpeg_path, "-v", "error", '-f', 'rawvideo', '-pix_fmt', 'rgb24', "-r", str(fps), # Set frame rate "-s", f"{width}x{height}", "-i", "-", "-y", # Overwrite output file if it exists "-c:v", "libx264", # Use x264 encoder "-crf", "19", # Set CRF (quality) "-pix_fmt", "yuv420p", # Set pixel format ] env = os.environ.copy() output_process = ffmpeg_process(ffmpeg_cmd, output_path, env) # Proceed to first yield output_process.send(None) output_process.send(first_image.tobytes()) for image in watermarked_images: output_process.send(image.tobytes()) try: output_process.send(None) # Signal end of input next(output_process) # Get the final yield except StopIteration: pass results.append({ "filename": output_file, "subfolder": subfolder, "type": self.type, }) end_time = time.time() logger.info(f"Save images took: {end_time - start_time} seconds") preview = { "filename": output_path, "subfolder": subfolder, "type": "output", "format": "video/mp4", "frame_rate": fps, } return { "ui": { "gifs": [preview], "metadata": (json.dumps(json_metadata),) }, "result": ((True, [output_path]),) } def add_watermark_to_image(self, img, background_brightness=None): """ Adds a watermark to a single PIL Image. Args: img: A PIL Image object. Returns: A PIL Image object with the watermark added. """ padding = 8 x = img.width - WATERMARK_SIZE - padding y = img.height - WATERMARK_SIZE - padding if background_brightness is None: background_brightness = self.analyze_background_brightness(img, x, y, WATERMARK_SIZE) # Generate watermark image (replace this with your actual watermark generation) watermark = self.generate_watermark(WATERMARK_SIZE, background_brightness) # Overlay the watermark img.paste(watermark, (x, y), watermark) return img def analyze_background_brightness(self, img, x, y, size): """ Analyzes the average brightness of a region in the image. Args: img: A PIL Image object. x: The x-coordinate of the top-left corner of the region. y: The y-coordinate of the top-left corner of the region. size: The size of the region (square). Returns: The average brightness of the region as an integer. """ region = img.crop((x, y, x + size, y + size)) pixels = np.array(region) total_brightness = np.sum( 0.299 * pixels[:, :, 0] + 0.587 * pixels[:, :, 1] + 0.114 * pixels[:, :, 2] ) / 1000 print(f"total_brightness: {total_brightness}") return max(0, min(255, total_brightness)) def generate_watermark(self, size, background_brightness): """ Generates a watermark image from an SVG string. Args: size: The size of the watermark (square). background_brightness: The background brightness at the watermark position. Returns: A PIL Image object representing the watermark. """ # Determine watermark color based on background brightness watermark_color = (0, 0, 0, 165) if background_brightness > 128 else (255, 255, 255, 165) # Parse the SVG string svg_tree = etree.fromstring(WATERMARK) # Find the path element and set its fill attribute path_element = svg_tree.find(".//{http://www.w3.org/2000/svg}path") if path_element is not None: r, g, b, a = watermark_color fill_color = f"rgba({r},{g},{b},{a/255})" # Convert to rgba string path_element.set("fill", fill_color) # Convert the modified SVG tree back to a string modified_svg = etree.tostring(svg_tree, encoding="unicode") # Render the modified SVG to a PNG image with a transparent background png_data = cairosvg.svg2png( bytestring=modified_svg, output_width=size, output_height=size, background_color="transparent" ) watermark_img = Image.open(BytesIO(png_data)) # Convert the watermark to RGBA to handle transparency watermark_img = watermark_img.convert("RGBA") return watermark_img class MD_VAEDecode: @classmethod def INPUT_TYPES(s): return { "required": { "samples": ("LATENT", {"tooltip": "The latent to be decoded."}), "vae": ("VAE", {"tooltip": "The VAE model used for decoding the latent."}) } } RETURN_TYPES = ("IMAGE",) OUTPUT_TOOLTIPS = ("The decoded image.",) FUNCTION = "decode" CATEGORY = "latent" DESCRIPTION = "Decodes latent images back into pixel space images." def decode(self, vae, samples): start_time = time.time() with torch.profiler.profile( activities=[torch.profiler.ProfilerActivity.CPU, torch.profiler.ProfilerActivity.CUDA], record_shapes=True, profile_memory=True, ) as prof: images = vae.decode(samples["samples"]) print(prof.key_averages().table(sort_by="cuda_time_total")) # Print profiling results if len(images.shape) == 5: images = images.reshape(-1, images.shape[-3], images.shape[-2], images.shape[-1]) end_time = time.time() print(f"VAE decoding time: {end_time - start_time:.4f} seconds") return (images,) # class MD_SaveMP4: # def __init__(self): # # Get absolute path of the output directory # self.output_dir = os.path.abspath("output/video_gen") # self.type = "output" # self.prefix_append = "" # methods = {"default": 4, "fastest": 0, "slowest": 6} # @classmethod # def INPUT_TYPES(s): # return {"required": # {"images": ("IMAGE", ), # "filename_prefix": ("STRING", {"default": "ComfyUI"}), # "fps": ("FLOAT", {"default": 24.0, "min": 0.01, "max": 1000.0, "step": 0.01}), # "quality": ("INT", {"default": 80, "min": 0, "max": 100}), # }, # "hidden": {"prompt": "PROMPT", "extra_pnginfo": "EXTRA_PNGINFO"}, # } # RETURN_TYPES = () # FUNCTION = "save_video" # OUTPUT_NODE = True # CATEGORY = "MemeDeck" # def save_video(self, images, fps, filename_prefix, quality, prompt=None, extra_pnginfo=None): # filename_prefix += self.prefix_append # full_output_folder, filename, counter, subfolder, filename_prefix = folder_paths.get_save_image_path( # filename_prefix, self.output_dir, images[0].shape[1], images[0].shape[0] # ) # results = list() # video_path = os.path.join(full_output_folder, f"{filename}_{counter:05}.mp4") # # Determine video resolution # height, width = images[0].shape[1], images[0].shape[2] # video_writer = cv2.VideoWriter( # video_path, # cv2.VideoWriter_fourcc(*'mp4v'), # fps, # (width, height) # ) # # Write each frame to the video # for image in images: # i = 255. * image.cpu().numpy() # frame = np.clip(i, 0, 255).astype(np.uint8) # frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR) # Convert RGB to BGR for OpenCV # video_writer.write(frame) # video_writer.release() # results.append({ # "filename": os.path.basename(video_path), # "subfolder": subfolder, # "type": self.type # }) # return {"ui": {"videos": results}}