
    +jX,                        d dl Z d dlZd dlZd dlZd dlZd dlmZ d dlmZ d dl	m
Z
 d dlmZmZmZmZmZmZ d dlmZ  G d dej                  Z G d	 d
ej                  Z G d dej                  Z G d dej                  Z G d dej                  Z G d dej                  Z G d de          ZdefdZdS )    N)Optional)override)Fraction)ComfyExtensioniouiInput	InputImplTypes)argsc                   J    e Zd Zed             Zedej        fd            ZdS )SaveWEBMc                    t          j        ddgdddt           j                            dd          t           j                            d	d
          t           j                            dddg          t           j                            ddddd          t           j                            dddddd          gt           j        j        t           j        j	        gd          S )Nr   zexport webmz	Save WEBMvideoTimageszPRGBA images are saved with their alpha channel as transparency (vp9 codec only).tooltipfilename_prefixComfyUI)defaultcodecvp9av1)optionsfpsg      8@g{Gz?g     @@r   minmaxstepcrfg      @@r   g     O@   zhHigher crf means lower quality with a smaller file size, lower crf means higher quality higher filesize.)r   r   r   r   r   )node_idsearch_aliasesdisplay_namecategoryis_experimentalinputshiddenis_output_node)
r   SchemaImager	   StringComboFloatHiddenpromptextra_pnginfoclss    8/home/wildlama/comfy/ComfyUI/comfy_extras/nodes_video.pydefine_schemazSaveWEBM.define_schema   s    y)?$ x  2D  E  E	 19EEw??ud&tTTudt!  V@  A  A I$bi&=>
 
 
 	
    returnc           	         t          j        |t          j                    |d         j        d         |d         j        d                   \  }}}}	}| d|dd}
t	          j        t          j                            ||
          d          }| j	        j
        &t          j        | j	        j
                  |j        d<   | j	        j        ;| j	        j        D ].}t          j        | j	        j        |                   |j        |<   /|j        d	         d
k    o|dk    }ddd}|                    ||         t!          t#          |dz            d                    }|j        d         |_        |j        d         |_        |rdn	|dk    rdnd|_        d|_        dt-          |          i|_        |dk    r
d|j        d<   |D ]?}|rt          j                            t5          j        |dd d
f         dz  dd                              t5          j        d          t4          j                                                  d           }nt          j                            t5          j        |dd d!f         dz  dd                              t5          j        d          t4          j                                                  d"           }|                     |          D ]}|!                    |           A|!                    |                                            |"                                 tG          j$        tK          j&        tK          j'        |
|	tF          j(        j)                  g          #          S )$Nr   r!   _05z_.webmw)moder0      r   z
libvpx-vp9	libsvtav1)r   r   i  )rateyuva420pr   yuv420p10leyuv420pr    6preset.   )r   r   cpu)devicedtypergba)format   rgb24r   )*folder_pathsget_save_image_pathget_output_directoryshapeavopenospathjoinr(   r0   jsondumpsmetadatar1   
add_streamr   roundwidthheightpix_fmtbit_ratestrr   
VideoFramefrom_ndarraytorchclamptorJ   uint8numpyencodemuxcloser   
NodeOutputr   PreviewVideoSavedResult
FolderTypeoutput)r3   r   r   r   r   r    full_output_folderfilenamecounter	subfolderfile	containerx
save_alpha	codec_mapstreamframepackets                     r4   executezSaveWEBM.execute    s   LXLl\>@@&)/RSBTV\]^V_VefgVhM
 M
IHgy/ //W////GBGLL);TBBMMM	:(+/:cj6G+H+HIx(:#/Z- P P(,
3:3KA3N(O(O	"1%% \"%*=u~
(==	%%i&6XeCRVJFWFWY]=^=^%__|B'R('1gRW]fS*E>>'*FN8$ 	& 	&E p225;uS"1"W~PS?SYZ`c3d3d3d3g3goto{  }B  pC  pC  KP  KV3g  4W  4W  4]  4]  4_  4_  hn2  o  o225;uS"1"W~PS?SYZ`c3d3d3d3g3goto{  }B  pC  pC  KP  KV3g  4W  4W  4]  4]  4_  4_  ho2  p  p --.. & &f%%%%&fmmoo&&&}iQSQ^Qe1f1f0g h hiiiir6   N)__name__
__module____qualname__classmethodr5   r   rn   r    r6   r4   r   r      s]        
 
 [
$ 'j"- 'j 'j 'j ['j 'j 'jr6   r   c                   \    e Zd Zed             Zedej        dedej	        fd            Z
dS )	SaveVideoc                    t          j        ddgddddt           j                            dd          t           j                            d	d
d          t           j                            dt          j                                        dd          t           j                            dt          j	                                        dd          gt           j
        j        t           j
        j        gd	  	        S )Nr   zexport videoz
