
    4Aj;                        d Z ddlm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mZmZmZmZmZmZmZmZmZ ddZ G d de          ZdS )u   Lightweight audio/video file probe using ffprobe.

Returns duration, format, sample rate, channels, and codec info
for any media file ffprobe can read. No heavy dependencies — just
requires ffmpeg/ffprobe on PATH.
    )annotationsN)Path)Any)
BaseToolDeterminismExecutionModeResourceProfileRetryPolicy
ToolResultToolRuntimeToolStability
ToolStatusToolTier	file_path
str | Pathreturnfloat | Nonec                   t          j        d          }|sdS 	 t          j        |dddddt	          |           gddd	
          }t          j        |j                  }t          |d         d                   S # t          $ r Y dS w xY w)zQuick helper: return duration in seconds, or None on failure.

    Use this from other tools that just need the duration without
    going through the full tool execute() flow.
    ffprobeN-vquiet-print_formatjson-show_formatT
   capture_outputtexttimeoutformatduration)
shutilwhich
subprocessrunstrr   loadsstdoutfloat	Exception)r   r   resultdatas       I/app/working/workspaces/default/OpenMontage/tools/analysis/audio_probe.pyprobe_durationr.      s     l9%%G tgI  
 
 
 z&-((T(^J/000   tts   AA: :
BBc                      e Zd ZdZdZej        ZdZdZ	e
j        Zej        Zej        Zej        ZdgZdZg dZg dZd	d
gd
dddidZ eddddd          Z edg           Zd
gZg Z ddZ!d dZ"d!dZ#dS )"
AudioProbeaudio_probez0.1.0analysisr   zbinary:ffprobezInstall ffmpeg (includes ffprobe):
  Windows: winget install ffmpeg
  macOS: brew install ffmpeg
  Linux: sudo apt install ffmpeg)r.   probe_formatprobe_streams)z/getting audio/video duration before compositionz&validating media file format and codecz pre-render checks on asset filesobject
input_pathstringzPath to audio or video file)typedescription)r8   required
properties   @   r   F)	cpu_coresram_mbvram_mbdisk_mbnetwork_required)max_retriesretryable_errorsr   r   c                Z    t          j        d          rt          j        S t          j        S )Nr   )r"   r#   r   	AVAILABLEUNAVAILABLE)selfs    r-   
get_statuszAudioProbe.get_statusg   s&    <	"" 	(''%%    inputsdict[str, Any]r)   c                    dS )Ng         )rH   rK   s     r-   estimate_costzAudioProbe.estimate_costl   s    srJ   r   c                "   t          |d                   }|                                st          dd|           S t          j        d          }|st          dd          S t          j                    }	 t          j        |ddd	d
ddt          |          gddd          }|j	        dk    r+t          dd|j
                                                   S t          j        |j                  }nG# t          j        $ r t          dd          cY S t          j        $ r t          dd          cY S w xY w|                    di           }|                    dg           }t%          d |D             d           }	t          |          t'          t)          |                    dd                    d          |                    d          |                    d          t+          |                    dd                    t+          |                    dd                    t-          |          d}
|	r|	                    d          t+          |	                    dd                    |	                    d           |	                    d!          |	                    d          r#t+          |	                    dd                    nd d"|
d#<   t          d|
t'          t          j                    |z
  d$          %          S )&Nr6   FzFile not found: )successerrorr   zffprobe not found on PATHr   r   r   r   r   z-show_streamsT   r   r   zffprobe failed: zffprobe timed out (15s)zffprobe returned invalid JSONr    streamsc              3  L   K   | ]}|                     d           dk    |V   dS )
codec_typeaudioN)get).0ss     r-   	<genexpr>z%AudioProbe.execute.<locals>.<genexpr>   s8      RR11553F3F'3Q3QQ3Q3Q3Q3QRRrJ   r!      format_nameformat_long_namesizebit_rate)fileduration_secondsr]   r^   
size_bytesr`   stream_count
codec_namesample_ratechannelschannel_layout)codecrf   rg   rh   r`   rW      )rQ   r,   rb   )r   existsr   r"   r#   timer$   r%   r&   
returncodestderrstripr   r'   r(   TimeoutExpiredJSONDecodeErrorrX   nextroundr)   intlen)rH   rK   r6   r   startr+   r,   fmtrT   audio_stream
probe_datas              r-   executezAudioProbe.executeo   s=   &.//
  "" 	Te3Rj3R3RSSSS,y)) 	Pe3NOOOO		T^'#V"#
OO  $  F  A%%!!DV]-@-@-B-BDD   
 :fm,,DD( 	N 	N 	Ne3LMMMMMM# 	T 	T 	Te3RSSSSSS	T hhx$$((9b)) RRRRRTXYY 
OO %eCGGJ,B,B&C&CQ G G77=11 #(: ; ;cggfa0011CGGJ2233LL&
 &

  	%)),77"<#3#3M1#E#EFF(,,Z88"."2"23C"D"DDPDTDTU_D`D`jC 0 0Q ? ?@@@fj# #Jw "49;;#6::
 
 
 	
s   9A"C6 C6 6 D:D:9D:N)r   r   )rK   rL   r   r)   )rK   rL   r   r   )$__name__
__module____qualname__nameversionr   COREtier
capabilityproviderr   
PRODUCTION	stabilityr   SYNCexecution_moder   DETERMINISTICdeterminismr   LOCALruntimedependenciesinstall_instructionscapabilitiesbest_forinput_schemar	   resource_profiler
   retry_policyidempotency_key_fieldsside_effectsrI   rO   rz   rN   rJ   r-   r0   r0   ;   s7       DG=DJH(I"'N+KG$%L	+  GFFL  H !N < 
	 	L 'B1u   ;1rBBBL*^L& & & &
   C
 C
 C
 C
 C
 C
rJ   r0   )r   r   r   r   )__doc__
__future__r   r   r"   r$   rl   pathlibr   typingr   tools.base_toolr   r   r   r	   r
   r   r   r   r   r   r.   r0   rN   rJ   r-   <module>r      s8    # " " " " "                                             8w
 w
 w
 w
 w
 w
 w
 w
 w
 w
rJ   