
    4Aj0                    v    d Z ddlm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  G d de	          ZdS )	a  Visual QA tool for automated video quality checks.

Extracts frames at specified timestamps and runs basic quality checks:
- File existence, resolution, duration, codec validation
- Frame extraction for visual inspection by the agent
- Caption occlusion check (compares brightness in face vs caption zones)
- Transition verification (frame similarity at transition points)

Returns frame paths so the agent can visually inspect them.
    )annotationsN)Path)Any)BaseToolDeterminismExecutionModeResourceProfile
ToolResultToolStabilityToolTierc                  L   e Zd ZdZdZej        ZdZdZ	e
j        Zej        Zej        ZddgZdZdgZg dZd	d
dgdg dddddddddidddddddg ddddd	dddiddiddiddiddiddiddd d!Z ed"d#d$d%&          Zg d'Zd(gZd)gZd7d.Zd7d/Zd7d0Zd7d1Z d8d5Z!d6S )9VisualQA	visual_qaz0.1.0analysisffmpegz
cmd:ffmpegzcmd:ffprobez0Install FFmpeg: https://ffmpeg.org/download.html)extract_review_framesprobe_videocheck_audio_levelsobject	operation
input_pathstring)reviewprobeaudio_levelszreview: extract frames at timestamps for visual inspection. probe: get video metadata (duration, resolution, codecs). audio_levels: check audio volume at specified timestamps.)typeenumdescriptionz"Path to the video file to inspect.)r   r   arrayr   numberzITimestamps (in seconds) at which to extract frames or check audio levels.)r   itemsr   zfDirectory to save extracted frames. Defaults to a 'review_frames' subdirectory next to the input file.)
resolutiondurationaudio_presentpixel_format	file_size)r   r   z)Specific checks to run (probe operation).ziExpected values for validation. Keys: width, height, min_duration, max_duration, pixel_format, has_audio.integerboolean)widthheightmin_durationmax_durationr%   	has_audio)r   r   
properties)r   r   
timestamps
output_dirchecksexpected)r   requiredr.      i   r      )	cpu_coresram_mbvram_mbdisk_mb)r   r   r/   z!writes frame images to output_dirz4Visually inspect extracted frames for quality issuesinputsdict[str, Any]returnr
   c                F   |d         }|d         }t          |                                          st          dd|           S t          j                    }	 |dk    r|                     |          }nL|dk    r|                     |          }n0|dk    r|                     |          }nt          dd	|           S n5# t          $ r(}t          dt          |                    cY d }~S d }~ww xY wt          t          j                    |z
  d
          |_
        |S )Nr   r   FzInput not found: )successerrorr   r   r   zUnknown operation:    )r   existsr
   time_review_probe_audio_levels	Exceptionstrroundduration_seconds)selfr:   r   r   startresultes          G/app/working/workspaces/default/OpenMontage/tools/analysis/visual_qa.pyexecutezVisualQA.executey   sC   ;'	L)
J&&(( 	Ue3Sz3S3STTTT	
	;H$$f--g%%V,,n,,++F33!%7XY7X7XYYYY 	; 	; 	;e3q66:::::::::	; #(	e(;Q"?"?s   A'C 
C5C0*C50C5c                   |d         }|                     dg           }|s6|                     |          }d|dz  |dz  |dz  t          |dz
  d          g}|                     d          }|s$t          t	          |          j        d	z            }t	          |                              d
d
           g }|D ]}|d                    dd          }t          t	          |          d| dz            }	dddt          |          d|dddd|	g}
	 |                     |
           t	          |	          	                                r|
                    ||	d           # t          $ r  |
                    |dd| dd           Y w xY wt          d
d|t          d |D                       |d d! |D             "          S )#z9Extract frames at specified timestamps for visual review.r   r/         ?g      ?      ?g      ?r   r0   review_framesT)parentsexist_ok.1f._frame_zs.jpgr   -y-ss-iz	-frames:v1z-q:v2)	timestamppathNzFailed to extract frame at s)r_   r`   r?   r   c                <    g | ]}|                     d           |S r`   get.0fs     rN   
<listcomp>z$VisualQA._review.<locals>.<listcomp>   s'    #G#G#G!v#GA#G#G#G    )r   inputframe_countframesc                H    g | ]}|                     d           |d           S rc   rd   rf   s     rN   ri   z$VisualQA._review.<locals>.<listcomp>   s+    BBBQAEE&MMBqyBBBrj   )r>   data	artifacts)re   _get_durationmaxrG   r   parentmkdirreplacerun_commandrA   appendrF   r
   len)rJ   r:   r   r/   durr0   rm   tsts_label
frame_pathcmds              rN   rC   zVisualQA._review   sT   L)
ZZb11
 		$$Z00Cd
d
d
C#Iq!!J ZZ--
 	HT*--4FGGJZtd;;; 	 	B{{**344HT*--0H0H0H0HHIIJ$s2wwjSC  %%%
##**,, MM%' *# #       !# @2@@@       %#"#G#Gv#G#G#GHH 	  CB&BBB	
 	
 	
 		
s   AE!!'F
Fc           	        |d         }|                     di           }ddddddd	|g}d
dl}|                     |          }|j        }|                    |          }d}	d}
|                     dg           D ]=}|                     d          dk    r|	s|}	 |                     d          dk    r|
s|}
>t          |                     di                                dd
                    t          t          |                     di                                dd
                    dz  d          |
