o
    iQ                     @   s  d dl Z d dlmZ d dlmZmZmZ d dlmZm	Z	m
Z
mZmZ d dlmZmZmZmZmZmZmZmZmZmZ dZdZdZd	Zd
ZdZdZdZdZ dZ!dZ"dZ#dZ$dZ%dZ&dede'fddZ(d8ddZ)dej*dej*fddZ+dej*de,e-e-f fddZ.de-de-ddfddZ/dej*dej*fddZ0d e1ddfd!d"Z2dej*d e1dej*fd#d$Z3d%e4fd&d'Z5d(d) Z6d*e7ej8 d+e4defd,d-Z9G d.d/ d/ej8Z:G d0d1 d1ej8Z;G d2d3 d3ej8Z<G d4d5 d5eZ=de=fd6d7Z>dS )9    N)override)IOComfyExtensionInput)MoonvalleyPromptResponse$MoonvalleyTextToVideoInferenceParamsMoonvalleyTextToVideoRequest%MoonvalleyVideoToVideoInferenceParamsMoonvalleyVideoToVideoRequest)
ApiEndpointdownload_url_to_video_outputpoll_opsync_op
trim_videoupload_images_to_comfyapiupload_video_to_comfyapi validate_container_format_is_mp4validate_image_dimensionsvalidate_stringz/proxy/moonvalley/uploadsz/proxy/moonvalley/promptsz(/proxy/moonvalley/prompts/video-to-videoz'/proxy/moonvalley/prompts/text-to-videoz(/proxy/moonvalley/prompts/image-to-video,  i'  i   @i  responsereturnc                 C   s
   t | jS )z6Verifies that the initial response contains a task ID.)boolid)r    r   >/mnt/c/Users/fbmor/ComfyUI/comfy_api_nodes/nodes_moonvalley.pyis_valid_task_creation_response1   s   
r   c                 C   s8   t | sd| j d| j d|  }t| t|d S )Nz4Moonvalley Marey API: Initial request failed. Code: z, Message: z, Data: )r   codemessageloggingerrorRuntimeError)r   	error_msgr   r   r   validate_task_creation_response6   s
   
r#   videoc                 C   s&   t | \}}t|| t|  t| S )aF  
    Validates and processes video input for Moonvalley Video-to-Video generation.

    Args:
        video: Input video to validate

    Returns:
        Validated and potentially trimmed video

    Raises:
        ValueError: If video doesn't meet requirements
        MoonvalleyApiError: If video duration is too short
    )_get_video_dimensions_validate_video_dimensionsr   _validate_and_trim_duration)r$   widthheightr   r   r   validate_video_to_video_input=   s   
r*   c              
   C   s@   z|   W S  ty } ztd| td| |d}~ww )z.Extracts video dimensions with error handling.z%Error getting dimensions of video: %szCannot get video dimensions: N)get_dimensions	Exceptionr   r    
ValueError)r$   er   r   r   r%   R   s   
r%   r(   r)   c                 C   sJ   h d}| |f|vr#d dd t|D }td|  d| d| dS )	z<Validates video dimensions meet Moonvalley V2V requirements.>        8    r1   r1   r1   r0   r4   r3   z, c                 S   s   g | ]\}}| d | qS )xr   ).0whr   r   r   
<listcomp>f   s    z._validate_video_dimensions.<locals>.<listcomp>zResolution r8   z not supported. Supported: N)joinsortedr-   )r(   r)   supported_resolutionssupported_listr   r   r   r&   [   s
   r&   c                 C   s   |   }t| t| |S )z:Validates video duration and trims to 5 seconds if needed.)get_duration_validate_minimum_duration_trim_if_too_longr$   durationr   r   r   r'   j   s   
