o
    igT                     @   s   d dl Z d dlmZ d dlmZ d dl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 d dlmZmZmZmZmZ dZdd	d
dddZG dd dejZG dd deZG dd dejZG dd deZdefddZdS )    N)BytesIO)override)IOComfyExtensionInput	InputImpl)VeoGenVidPollRequestVeoGenVidPollResponseVeoGenVidRequestVeoGenVidResponseVeoRequestInstanceVeoRequestInstanceImageVeoRequestParameters)ApiEndpointdownload_url_to_video_outputpoll_opsync_optensor_to_base64_string    veo-2.0-generate-001zveo-3.1-generate-previewzveo-3.1-fast-generate-previewveo-3.0-generate-001veo-3.0-fast-generate-001)r   veo-3.1-generateveo-3.1-fast-generater   r   c                   @   s<   e Zd ZdZedd Ze								
		dddZd
S )VeoVideoGenerationNodez
    Generates videos from text prompts using Google's Veo API.

    This node can create videos from text descriptions and optional image inputs,
    with control over parameters like aspect ratio, duration, and more.
    c                 C   s  t jddddt jjddddd	t jjd
ddgdddt jj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t jjdddgdddddt jjd d!d!d"dt jjdd#dd$	t j	jd%d&dd't jjd(d)gd)d*dd+g	t j
 gt jjt jjt jjgdt jt jdgd,d-d.d/	S )0Nr   zGoogle Veo 2 Video Generationapi node/video/Veoz;Generates videos from text prompts using Google's Veo 2 APIpromptT Text description of the video	multilinedefaulttooltipaspect_ratio16:99:16 Aspect ratio of the output videooptionsr!   r"   negative_prompt8Negative text prompt to guide what to avoid in the videor    r!   r"   optionalduration_seconds         'Duration of the output video in secondsr!   minmaxstepdisplay_moder"   r,   enhance_promptz0Whether to enhance the prompt with AI assistancer!   r"   r,   advancedperson_generationALLOWBLOCK/Whether to allow generating people in the videor(   r!   r"   r,   r9   seedr       (Seed for video generation (0 for random)r!   r3   r4   r5   r6   control_after_generater"   r,   image2Optional reference image to guide video generationr"   r,   modelr   z'Veo 2 model to use for video generationr(   r!   r"   r,   widgetsz4{"type":"usd","usd": 0.5 * widgets.duration_seconds}
depends_onexpr	node_iddisplay_namecategorydescriptioninputsoutputshiddenis_api_nodeprice_badger   SchemaStringr   ComboIntNumberDisplaynumberBooleanImageVideoOutputHiddenauth_token_comfy_orgapi_key_comfy_org	unique_id
PriceBadgePriceBadgeDependscls rk   8/mnt/c/Users/fbmor/ComfyUI/comfy_api_nodes/nodes_veo2.pydefine_schema*   s   
Fz$VeoVideoGenerationNode.define_schemar$   r   r.   Tr;   r   Nr   Fc              	      s*  t |	 }	g }d|i}|d urt|}|r|dd|d< || ||||d}|r.||d< |dkr6||d< |	d	d
krE|
|d< d|d< t| td|	 dddtt||ddI d H }dd }t| td|	 dddt	|t
|jddtdI d H }|jrtd|jj d|jj dt|jdr|jjdkrt|jdr|jjr|jjd }d| d |jj d!}t|d"|jj d!}t||jrt|jd#r|jjrt|jjdkr|jjd }t|d$r|jrtttt|jS t|d%r|jrtt |jI d H S td&td')(Nr   	image/pngbytesBase64EncodedmimeTyperD   )aspectRatiopersonGenerationdurationSecondsenhancePromptnegativePromptr   r?   zveo-2.0generateAudioTru   /proxy/veo/	/generatePOSTpathmethod	instances
parametersresponse_modeldatac                 S      | j rdS dS N	completedpendingdone)responserk   rk   rl   status_extractor   s   z8VeoVideoGenerationNode.execute.<locals>.status_extractor/polloperationName      @r   r   r   poll_intervalestimated_durationVeo API error:  (code: )raiMediaFilteredCountraiMediaFilteredReasonsz7Content filtered by Google's Responsible AI practices:  (z videos filtered.)z7Content filtered by Google's Responsible AI practices (videosrp   gcsUri.Video returned but no data or URL was provided4Video generation completed but no video was returned)!
MODELS_MAPr   appendfindr   r   r   r
   r   r	   r   nameAVERAGE_DURATION_VIDEO_GENerror	Exceptionmessagecodehasattrr   r   r   r   lenrp   r   
NodeOutputr   VideoFromFiler   base64	b64decoder   r   )rj   r   r#   r)   r-   r7   r:   r?   rD   rG   generate_audior   instanceimage_base64r   initial_responser   poll_responsereasonerror_messagevideork   rk   rl   execute   s   




zVeoVideoGenerationNode.execute)	r$   r   r.   Tr;   r   Nr   F)__name__
__module____qualname____doc__classmethodrm   r   rk   rk   rk   rl   r   "   s    
Zr   c                   @   s   e Zd ZdZedd ZdS )Veo3VideoGenerationNodea  
    Generates videos from text prompts using Google's Veo 3 API.

    Supported models:
    - veo-3.0-generate-001
    - veo-3.0-fast-generate-001

    This node extends the base Veo node with Veo 3 specific features including
    audio generation and fixed 8-second duration.
    c                 C   s.  t jddddt jjddddd	t jjd
