
    j                        d Z ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
 ddlmZmZ  ed	           G d
 d                      Z ed	           G d d                      Zd#dZd$dZd%dZd&dZd'd!Zd"S )(zASide-effect-free FFmpeg concat planning for local preview frames.    )annotations)	dataclass)Path)ValidationResult)LocalFrameWriteResult)LocalPreviewPlanvalidate_local_preview_planT)frozenc                  $    e Zd ZU ded<   ded<   dS )FFmpegConcatEntrystr
image_pathfloatduration_secondsN)__name__
__module____qualname____annotations__     Z/home/agent/jarvis_runtime/autoshortsbot/AutoShortsBot/autoshorts/rendering/ffmpeg_plan.pyr   r      s'         OOOr   r   c                  |    e Zd ZU ded<   ded<   ded<   ded<   ded<   ded<   d	ed
<   ded<   dZded<   dZded<   dS )FFmpegConcatPlanr   	script_idoutput_pathconcat_file_pathintwidthheightfpstuple[FFmpegConcatEntry, ...]entriesconcat_file_contentr   ztuple[str, ...]side_effectsexternal_callsN)r   r   r   r   r$   r%   r   r   r   r   r      s         NNNJJJKKKHHH****$&L&&&&&(N((((((r   r   valuer   returnboolc                0    | pd                                  S )N )strip)r&   s    r   	_is_blankr,   !   s    ""$$$$r   pathc                .    |                      dd          S )N'z'\'')replace)r-   s    r   _quote_concat_pathr1   %   s    <<W%%%r   r"   r!   c                8   g }| D ]K}|                     dt          |j                   d           |                     d|j        d           L| r1|                     dt          | d         j                   d           d                    |          dz   S )Nzfile 'r/   z	duration z.3f
)appendr1   r   r   join)r"   linesentrys      r   _concat_contentr9   )   s    E ? ?E01ABBEEEFFF=!7===>>>> MK01GHHKKKLLL99Ud""r   preview_planr   write_resultr   r   
str | Pathc                  t          |           }|j        s't          d                    |j                            |j        | j        k    rt          d          t          |j                  t          | j                  k    rt          d          t          d |j        D                       rt          d          |j
        rt          d          t          d t          | j        |j        d	          D                       }t          |          }|                    d
          }t          | j        t!          |          t!          |          | j        | j        | j        |t)          |          dd
  
        }t+          |          }|j        s't          d                    |j                            |S )zDBuild an FFmpeg concat plan without writing files or calling FFmpeg.z; z.write result script_id must match preview planz#frame count must match preview planc              3  B   K   | ]}|                     d            V  dS ).pngN)endswith).0r-   s     r   	<genexpr>z+build_ffmpeg_concat_plan.<locals>.<genexpr>B   s1      
J
Jt}}V$$$
J
J
J
J
J
Jr   frame paths must be png filesz2frame write result must not include external callsc              3  N   K   | ] \  }}t          ||j        d z            V  !dS )i  )r   r   N)r   duration_ms)rA   framer   s      r   rB   z+build_ffmpeg_concat_plan.<locals>.<genexpr>G   sY        
 E:	 	!".5	
 	
 	
     r   T)strict.concat.txtr   )
r   r   r   r   r   r    r"   r#   r$   r%   )r	   is_valid
ValueErrorr6   errorsr   lenframe_pathsframesanyr%   tuplezipr   with_suffixr   r   r   r   r    r9   validate_ffmpeg_concat_plan)	r:   r;   r   preview_validationr"   out_pathconcat_pathplan
validations	            r   build_ffmpeg_concat_planrY   3   s    5\BB& ?#5#<==>>>!777IJJJ
<#$$L,?(@(@@@>???

J
J1I
J
J
JJJ :8999" OMNNN  
 "%\%8,:R[_!`!`!`    G K  H&&}55K(MM[)) "+G44  D -T22J 7:#455666Kr   rW   r   c                   g }t          | j                  r|                    d           t          | j                  r|                    d           n/| j                            d          s|                    d           t          | j                  r|                    d           n/| j                            d          s|                    d           | j        dk    s| j        dk    r|                    d	           | j        dk    r|                    d
           | j	        r|                    d           | j
        r|                    d           | j        s|                    d           t          | j                  r|                    d           t          | j        d          D ]\  }}t          |j                  r|                    d| d           n/|j                            d          s|                    d           |j        dk    r|                    d| d           t!          | t#          |                    S )Nzscript_id is requiredzoutput_path is requiredz.mp4zoutput_path must end with .mp4zconcat_file_path is requiredrH   z*concat_file_path must end with .concat.txtr   z!width and height must be positivezfps must be positivez.ffmpeg concat plans must not have side effectsz0ffmpeg concat plans must not call external toolsz%at least one concat entry is requiredzconcat_file_content is required   )startzentry z image_path is requiredr?   rC   z" duration_seconds must be positive)rI   rK   )r,   r   r5   r   r@   r   r   r   r    r$   r%   r"   r#   	enumerater   r   r   rP   )rW   rK   indexr8   s       r   rS   rS   b   sm   F   /-...!"" 8/0000&&v.. 86777&'' D45555"++M:: DBCCCzQ$+**9:::x1}},--- HFGGG JHIII< ?=>>>)** 97888!$,a888 N NuU%&& 	;MMA5AAABBBB!**622 	;MM9:::!Q&&MML5LLLMMMZfFFFFr   N)r&   r   r'   r(   )r-   r   r'   r   )r"   r!   r'   r   )r:   r   r;   r   r   r<   r'   r   )rW   r   r'   r   )__doc__
__future__r   dataclassesr   pathlibr   autoshorts.ideas.candidater   'autoshorts.rendering.local_frame_writerr   "autoshorts.rendering.local_previewr   r	   r   r   r,   r1   r9   rY   rS   r   r   r   <module>rf      sv   G G " " " " " " ! ! ! ! ! !       7 7 7 7 7 7 I I I I I I \ \ \ \ \ \ \ \ $       
 $
) 
) 
) 
) 
) 
) 
) 
)% % % %& & & &# # # #, , , ,^!G !G !G !G !G !Gr   