The most powerful and modular diffusion model GUI, api and backend with a graph/nodes interface. The fastest local inference engine in the world.
convert Video nodes to V3 schema (#9489)
Alexander Piskunfea9ea8268d9
diff --git a/comfy_extras/nodes_video.py b/comfy_extras/nodes_video.pyindex 969f888b9..69fabb12e 100644--- a/comfy_extras/nodes_video.py+++ b/comfy_extras/nodes_video.py@@ -5,52 +5,49 @@ import avimport torchimport folder_pathsimport json-from typing import Optional, Literal+from typing import Optional+from typing_extensions import overridefrom fractions import Fraction-from comfy.comfy_types import IO, FileLocator, ComfyNodeABC-from comfy_api.latest import Input, InputImpl, Types+from comfy_api.input import AudioInput, ImageInput, VideoInput+from comfy_api.input_impl import VideoFromComponents, VideoFromFile+from comfy_api.util import VideoCodec, VideoComponents, VideoContainer+from comfy_api.latest import ComfyExtension, io, uifrom comfy.cli_args import args-class SaveWEBM:- def __init__(self):- self.output_dir = folder_paths.get_output_directory()- self.type = "output"- self.prefix_append = ""-+class SaveWEBM(io.ComfyNode):@classmethod- def INPUT_TYPES(s):- return {"required":- {"images": ("IMAGE", ),- "filename_prefix": ("STRING", {"default": "ComfyUI"}),- "codec": (["vp9", "av1"],),- "fps": ("FLOAT", {"default": 24.0, "min": 0.01, "max": 1000.0, "step": 0.01}),- "crf": ("FLOAT", {"default": 32.0, "min": 0, "max": 63.0, "step": 1, "tooltip": "Higher crf means lower quality with a smaller file size, lower crf means higher quality higher filesize."}),- },- "hidden": {"prompt": "PROMPT", "extra_pnginfo": "EXTRA_PNGINFO"},- }-- RETURN_TYPES = ()- FUNCTION = "save_images"-- OUTPUT_NODE = True-- CATEGORY = "image/video"-- EXPERIMENTAL = True+ def define_schema(cls):+ return io.Schema(+ node_id="SaveWEBM",+ category="image/video",+ is_experimental=True,+ inputs=[+ io.Image.Input("images"),+ io.String.Input("filename_prefix", default="ComfyUI"),+ io.Combo.Input("codec", options=["vp9", "av1"]),+ io.Float.Input("fps", default=24.0, min=0.01, max=1000.0, step=0.01),+ io.Float.Input("crf", default=32.0, min=0, max=63.0, step=1, tooltip="Higher crf means lower quality with a smaller file size, lower crf means higher quality higher filesize."),+ ],+ outputs=[],+ hidden=[io.Hidden.prompt, io.Hidden.extra_pnginfo],+ is_output_node=True,+ )- def save_images(self, images, codec, fps, filename_prefix, crf, 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])+ @classmethod+ def execute(cls, images, codec, fps, filename_prefix, crf) -> io.NodeOutput:+ full_output_folder, filename, counter, subfolder, filename_prefix = folder_paths.get_save_image_path(+ filename_prefix, folder_paths.get_output_directory(), images[0].shape[1], images[0].shape[0]+ )file = f"{filename}_{counter:05}_.webm"container = av.open(os.path.join(full_output_folder, file), mode="w")- if prompt is not None:- container.metadata["prompt"] = json.dumps(prompt)+ if cls.hidden.prompt is not None:+ container.metadata["prompt"] = json.dumps(cls.hidden.prompt)- if extra_pnginfo is not None:- for x in extra_pnginfo:- container.metadata[x] = json.dumps(extra_pnginfo[x])+ if cls.hidden.extra_pnginfo is not None:+ for x in cls.hidden.extra_pnginfo:+ container.metadata[x] = json.dumps(cls.hidden.extra_pnginfo[x])codec_map = {"vp9": "libvpx-vp9", "av1": "libsvtav1"}stream = container.add_stream(codec_map[codec], rate=Fraction(round(fps * 1000), 1000))@@ -69,63 +66,46 @@ class SaveWEBM:container.mux(stream.encode())container.close()- results: list[FileLocator] = [{- "filename": file,- "subfolder": subfolder,- "type": self.type- }]+ return io.NodeOutput(ui=ui.PreviewVideo([ui.SavedResult(file, subfolder, io.FolderType.output)]))- return {"ui": {"images": results, "animated": (True,)}} # TODO: frontend side--class SaveVideo(ComfyNodeABC):- def __init__(self):- self.output_dir = folder_paths.get_output_directory()- self.type: Literal["output"] = "output"- self.prefix_append = ""+class SaveVideo(io.ComfyNode):+ @classmethod+ def define_schema(cls):+ return io.Schema(+ node_id="SaveVideo",+ display_name="Save Video",+ category="image/video",+ description="Saves the input images to your ComfyUI output directory.",+ inputs=[+ io.Video.Input("video", tooltip="The video to save."),+ io.String.Input("filename_prefix", default="video/ComfyUI", tooltip="The prefix for the file to save. This may include formatting information such as %date:yyyy-MM-dd% or %Empty Latent Image.width% to include values from nodes."),+ io.Combo.Input("format", options=VideoContainer.as_input(), default="auto", tooltip="The format to save the video as."),+ io.Combo.Input("codec", options=VideoCodec.as_input(), default="auto", tooltip="The codec to use for the video."),+ ],+ outputs=[],+ hidden=[io.Hidden.prompt, io.Hidden.extra_pnginfo],+ is_output_node=True,+ )@classmethod- def INPUT_TYPES(cls):- return {- "required": {- "video": (IO.VIDEO, {"tooltip": "The video to save."}),- "filename_prefix": ("STRING", {"default": "video/ComfyUI", "tooltip": "The prefix for the file to save. This may include formatting information such as %date:yyyy-MM-dd% or %Empty Latent Image.width% to include values from nodes."}),- "format": (Types.VideoContainer.as_input(), {"default": "auto", "tooltip": "The format to save the video as."}),- "codec": (Types.VideoCodec.as_input(), {"default": "auto", "tooltip": "The codec to use for the video."}),- },- "hidden": {- "prompt": "PROMPT",- "extra_pnginfo": "EXTRA_PNGINFO"- },- }-- RETURN_TYPES = ()- FUNCTION = "save_video"-- OUTPUT_NODE = True-- CATEGORY = "image/video"- DESCRIPTION = "Saves the input images to your ComfyUI output directory."-- def save_video(self, video: Input.Video, filename_prefix, format, codec, prompt=None, extra_pnginfo=None):- filename_prefix += self.prefix_append+ def execute(cls, video: VideoInput, filename_prefix, format, codec) -> io.NodeOutput:width, height = video.get_dimensions()full_output_folder, filename, counter, subfolder, filename_prefix = folder_paths.get_save_image_path(filename_prefix,- self.output_dir,+ folder_paths.get_output_directory(),width,height)- results: list[FileLocator] = list()saved_metadata = Noneif not args.disable_metadata:metadata = {}- if extra_pnginfo is not None:- metadata.update(extra_pnginfo)- if prompt is not None:- metadata["prompt"] = prompt+ if cls.hidden.extra_pnginfo is not None:+ metadata.update(cls.hidden.extra_pnginfo)+ if cls.hidden.prompt is not None:+ metadata["prompt"] = cls.hidden.promptif len(metadata) > 0:saved_metadata = metadata- file = f"{filename}_{counter:05}_.{Types.VideoContainer.get_extension(format)}"+ file = f"{filename}_{counter:05}_.{VideoContainer.get_extension(format)}"video.save_to(os.path.join(full_output_folder, file),format=format,@@ -133,83 +113,82 @@ class SaveVideo(ComfyNodeABC):metadata=saved_metadata)- results.append({- "filename": file,- "subfolder": subfolder,- "type": self.type- })- counter += 1+ return io.NodeOutput(ui=ui.PreviewVideo([ui.SavedResult(file, subfolder, io.FolderType.output)]))- return { "ui": { "images": results, "animated": (True,) } }-class CreateVideo(ComfyNodeABC):+class CreateVideo(io.ComfyNode):@classmethod- def INPUT_TYPES(cls):- return {- "required": {- "images": (IO.IMAGE, {"tooltip": "The images to create a video from."}),- "fps": ("FLOAT", {"default": 30.0, "min": 1.0, "max": 120.0, "step": 1.0}),- },- "optional": {- "audio": (IO.AUDIO, {"tooltip": "The audio to add to the video."}),- }- }-- RETURN_TYPES = (IO.VIDEO,)- FUNCTION = "create_video"-- CATEGORY = "image/video"- DESCRIPTION = "Create a video from images."-- def create_video(self, images: Input.Image, fps: float, audio: Optional[Input.Audio] = None):- return (InputImpl.VideoFromComponents(- Types.VideoComponents(- images=images,- audio=audio,- frame_rate=Fraction(fps),- )- ),)--class GetVideoComponents(ComfyNodeABC):+ def define_schema(cls):+ return io.Schema(+ node_id="CreateVideo",+ display_name="Create Video",+ category="image/video",+ description="Create a video from images.",+ inputs=[+ io.Image.Input("images", tooltip="The images to create a video from."),+ io.Float.Input("fps", default=30.0, min=1.0, max=120.0, step=1.0),+ io.Audio.Input("audio", optional=True, tooltip="The audio to add to the video."),+ ],+ outputs=[+ io.Video.Output(),+ ],+ )++ @classmethod+ def execute(cls, images: ImageInput, fps: float, audio: Optional[AudioInput] = None) -> io.NodeOutput:+ return io.NodeOutput(+ VideoFromComponents(VideoComponents(images=images, audio=audio, frame_rate=Fraction(fps)))+ )++class GetVideoComponents(io.ComfyNode):+ @classmethod+ def define_schema(cls):+ return io.Schema(+ node_id="GetVideoComponents",+ display_name="Get Video Components",+ category="image/video",+ description="Extracts all components from a video: frames, audio, and framerate.",+ inputs=[+ io.Video.Input("video", tooltip="The video to extract components from."),+ ],+ outputs=[+ io.Image.Output(display_name="images"),+ io.Audio.Output(display_name="audio"),+ io.Float.Output(display_name="fps"),+ ],+ )+@classmethod- def INPUT_TYPES(cls):- return {- "required": {- "video": (IO.VIDEO, {"tooltip": "The video to extract components from."}),- }- }- RETURN_TYPES = (IO.IMAGE, IO.AUDIO, IO.FLOAT)- RETURN_NAMES = ("images", "audio", "fps")- FUNCTION = "get_components"-- CATEGORY = "image/video"- DESCRIPTION = "Extracts all components from a video: frames, audio, and framerate."-- def get_components(self, video: Input.Video):+ def execute(cls, video: VideoInput) -> io.NodeOutput:components = video.get_components()- return (components.images, components.audio, float(components.frame_rate))+ return io.NodeOutput(components.images, components.audio, float(components.frame_rate))-class LoadVideo(ComfyNodeABC):+class LoadVideo(io.ComfyNode):@classmethod- def INPUT_TYPES(cls):+ def define_schema(cls):input_dir = folder_paths.get_input_directory()files = [f for f in os.listdir(input_dir) if os.path.isfile(os.path.join(input_dir, f))]files = folder_paths.filter_files_content_types(files, ["video"])- return {"required":- {"file": (sorted(files), {"video_upload": True})},- }-- CATEGORY = "image/video"+ return io.Schema(+ node_id="LoadVideo",+ display_name="Load Video",+ category="image/video",+ inputs=[+ io.Combo.Input("file", options=sorted(files), upload=io.UploadType.video),+ ],+ outputs=[+ io.Video.Output(),+ ],+ )- RETURN_TYPES = (IO.VIDEO,)- FUNCTION = "load_video"- def load_video(self, file):+ @classmethod+ def execute(cls, file) -> io.NodeOutput:video_path = folder_paths.get_annotated_filepath(file)- return (InputImpl.VideoFromFile(video_path),)+ return io.NodeOutput(VideoFromFile(video_path))@classmethod- def IS_CHANGED(cls, file):+ def fingerprint_inputs(s, file):video_path = folder_paths.get_annotated_filepath(file)mod_time = os.path.getmtime(video_path)# Instead of hashing the file, we can just use the modification time to avoid@@ -217,24 +196,23 @@ class LoadVideo(ComfyNodeABC):return mod_time@classmethod- def VALIDATE_INPUTS(cls, file):+ def validate_inputs(s, file):if not folder_paths.exists_annotated_filepath(file):return "Invalid video file: {}".format(file)return True-NODE_CLASS_MAPPINGS = {- "SaveWEBM": SaveWEBM,- "SaveVideo": SaveVideo,- "CreateVideo": CreateVideo,- "GetVideoComponents": GetVideoComponents,- "LoadVideo": LoadVideo,-}--NODE_DISPLAY_NAME_MAPPINGS = {- "SaveVideo": "Save Video",- "CreateVideo": "Create Video",- "GetVideoComponents": "Get Video Components",- "LoadVideo": "Load Video",-}+class VideoExtension(ComfyExtension):+ @override+ async def get_node_list(self) -> list[type[io.ComfyNode]]:+ return [+ SaveWEBM,+ SaveVideo,+ CreateVideo,+ GetVideoComponents,+ LoadVideo,+ ]++async def comfy_entrypoint() -> VideoExtension:+ return VideoExtension()