Save Videor   Basicsz8Saves the input images to your ComfyUI output directory.zThe video to save.r   r   zvideo/ComfyUIzThe 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.r   r   rM   autoz The format to save the video as.)r   r   r   r   zThe codec to use for the video.T)	r"   r#   r$   r%   essentials_categorydescriptionr'   r(   r)   )r   r*   Videor	   r,   r-   r   VideoContaineras_input
VideoCodecr/   r0   r1   r2   s    r4   r5   zSaveVideo.define_schemaK   s    y*+% (Rw0DEE	 1?  Uu  v  vx1E1N1N1P1PZ`  kM  N  Nw0@0I0I0K0KU[  fG  H  H	 I$bi&=>
 
 
 	
r6   r   rM   r7   c           	         |                                 \  }}t          j        |t          j                    ||          \  }}}	}
}d }t          j        s]i }| j        j        |                    | j        j                   | j        j	        | j        j	        |d<   t          |          dk    r|}| d|	ddt          j                            |           }|                    t          j                            ||          t          j        |          ||           t%          j        t)          j        t)          j        ||
t$          j        j                  g                    S )Nr0   r   r9   r:   z_.)rM   r   r\   rP   )get_dimensionsrQ   rR   rS   r   disable_metadatar(   r1   updater0   lenr   r   get_extensionsave_torW   rX   rY   r   rn   r   ro   rp   rq   rr   )r3   r   r   rM   r   r_   r`   rs   rt   ru   rv   saved_metadatar\   rw   s                 r4   r   zSaveVideo.execute^   s^   ,,..vLXLl-//	M
 M
IHgy/ $ 	*Hz'3
 8999z ,%(Z%6"8}}q  !)WWWWWW5+?+M+Mf+U+UWWGLL+T22'//#	 	 	
 	
 	
 }iQSQ^Qe1f1f0g h hiiiir6   N)r   r   r   r   r5   r	   r   rc   r   rn   r   r   r6   r4   r   r   J   sq        
 
 [
$ jEK j# jQSQ^ j j j [j j jr6   r   c            
       x    e Zd Zed             Zeddej        dedeej	                 de
j        fd            ZdS )	CreateVideoc                 >   t          j        ddgddddt           j                            dd	          t           j                            d
dddd          t           j                            ddd          gt           j                                        g          S )Nr   zimages to videozCreate Videor   Video ToolszCreate a video from images.r   z"The images to create a video from.r   r   g      >@g      ?g      ^@r   audioTzThe audio to add to the video.)optionalr   )r"   r#   r$   r%   r   r   r'   outputs)r   r*   r+   r	   r.   Audior   Outputr2   s    r4   r5   zCreateVideo.define_schema|   s    y!-.' -5x1UVVudSQQw?_`` !!
 
 
 	
r6   Nr   r   r   r7   c                     t          j        t          j        t	          j        ||t          |                                        S )N)r   r   
frame_rate)r   rn   r
   VideoFromComponentsr   VideoComponentsr   )r3   r   r   r   s       r4   r   zCreateVideo.execute   sA    })%*?vUZgopsgtgt*u*u*uvv
 
 	