r'   rE   c                 C   s   | dk rt ddS )z)Ensures video is at least 5 seconds long.   z,Input video must be at least 5 seconds long.N)r-   )rE   r   r   r   rB   q   s   rB   c                 C   s   |dkr	t | dS | S )z#Trims video to 5 seconds if longer.rF   )r   rD   r   r   r   rC   w   s   
rC   
resolutionc                 C   s@   dddddddddddddddd}| | dddS )Nr4   r3   )r(   r)   r1   r0   16:9 (1920 x 1080)9:16 (1080 x 1920)1:1 (1152 x 1152)4:3 (1536 x 1152)3:4 (1152 x 1536)get)rG   res_mapr   r   r   parse_width_height_from_res~   s   rQ   c                 C   s   ddddd}| | |d S )Nmotion_controlcanny_controlpose_controldepth_control)Motion TransferCannyPose TransferDepthrV   rN   )valuecontrol_mapr   r   r   parse_control_parameter   s   r\   clstask_idc                    s0   t | tt d| dtdd dddI d H S )N/)pathc                 S   s   | r| j r| j S d S N)status)rr   r   r   <lambda>   s    zget_response.<locals>.<lambda>g      0@   )response_modelstatus_extractorpoll_intervalmax_poll_attempts)r   r   API_PROMPTS_ENDPOINTr   )r]   r^   r   r   r   get_response   s   rk   c                   @   sR   e Zd ZedejfddZedejde	de	de	de
d	ed
edejfddZdS )MoonvalleyImg2VideoNoder   c                 C   s   t jddddt jjdddt jjdd	d
t jjdd	dddt jjdg ddddt jjdddddddt jjdddddt jj	dd	d t jjd!d"d#d$dd%dgt j
 gt jjt jjt jjgd	t jt  d&d'd(	S ))Nrl   zMoonvalley Marey Image to Videoapi node/video/Moonvalley Mareyz$Moonvalley Marey Image to Video Nodeimagez.The reference image used to generate the videotooltippromptT	multilinenegative_prompt  <synthetic> <scene cut> gopro, bright, contrast, static, overexposed, vignette, artifacts, still, noise, texture, scanlines, videogame, 360 camera, VR, transition, flare, saturation, distorted, warped, wide angle, saturated, vibrant, glowing, cross dissolve, cheesy, ugly hands, mutated hands, mutant, disfigured, extra fingers, blown out, horrible, blurry, worst quality, bad, dissolve, melt, fade in, fade out, wobbly, weird, low quality, plastic, stock footage, video camera, boringNegative prompt textrs   defaultrp   rG   rH   rI   Resolution of the output videooptionsrx   rp   prompt_adherence      @      ?      4@%Guidance scale for generation controlrx   minmaxsteprp   seed	   r          Random seed valuerx   r   r   r   display_moderp   control_after_generatestepsP   K   d   zNumber of denoising steps{"type":"usd","usd": 1.5}
depends_onexpr	node_iddisplay_namecategorydescriptioninputsoutputshiddenis_api_nodeprice_badge)r   SchemaImager   StringComboFloatIntNumberDisplaynumberVideoOutputHiddenauth_token_comfy_orgapi_key_comfy_org	unique_id
PriceBadgePriceBadgeDependsr]   r   r   r   define_schema   s~   

