o
    i*                     @  s  d dl mZ 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	 d	ejZG d
d dejZG dd dejZG dd dejZG dd dejZG dd dejZG dd deZdddZdS )    )annotationsN)Optional)override)Fraction)ComfyExtensioniouiInput	InputImplTypes)argsc                   @  s&   e Zd Zedd ZedddZdS )	SaveWEBMc                 C  sz   t jddgddt jdt jjdddt jjd	d
dgdt jjddddddt jjdddddddgt jjt jj	gddS )Nr   zexport webmimage/videoTimages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   tooltip)node_idsearch_aliasescategoryis_experimentalinputshiddenis_output_node)
r   SchemaImager	   StringComboFloatHiddenpromptextra_pnginfocls r0   6/mnt/c/Users/fbmor/ComfyUI/comfy_extras/nodes_video.pydefine_schema   s   
zSaveWEBM.define_schemareturnio.NodeOutputc                 C  s  t |t  |d jd |d jd \}}}}	}| d|dd}
tjtj||
dd}| j	j
d ur>t| j	j
|jd< | j	jd urW| j	jD ]}t| j	j| |j|< qHd	d
d}|j|| tt|d dd}|jd |_|jd |_|dkr~dnd|_d|_dt|i|_|dkrd|jd< |D ]0}tjjtj|dd df d dddjtdtjd dd}| |D ]}|!| qq|!|   |"  t#j$t%&t%'|
|	t#j(j)gdS )Nr   r   _05z_.webmw)moder,   z
libvpx-vp9	libsvtav1)r   r   i  )rater   yuv420p10leyuv420pr   6preset.      )r   r   cpu)devicedtypergb24)formatr   )*folder_pathsget_save_image_pathget_output_directoryshapeavopenospathjoinr$   r,   jsondumpsmetadatar-   
add_streamr   roundwidthheightpix_fmtbit_ratestrr   
VideoFramefrom_ndarraytorchclamptorD   uint8numpyencodemuxcloser   
NodeOutputr   PreviewVideoSavedResult
FolderTypeoutput)r/   r   r   r   r   r   full_output_folderfilenamecounter	subfolderfile	containerx	codec_mapstreamframepacketr0   r0   r1   execute!   s6    
 
B"zSaveWEBM.executeNr3   r4   __name__
__module____qualname__classmethodr2   rv   r0   r0   r0   r1   r      
    
r   c                   @  s&   e Zd Zedd Zedd	d
ZdS )	SaveVideoc                 C  sx   t jddgddddt jjddd	t jjd
dddt jjdtj dddt jjdtj	 dddgt j
jt j
jgdd	S )Nr~   zexport videoz
Save Videor   Basicsz8Saves the input images to your ComfyUI output directory.videoz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   rG   autoz The format to save the video as.)r   r   r   r   zThe codec to use for the video.T)	r   r    display_namer!   essentials_categorydescriptionr#   r$   r%   )r   r&   Videor	   r(   r)   r   VideoContaineras_input
VideoCodecr+   r,   r-   r.   r0   r0   r1   r2   E   s   zSaveVideo.define_schemar   Input.VideorG   r[   r3   r4   c                 C  s   |  \}}t|t ||\}}}	}
}d }tjs=i }| jjd ur)|| jj | jj	d ur5| jj	|d< t
|dkr=|}| d|	ddtj| }|jtj||t|||d tjtt||
tjjgdS )Nr,   r   r5   r6   z_.)rG   r   rT   rH   )get_dimensionsrI   rJ   rK   r   disable_metadatar$   r-   updater,   lenr   r   get_extensionsave_torO   rP   rQ   r   rf   r   rg   rh   ri   rj   )r/   r   r   rG   r   rW   rX   rk   rl   rm   rn   saved_metadatarT   ro   r0   r0   r1   rv   X   s0   "zSaveVideo.executeN)r   r   rG   r[   r3   r4   rx   r0   r0   r0   r1   r~   D   s
    
r~   c                   @  s(   e Zd Zedd ZeddddZdS )CreateVideoc                 C  sT   t jddgdddt jjdddt jjd	d
ddddt jjddddgt j gdS )Nr   zimages to videozCreate Videor   z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#   outputs)r   r&   r'   r	   r*   Audior   Outputr.   r0   r0   r1   r2   v   s   zCreateVideo.define_schemaNr   Input.Imager   floatr   Optional[Input.Audio]r3   r4   c              	   C  s    t ttj||t|dS )N)r   r   
frame_rate)r   rf   r
   VideoFromComponentsr   VideoComponentsr   )r/   r   r   r   r0   r0   r1   rv      s   zCreateVideo.executeN)r   r   r   r   r   r   r3   r4   rx   r0   r0   r0   r1   r   u   s
    