r6   N)r   r   r   r   r5   r	   r+   floatr   r   r   rn   r   r   r6   r4   r   r   {   s{        
 
 [
$ 
 
U[ 
u 
Xek=R 
^`^k 
 
 
 [
 
 
r6   r   c                   X    e Zd Zed             Zedej        dej        fd            Z	dS )GetVideoComponentsc                 6   t          j        dg ddddt           j                            dd          gt           j                            d	          t           j                            d
	          t           j                            d	          g          S )Nr   )zextract frameszsplit videozvideo to imagesdemuxzGet Video Componentsr   zCExtracts all components from a video: frames, audio, and framerate.z%The video to extract components from.r   r   )r$   r   r   )r"   r#   r$   r%   r   r'   r   )r   r*   r   r	   r+   r   r   r.   r2   s    r4   r5   z GetVideoComponents.define_schema   s    y(XXX/]w0WXX X66W55U33
 
 
 	
r6   r   r7   c                     |                                 }t          j        |j        |j        t          |j                            S r   )get_componentsr   rn   r   r   r   r   )r3   r   
componentss      r4   r   zGetVideoComponents.execute   s8    ))++
}Z.
0@%
H]B^B^___r6   N)
r   r   r   r   r5   r	   r   r   rn   r   r   r6   r4   r   r      sg        
 
 [
" `EK `BM ` ` ` [` ` `r6   r   c                   v    e Zd Zed             Zedej        fd            Zed             Zed             Z	dS )	LoadVideoc                    t          j                    fdt          j                  D             }t          j        |dg          }t          j        dg ddddt
          j                            dt          |          t
          j
        j                  gt
          j                                        g	          S )
Nc                     g | ]A}t           j                            t           j                            |                    ?|BS r   )rW   rX   isfilerY   ).0f	input_dirs     r4   
<listcomp>z+LoadVideo.define_schema.<locals>.<listcomp>   s>    ```qRW^^BGLLQZ\]D^D^5_5_````r6   r   r   )zimport videoz
open videoz
video filez
Load Videor   rw   )r   upload)r"   r#   r$   r%   r   r'   r   )rQ   get_input_directoryrW   listdirfilter_files_content_typesr   r*   r-   r	   sorted
UploadTyper   r   r   )r3   filesr   s     @r4   r5   zLoadVideo.define_schema   s     466	````BJy11```7yIIyGGG% (vve}}R]EXYY !!
 
 
 	
r6   r7   c                 v    t          j        |          }t          j        t	          j        |                    S r   )rQ   get_annotated_filepathr   rn   r
   VideoFromFile)r3   rw   
video_paths      r4   r   zLoadVideo.execute   s-    !8>>
}Y4Z@@AAAr6   c                 l    t          j        |          }t          j                            |          }|S r   )rQ   r   rW   rX   getmtime)srw   r   mod_times       r4   fingerprint_inputszLoadVideo.fingerprint_inputs   s.    !8>>
7##J// r6   c                 X    t          j        |          sd                    |          S dS )NzInvalid video file: {}T)rQ   exists_annotated_filepathrM   )r   rw   s     r4   validate_inputszLoadVideo.validate_inputs   s.    5d;; 	9+224888tr6   N)
r   r   r   r   r5   r   rn   r   r   r   r   r6   r4   r   r      s        
 
 [
$ Bbm B B B [B   [   [  r6   r   c                   n    e Zd Zed             Zedej        j        dedede	dej
        f
d            ZdS )	
VideoSlicec                    t          j        ddg dddt           j                            d          t           j                            ddddd	d
          t           j                            dddd	d          t           j                            ddd          gt           j                                        g          S )NzVideo Slice)ztrim video durationzskip first frameszframe load capz
start timer   r   
start_timeg        g     j@g     jgMbP?zStart time in seconds)r   r   r   r   r   durationz0Duration in seconds, or 0 for unlimited duration)r   r   r   r   strict_durationFzNIf True, when the specified duration is not possible, an error will be raised.r   )r"   r$   r#   r%   r   r'   r   )r   r*   r   r	   r.   Booleanr   r2   s    r4   r5   zVideoSlice.define_schema   s    y!&    -w'' 3    N    
  %!l !  #0 !!E%
 %
 %
 %	
r6   r   r   r   r   r7   c                     |                     |||          }|t          j        |          S t          d|                                 d| d|           )N)r   z(Failed to slice video:
Source duration: z
Start time: z
Target duration: )
as_trimmedr   rn   
ValueErrorget_duration)r3   r   r   r   r   trimmeds         r4   r   zVideoSlice.execute   s    "":x"YY=))) F8J8J8L8L  F  F\f  F  F  |D  F  F
 
 	
r6   N)r   r   r   r   r5   r   r   Typer   boolrn   r   r   r6   r4   r   r      sz        &
 &
 [&
P 
BHM 
u 
 
`d 
ikiv 
 
 
 [
 
 
r6   r   c                   L    e Zd Zedeeej                          fd            ZdS )VideoExtensionr7   c                 R   K   t           t          t          t          t          t
          gS r   )r   r   r   r   r   r   )selfs    r4   get_node_listzVideoExtension.get_node_list  s$       
 	
r6   N)	r   r   r   r   listtyper   	ComfyNoder   r   r6   r4   r   r   
  sE        
T$r|*<%= 
 
 
 X
 
 
r6   r   r7   c                  "   K   t                      S r   )r   r   r6   r4   comfy_entrypointr     s      r6   )rW   rU   rf   rQ   rZ   typingr   typing_extensionsr   	fractionsr   comfy_api.latestr   r   r   r	   r
   r   comfy.cli_argsr   r   r   r   r   r   r   r   r   r   r   r6   r4   <module>r      s    				 				             & & & & & &       L L L L L L L L L L L L L L L L      <j <j <j <j <jr| <j <j <j|.j .j .j .j .j .j .j .jb
 
 
 
 
", 
 
 
4` ` ` ` ` ` ` `2& & & & & & & &P1
 1
 1
 1
 1
 1
 1
 1
h

 

 

 

 

^ 

 

 

      r6   