dud}|	rz|                    |	                     d          |	                     d          |	                     d          |	                     d          |	                     d          d           |
rR|                    |
                     d          |
                     d          |
                     d          d           g }d|v rS|                     d          |d         k    r4|	                    d|d          d |                     d                      d|v rS|                     d          |d         k    r4|	                    d!|d          d |                     d                      d"|v r;|d         |d"         k     r)|	                    d#|d         d$d%|d"          d&           d'|v r;|d         |d'         k    r)|	                    d(|d         d$d)|d'          d&           d*|v rS|                     d*          |d*         k    r4|	                    d+|d*          d |                     d*                      d,|v rA|d,         |d,         k    r/|	                    d-|d,         rd.nd/ d |d,         rd.nd/            ||d0<   t          |          d
k    |d1<   t          d2d3|d4|5          S )6zBProbe video metadata and optionally validate against expectations.r   r2   ffprobe-vr?   -show_entrieszhformat=duration,size:stream=width,height,codec_name,pix_fmt,r_frame_rate,sample_rate,channels,codec_type-ofjsonr   Nstreams
codec_typevideoaudioformatr#   sizei   r4   )r#   file_size_mbr-   r)   r*   pix_fmt
codec_namer_frame_rate)r)   r*   r%   video_codec
frame_ratesample_ratechannels)audio_codecr   r   zWidth: expected z, got zHeight: expected r+   zDuration too short: rV   zs < ra   r,   zDuration too long: zs > r%   zPixel format: expected r-   zAudio: expected presentabsentvalidation_issuesvalidation_passedTr   )r   rk   r>   ro   )re   r   rv   stdoutloadsfloatrH   intupdaterw   rx   r
   )rJ   r:   r   r2   r}   r   probe_result	probe_out
probe_datavideo_streamaudio_streamra   infoissuess                 rN   rD   zVisualQA._probe   s   L)
::j"-- tW;6
 	'',, '	ZZ	**
 	2.. 	! 	!Auu\""g--l- |$$///  jnnXr::>>z1MMNN!JNN8R0044VQ??@@7JA  &T1
 
  	KK%))'22&**844 , 0 0 ; ;+//==*..~>>      	KK+//==+//>>(,,Z88     h488G#4#48I#I#IMMYXg->YYdhhwFWFWYYZZZxDHHX$6$6(8:L$L$LMM\hx.@\\QYHZHZ\\]]]X%%$z*:Xn=U*U*UMM\tJ'7\\\.AY\\\   X%%$z*:Xn=U*U*UMM[d:&6[[[@X[[[   X%%$((>*B*Bh~F^*^*^MMd(>*Bdd$((SaJbJbdd   (""tK'8H[<Q'Q'QMMF0E#S998 F F$($5Cyy8F F  
 %+ !$'KK1$4 !$#  
 
 
 	
rj   c                   |d         }|                     dg           }|s.|                     |          }d|dz  t          |dz
  d          g}g }|D ]v}ddd	t          |          d
dd|dddddt	          d          j        dk    rdndg}	 |                     |          }|j        }	d}
d}|	                    d          D ]}d|v rSt          |                    d          d         
                                                                d                   }
Yd|v rRt          |                    d          d         
                                                                d                   }|                    ||
|d           ;# t          $ r0}|                    |t          |          d           Y d}~pd}~ww xY wt          dd ||d!"          S )#z+Check audio levels at specified timestamps.r   r/   rQ   rR   g       @r   r   rZ   r[   z-t3r\   z-vnz-afvolumedetectz-fnullsyswin32NULz	/dev/nullN
mean_volumezmean_volume:r4   
max_volumezmax_volume:)r_   mean_volume_dbmax_volume_db)r_   r?   Tr   )r   rk   levelsr   )re   rq   rr   rG   
__import__platformrv   stderrsplitr   striprw   rF   r
   )rJ   r:   r   r/   ry   r   rz   r}   
cmd_resultoutputmean_volmax_vollinerM   s                 rN   rE   zVisualQA._audio_levels!  sP   L)
ZZb11
 	=$$Z00CsSy#cCi*;*;<J 	 	B$s2wwcjunfz%'8'8'AW'L'LeeR]C!--c22
#*"LL.. Y YD$,,#(N)C)CA)F)L)L)N)N)T)T)V)VWX)Y#Z#Z%--"'

=(A(A!(D(J(J(L(L(R(R(T(TUV(W"X"X!#&.%,     
    !# VV          +#  
 
 
 	
s   C>F
G%GGr`   rG   r   c                    ddddddd|g}|                      |          }t          |j                                                            d          d	                   S )
Nr   r   r?   r   zformat=durationr   zcsv=p=0r   r   )rv   r   r   r   r   )rJ   r`   r}   
dur_results       rN   rq   zVisualQA._get_durationR  sc    tW.9	
 %%c**
Z&,,..44T::1=>>>rj   N)r:   r;   r<   r
   )r`   rG   r<   r   )"__name__
__module____qualname__nameversionr   COREtier
capabilityproviderr   EXPERIMENTAL	stabilityr   SYNCexecution_moder   DETERMINISTICdeterminismdependenciesinstall_instructionsagent_skillscapabilitiesinput_schemar	   resource_profileidempotency_key_fieldsside_effectsuser_visible_verificationrO   rC   rD   rE   rq    rj   rN   r   r      s       DG=DJH*I"'N+K -0LM:L  L  ,/ !;;;P	  !C 
   (+*	  !K   $  	 	  K  !/
 %i0%y1%+X$6%+X$6%+X$6"()!4  W;
 ;
? ?LB '3SVWWWFFF78L>!   08
 8
 8
 8
tT
 T
 T
 T
l/
 /
 /
 /
b? ? ? ? ? ?rj   r   )__doc__
__future__r   rB   pathlibr   typingr   tools.base_toolr   r   r   r	   r
   r   r   r   r   rj   rN   <module>r      s   	 	 # " " " " "                              }? }? }? }? }?x }? }? }? }? }?rj   