r   c                   @  s&   e Zd Zedd Zed
ddZd	S )GetVideoComponentsc                 C  sN   t jdg ddddt jjdddgt jjd	d
t jjdd
t jjdd
gdS )Nr   )zextract frameszsplit videozvideo to imagesdemuxzGet Video Componentsr   zCExtracts all components from a video: frames, audio, and framerate.r   z%The video to extract components from.r   r   )r   r   r   r   )r   r&   r   r	   r'   r   r   r*   r.   r0   r0   r1   r2      s   z GetVideoComponents.define_schemar   r   r3   r4   c                 C  s    |  }t|j|jt|jS r   )get_componentsr   rf   r   r   r   r   )r/   r   
componentsr0   r0   r1   rv      s   zGetVideoComponents.executeN)r   r   r3   r4   rx   r0   r0   r0   r1   r      r}   r   c                   @  s>   e Zd Zedd ZedddZedd Zed	d
 ZdS )	LoadVideoc                   sj   t    fddt D }t |dg}tjdg ddddtjjd	t	|tj
jd
gtj gdS )Nc                   s&   g | ]}t jt j |r|qS r0   )rO   rP   isfilerQ   ).0f	input_dirr0   r1   
<listcomp>   s   & z+LoadVideo.define_schema.<locals>.<listcomp>r   r   )zimport videoz
open videoz
video filez
Load Videor   r   ro   )r   upload)r   r    r   r!   r   r#   r   )rI   get_input_directoryrO   listdirfilter_files_content_typesr   r&   r)   r	   sorted
UploadTyper   r   r   )r/   filesr0   r   r1   r2      s   zLoadVideo.define_schemar3   r4   c                 C  s   t |}tt|S r   )rI   get_annotated_filepathr   rf   r
   VideoFromFile)r/   ro   
video_pathr0   r0   r1   rv      s   
zLoadVideo.executec                 C  s   t |}tj|}|S r   )rI   r   rO   rP   getmtime)sro   r   mod_timer0   r0   r1   fingerprint_inputs   s   
zLoadVideo.fingerprint_inputsc                 C  s   t |s
d|S dS )NzInvalid video file: {}T)rI   exists_annotated_filepathrG   )r   ro   r0   r0   r1   validate_inputs   s   

zLoadVideo.validate_inputsNrw   )ry   rz   r{   r|   r2   rv   r   r   r0   r0   r0   r1   r      s    

r   c                   @  s&   e Zd Zedd ZedddZdS )
VideoSlicec                 C  sh   t jddg dddt jdt jjdddd	d
ddt jjdddd
ddt jjddddgt j gdS )NzVideo Slice)ztrim video durationzskip first frameszframe load capz
start timer   zVideo Toolsr   
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   r.   r0   r0   r1   r2      s>   
zVideoSlice.define_schemar   io.Video.Typer   r   r   r   boolr3   r4   c                 C  s@   |j |||d}|d urt|S td|  d| d| )N)r   z(Failed to slice video:
Source duration: z
Start time: z
Target duration: )
as_trimmedr   rf   
ValueErrorget_duration)r/   r   r   r   r   trimmedr0   r0   r1   rv      s   
zVideoSlice.executeN)
r   r   r   r   r   r   r   r   r3   r4   rx   r0   r0   r0   r1   r      s
    
(r   c                   @  s   e Zd ZedddZdS )VideoExtensionr3   list[type[io.ComfyNode]]c                   s   t tttttgS r   )r   r~   r   r   r   r   )selfr0   r0   r1   get_node_list  s   zVideoExtension.get_node_listN)r3   r   )ry   rz   r{   r   r   r0   r0   r0   r1   r     s    r   r3   c                     s   t  S r   )r   r0   r0   r0   r1   comfy_entrypoint  s   r   )r3   r   )
__future__r   rO   rM   r^   rI   rR   typingr   typing_extensionsr   	fractionsr   comfy_api.latestr   r   r   r	   r
   r   comfy.cli_argsr   	ComfyNoder   r~   r   r   r   r   r   r   r0   r0   r0   r1   <module>   s&     61(4