ddgdddt jj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t jjdddgdddddt jjdd d d!dt jjdd"dd#	t j	jd$d%dd&t jjd'g d(d)d*dd+t jjd,d-d.dd/g
t j
 gt jjt jjt jjgdt jt jd'd,gd0d1d2d3	S )4Nr   zGoogle Veo 3 Video Generationr   z;Generates videos from text prompts using Google's Veo 3 APIr   Tr   r   r   r#   r$   r%   r&   r'   r)   r*   r+   r-   r/   r0   zGDuration of the output video in seconds (Veo 3 only supports 8 seconds)r2   r7   z)This parameter is deprecated and ignored.r8   r:   r;   r<   r=   r>   r?   r   r@   rA   rB   rD   rE   rF   rG   )r   r   r   r   r   z'Veo 3 model to use for video generationrH   r   Fz<Generate audio for the video. Supported by all Veo 3 models.)r!   r"   r,   rI   a  
                (
                  $m := widgets.model;
                  $a := widgets.generate_audio;
                  ($contains($m,"veo-3.0-fast-generate-001") or $contains($m,"veo-3.1-fast-generate"))
                    ? {"type":"usd","usd": ($a ? 1.2 : 0.8)}
                    : ($contains($m,"veo-3.0-generate-001") or $contains($m,"veo-3.1-generate"))
                      ? {"type":"usd","usd": ($a ? 3.2 : 1.6)}
                      : {"type":"range_usd","min_usd":0.8,"max_usd":3.2}
                )
                rK   rN   rX   ri   rk   rk   rl   rm     s   
Qz%Veo3VideoGenerationNode.define_schemaN)r   r   r   r   r   rm   rk   rk   rk   rl   r      s    r   c                   @   sR   e Zd Zedd Zededededededed	ejd
ejdede	fddZ
dS )Veo3FirstLastFrameNodec                 C   s  t 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gdt jjdddgdddt jjdddddt jj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#t jjd&d'd(gd(d)t j
jd*dd+d,g
t j gt jjt jjt jjgdt jt jg d-d.d/d0d1	S )2Nr   z&Google Veo 3 First-Last-Frame to Videor   z6Generate video using prompt and first and last frames.r   Tr   r   r   r)   r*   
resolution720p1080p)r(   r#   r$   r%   r&   r'   durationr/         r1   )r!   r3   r4   r5   r6   r"   r?   r   r@   r0   zSeed for video generation)r!   r3   r4   r5   r6   rC   r"   first_framezStart frame)r"   
last_framez	End framerG   r   r   )r(   r!   r   zGenerate audio for the video.)r!   r"   )rG   r   r   rI   a  
                (
                  $prices := {
                    "veo-3.1-fast-generate": { "audio": 0.15, "no_audio": 0.10 },
                    "veo-3.1-generate":      { "audio": 0.40, "no_audio": 0.20 }
                  };
                  $m := widgets.model;
                  $ga := (widgets.generate_audio = "true");
                  $seconds := widgets.duration;
                  $modelKey :=
                    $contains($m, "veo-3.1-fast-generate") ? "veo-3.1-fast-generate" :
                    $contains($m, "veo-3.1-generate")      ? "veo-3.1-generate" :
                    "";
                  $audioKey := $ga ? "audio" : "no_audio";
                  $modelPrices := $lookup($prices, $modelKey);
                  $pps := $lookup($modelPrices, $audioKey);
                  ($pps != null)
                    ? {"type":"usd","usd": $pps * $seconds}
                    : {"type":"range_usd","min_usd": 0.4, "max_usd": 3.2}
                )
                rK   rN   )r   rY   rZ   r   r[   r\   r]   sliderr^   r`   r_   ra   rb   rc   rd   re   rf   rg   rh   ri   rk   rk   rl   rm   y  s   	
5z$Veo3FirstLastFrameNode.define_schemar   r)   r   r#   r   r?   r   r   rG   r   c                    s  t |	 }	t| td|	 dddttt|tt|ddtt|dddgt|d|d	||
||d
ddI d H }t	| td|	 dddt
dd t|jddtdI d H }|jrftd|jj d|jj d|j}|j}|r|jprg }|r|d|d  nd}td| d| d|dkrdnd d|jr|jd }|jrtttt|jS |jrtt|jI d H S tdtd )!Nry   rz   r{   r|   rn   ro   )r   rD   	lastFramer;   T)rr   rs   rt   ru   r?   rx   rv   r   r   r   r   c                 S   r   r   r   )rrk   rk   rl   <lambda>  s    z0Veo3FirstLastFrameNode.execute.<locals>.<lambda>r   r   r   r   r   r   z: r   r   z2Content blocked by Google's Responsible AI filtersr   z videor0   sz filtered).r   r   ) r   r   r   r   r
   r   r   r   r   r   r	   r   r   r   r   r   r   r   r   r   r   r   rp   r   r   r   r   r   r   r   r   r   )rj   r   r)   r   r#   r   r?   r   r   rG   r   r   r   r   filtered_countreasonsreason_partr   rk   rk   rl   r     s|   

zVeo3FirstLastFrameNode.executeN)r   r   r   r   rm   strintr   r`   boolr   rk   rk   rk   rl   r   w  s2    
]	
r   c                   @   s(   e Zd Zedeeej  fddZdS )VeoExtensionreturnc                    s   t ttgS N)r   r   r   )selfrk   rk   rl   get_node_list&  s
   zVeoExtension.get_node_listN)	r   r   r   r   listtyper   	ComfyNoder   rk   rk   rk   rl   r   %  s    r   r   c                      s   t  S r   )r   rk   rk   rk   rl   comfy_entrypoint/  s   r   ) r   ior   typing_extensionsr   comfy_api.latestr   r   r   r   comfy_api_nodes.apis.veor   r	   r
   r   r   r   r   comfy_api_nodes.utilr   r   r   r   r   r   r   r   r   r   r   r   r   rk   rk   rk   rl   <module>   s(    $		 Y} /