<z%MoonvalleyImg2VideoNode.define_schemarn   rq   rt   rG   r|   r   r   c              	      s   t |ddttd t|dtd t|dtd t|}t|||||d |d d	d
}	d}
t| |d|
dI d H d }t| t	t
ddtt|||	ddI d H }t| t| |jI d H }t|jI d H }t|S )Nr   )	min_width
min_height
max_height	max_widthr   
min_length
max_lengthrt   
field_namer   r(   r)   T)rt   r   r   guidance_scaler(   r)   use_negative_promptsz	image/png)
max_images	mime_typer   POSTr`   method)	image_urlprompt_textinference_paramsendpointrf   data)r   
MAX_HEIGHT	MAX_WIDTHr   "MOONVALLEY_MAREY_MAX_PROMPT_LENGTHrQ   r   r   r   r   API_IMG2VIDEO_ENDPOINTr   r   r#   rk   r   r   
output_urlr   
NodeOutput)r]   rn   rq   rt   rG   r|   r   r   width_heightr   r   r   task_creation_responsefinal_responser$   r   r   r   execute   s8   

zMoonvalleyImg2VideoNode.executeN)__name__
__module____qualname__classmethodr   r   r   r   r   strfloatintr   r   r   r   r   r   rl      s*    O	rl   c                   @   sb   e Zd ZedejfddZe					dd	ed
edede	j
dB dededB dejfddZdS )MoonvalleyVideo2VideoNoder   c                 C   s   t jddddt jjddddt jjd	dd
ddt jjdddddt jjdddt jjdddt jjdddgdddt jjddddddddt jjd d!d!ddt jjd"d#gt j	 gt j
jt j
jt j
jgdt jt  d$d%d&	S )'Nr   zMoonvalley Marey Video to Videorm    rq   TzDescribes the video to generate)rs   rp   rt   ru   rv   rw   r   r   r   r   r   r   Fr   r$   zThe reference video used to generate the output video. Must be at least 5 seconds long. Videos longer than 5s will be automatically trimmed. Only MP4 format supported.ro   control_typerV   rX   )r{   rx   optionalmotion_intensityr   z.Only used if control_type is 'Motion Transfer')rx   r   r   r   rp   r   r   <   zNumber of inference steps)rx   r   r   r   r   rp   z{"type":"usd","usd": 2.25}r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r     s   
	
9z'MoonvalleyVideo2VideoNode.define_schemaNrV   r   r   r}   rq   rt   r   r$   r   r   c	              
      s   t |}	t| |	I d H }
t|dtd t|dtd i }|dkr)|d ur)||d< t|||||d}t| ttdd	tt	t
||
||d
dI d H }t| t| |jI d H }tt|jI d H S )Nr   r   rt   r   rV   r   )rt   r   control_paramsr   r   r   r   )r   	video_urlr   r   r   )r*   r   r   r   r	   r   r   API_VIDEO2VIDEO_ENDPOINTr   r
   r\   r#   rk   r   r   r   r   r   )r]   rq   rt   r   r$   r   r   r   r|   validated_videor   r   r   r   r   r   r   r   r   l  s:   
z!MoonvalleyVideo2VideoNode.execute)NrV   r   r   r}   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r     s0    L
r   c                   @   sL   e Zd ZedejfddZededededede	d	e	dej
fd
dZdS )MoonvalleyTxt2VideoNoder   c                 C   s   t jddddt jjdddt jjddd	d
dt jjdg ddddt jjdddddddt jjdddddt jjdddt jjddd d!dd"dgt j	
 gt jjt jjt jjgdt jt  d#d$d%	S )&Nr   zMoonvalley Marey Text to Videorm   r   rq   Trr   rt   ru   rv   rw   rG   )rI   rJ   rK   rL   rM   z21:9 (2560 x 1080)rI   ry   rz   r|   g      @r~   r   r   r   r   r   r   r   r   r   )rx   r   r   r   r   r   rp   r   r   r   r   zInference stepsr   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r     sv   

8z%MoonvalleyTxt2VideoNode.define_schemarq   rt   rG   r|   r   r   c              	      s   t |dtd t |dtd t|}t||||d|d |d d}t| ttd	d
tt||ddI d H }	t	|	 t
| |	jI d H }
tt|
jI d H S )Nr   r   rt   r      r(   r)   )rt   r   r   r   
num_framesr(   r)   r   r   )r   r   r   )r   r   rQ   r   r   r   API_TXT2VIDEO_ENDPOINTr   r   r#   rk   r   r   r   r   r   )r]   rq   rt   rG   r|   r   r   r   r   r   r   r   r   r   r     s,   



zMoonvalleyTxt2VideoNode.executeN)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r     s&    Kr   c                   @   s(   e Zd Zedeeej  fddZdS )MoonvalleyExtensionr   c                    s   t ttgS ra   )rl   r   r   )selfr   r   r   get_node_list  s
   z!MoonvalleyExtension.get_node_listN)	r   r   r   r   listtyper   	ComfyNoder   r   r   r   r   r     s    r   c                      s   t  S ra   )r   r   r   r   r   comfy_entrypoint  s   r   )r   N)?r   typing_extensionsr   comfy_api.latestr   r   r   comfy_api_nodes.apis.moonvalleyr   r   r   r	   r
   comfy_api_nodes.utilr   r   r   r   r   r   r   r   r   r   API_UPLOADS_ENDPOINTrj   r   r   r   	MIN_WIDTH
MIN_HEIGHTr   r   MIN_VID_WIDTHMIN_VID_HEIGHTMAX_VID_WIDTHMAX_VID_HEIGHTMAX_VIDEO_SIZEr   r   r   r#   r   r*   tupler   r%   r&   r'   r   rB   rC   r   rQ   r\   r   r   rk   rl   r   r   r   r   r   r   r   r   <module>   sH    0
	
}}q
