
    -^j*                        d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	Z	d dl
m
Z
mZ d dlmZ  ee                                          j        d         Zedz  Zedz  Zedz  Zedz  Zed	z  Zed
z  Zedz  Zedz  Zedz  Zedz  Zedz  ZdZdZdZd Z d Z!d Z"ddZ#d Z$d Z%d Z&d Z'e(dk    rS	  ej)         e'                       dS # e*$ r0Z+ e,de+ ej-                    ej)        d           Y dZ+[+dS dZ+[+ww xY wdS )     N)datetimetimezone)Path   zstory/promotion-report.jsonzimage/layout-manifest.jsonz image/intro-poster-1920x1080.pngzaudio/intro-voice-source.txtz#audio/intro-voice-pronunciation.txtz log/intro-tts-pronunciation.jsonzaudio/intro-voice.wavzaudio/intro-full.wavzoutput/intro/intro.mp4zlog/intro-render.jsonzlog/intro-voice.jsonzhttp://192.168.40.33:7862zhttp://192.168.1.104:8021zngoc-huyen-vbeec                    t          j                    }|                     d          5 t          fdd          D ]}|                    |           	 d d d            n# 1 swxY w Y   |                                S )Nrbc                  .                          d          S )Ni   )read)handles   script/render-intro-gpu.py<lambda>zsha.<locals>.<lambda>"   s    &++o">">     r   )hashlibsha256openiterupdate	hexdigest)pathdigestblockr   s      @r   shar      s    ^F	4 !F>>>>DD 	! 	!EMM%    	!! ! ! ! ! ! ! ! ! ! ! ! ! ! ! s   ,A##A'*A'c                     t          j        dddddddddt          |           g
d	d	d	
          }t          j        |j                  S )Nffprobez-verrorz-show_entrieszformat=duration,sizezMstream=index,codec_name,width,height,sample_rate,channels:stream_tags=encoderz-ofjsonT)capture_outputtextcheck)
subprocessrunstrr   loadsstdout)r   results     r   prober&   '   sY    ^4/3Ihvs4yy T	3 3 3F
 :fm$$$r   c           	         t          j        t          |           d          5 }|                                |                                |                                |                                |                                |                                |                                z  dcd d d            S # 1 swxY w Y   d S )Nr   )channelssample_widthsample_rateframescompressionduration_seconds)waver   r"   getnchannelsgetsampwidthgetframerate
getnframesgetcomptype)r   r   s     r   wav_infor4   0   s    	3t99d	#	# 
v++--"//11!..00''))!--// & 1 1 3 3f6I6I6K6K K
 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
s   B
B::B>B>   c                 P   |'t          j        |                              d          nd }t          j                            | ||rddini |rdnd          }t          j                            ||          5 }t          j        |          cd d d            S # 1 swxY w Y   d S )Nutf-8Content-Typezapplication/jsonPOSTGETdataheadersmethodtimeout)r   dumpsencodeurllibrequestRequesturlopenload)urlpayloadr@   bodyrD   responses         r   request_jsonrL   <   s	   292E4:g%%g...4Dn$$Stei=qnN`=a=aoq  EI  {T  {A  {A  OT$  U  UG					9	9 #Xy""# # # # # # # # # # # # # # # # # #s   :BB"Bc           
      n   t          j        t                              d                    }|                    d          dus|                    d          dk    rt          d          |                    d          | k    |                    d	          t          t                    k    |                    d
          t          t                    k    |                    d          du |                    d          dk    |                    d          du |                    dd          dk    |                    d           f}t          d |                    dg           D             d           }t          |          r|r|                    dd          dk     rt          d          |S )Nr7   encodingverifiedTkindintroz#intro pronunciation receipt invalidsource_canon_sha256source_sha256projection_sha256reverse_verifiednon_lexicon_changesr   semantic_content_changedFreplacement_countr   unresolved_requiredc              3   L   K   | ]}|                     d           dk    |V   dS )sourceaudioNget.0rows     r   	<genexpr>z)validate_pronunciation.<locals>.<genexpr>Q   s:      ffcQYIZIZ^eIeIecIeIeIeIeffr   lexiconu2   intro audio -> au đi ô pronunciation gate failed)r   r#   PRONUNCIATION	read_textr_   RuntimeErrorr   SOURCE
PROJECTIONnextall)canonpronunciationchecks	audio_rows       r   validate_pronunciationrp   C   s   J}666HHIIM$$D00M4E4Ef4M4MQX4X4X@AAA/00E9/**c&kk9-..#j//A,--5/00A5455>-q11Q63444	F ff]%6%6y"%E%EfffhlmmIv;; Qi Q9==9La+P+PST+T+TOPPPr   c                 
   t          t                    }t          t                    }t                                          rt
                                          rt          j        t                              d                    }|	                    d          du r|	                    d          | k    r|	                    d          t          t                    k    r}|	                    d          |k    rd|	                    d          |k    rK|	                    d	          t          k    r-|	                    d
          t          t
                    k    r|S t          t          dz             }t          t          dz             	                    dg           }t          d |D             d           }|	                    d          dk    s0|r.|	                    d          dus|	                    d          durt          d          t                              d          }t           j                            |t          d                              d          }	t           j                            t          dz   |	ddid          }
t           j                            |
d          5 }|                                }d d d            n# 1 swxY w Y   t1          |          dk     s|d d         dk    s|dd          d!k    rt          d"          t
          j                            dd#           t
                              d$          }|                    |           t;          |          }|d%         d&k    s|d'         d(k    r%|                    d)           t          d*          t?          j         |t
                     i d+d,ddd| d-tC          t          "                    tF                              dt          t                    d.tC          t          "                    tF                              d|d/tC          t          "                    tF                              d|d0dd	t          d1t          d2tC          t
          "                    tF                              d
t          t
                    d3tI          t
                    d4|d5tK          j&        tN          j(                  )                                }t          *                    t          j+        |d6d78          d9z   d           |S ):Nr7   rN   rP   TrS   rT   rU   pronunciation_receipt_sha256voiceoutput_sha256/healthz/voicesvoicesc              3   V   K   | ]$}|                     d           t          k     |V  %dS )idN)r_   
VOICE_NAME)ra   items     r   rc   z#synthesize_intro.<locals>.<genexpr>c   s6      HH488D>>Z+G+G+G+G+G+GHHr   statusokmodel_existsconfig_existszintro TTS preflight failed)r   rs   z/ttsr8   z!application/x-www-form-urlencodedr9   r;   i,  r?   ,      s   RIFF      s   WAVEzintro TTS response is not WAVparentsexist_ok	.part.wavr-   r   r,   NONE)
missing_okz+intro TTS WAV technical verification failedversionr   source_pathprojection_pathpronunciation_receiptpronunciation_verifiedendpointoutputr&   wav
checked_atF   ensure_asciiindent
),r   ri   re   VOICE_RECEIPTexistsVOICEr   r#   rf   r_   rh   ry   rL   TTS_BASErj   rg   rC   parse	urlencoderB   rD   rE   rF   r
   lenparentmkdirwith_suffixwrite_bytesr4   unlinkosreplacer"   relative_toPROJECTr&   r   nowr   utc	isoformat
write_textrA   )rl   rm   projection_shareceipt_shaoldhealthrv   rb   r   rJ   reqrK   r<   tempinforeceipts                   r   synthesize_intror   W   s   __Nm$$K %,,.. j00'0BBCCGGJ4''CGG4I,J,Je,S,SGGO,,F;;H[@\@\`n@n@nGG:;;{JJswwW^O_O_cmOmOmGGO,,E

::J(Y.//F(Y.//33HbAAF
HHHHH$
O
OCzz(t##3##''.2I2IQU2U2UY\Y`Y`apYqYqy}Y}Y}788811D<!!4*"E"EFFMMgVVD
.
 
 F!2X{G|  FL
   M  MC			S		1	1 X}}              
4yy2~~bqbW,,QrT
g0E0E:;;;	Ltd333[))DTD>>D1$$](;v(E(Et$$$HIIIJtU	1	 $	(=u	s6--g6677	9H#f++	 	3z55g>>??	 BUVd	 	 ]%>%>w%G%G!H!H		
 	'	
 6Nt	 		 (	 	#e''0011	 4CCJJ	 	u	  %d	 -9(,x|:T:T:^:^:`:`	G TZeANNNQUU`ghhhNs   9KK!Kc                 n   |                      d          t          t                    k    rt          d          t                              d          }t          j        t          t                    d          5 }|	                                }|
                                dk    rt          d          |                    |                                          }d d d            n# 1 swxY w Y   t          |j        dz            }d|z  |j        z  |j        z  }t          j        t          |          d	          5 }|                    |           |                    |           |                    |           d d d            n# 1 swxY w Y   t'          j        |t                     d S )
Nrt   z#intro voice receipt/output mismatchr   r   r   zintro voice WAV is compressedg       @    wb)r_   r   r   rg   FULLr   r.   r   r"   	getparamsr3   
readframesr2   int	framerate	nchannels	sampwidth	setparamswriteframesr   r   )voice_receiptr   r\   paramsr+   silence_framessilencetargets           r   append_silencer      s   ))SZZ77@AAAK((D	3u::t	$	$ 8!!##6))>???""6#4#4#6#677	8 8 8 8 8 8 8 8 8 8 8 8 8 8 8
 )C/00N&)99F<LLG	3t99d	#	# $v   6"""7###$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ JtTs&   <A#C++C/2C/A FFFc            
         t           t          t          t          t          t
          fD ](} |                                 st          d|            )t          j	        t           
                    d                    }t          j	        t          
                    d                    }|                    d          }|                    d          dus[|                    d          dusD|                    di                               d          dus|                    d          |k    rt          d	          t          |          }t          ||          }t          |           t          t!          t"                    d
         d                   }t%          t&          dz             }|                    d          dk    s0|                    d          dk    s|                    d          durt          d          t(                                          r_t,                                          rEt          j	        t(          
                    d                    }|                    d          du r |                    d          |k    r|                    d          t/          t                    k    r|                    d          t/          t"                    k    r|                    d          t/          t                    k    rf|                    d          t/          t,                    k    r;t1          t          j        ddt5          t,                    dd                     dS t,          j                            dd           dt;          j                    j        d d         z   }	i d|	dt5          t                    |d gd!t5          t"                    d"d d#t5          t,                    d$d%d&d'd(d)d*d+d,d-d.dd/dd0d1d2d3d4d5d6d5d7d5ddd8}
t%          t&          d9z   |
d:;          }t,                                          st          d<          t!          t,                    }|                    d=g           }t          |d
         d                   }tA          |          d>k    oGtC          d? |D                       o.tC          d@ |D                       otE          ||z
            dAk    }i dBdCd|d|rdDndEd|d|	dFt&          dGddH|dI|dt/          t                    dt/          t"                    dJt/          t                    dt/          t                    dKt/          t
                    dLt/          tF                    dMt5          t,          $                    tJ                              dt/          t,                    ||ddNddOt/          t                    dPtM          j'        tP          j)                  *                                dQ}t(          j                            dd           t(          +                    t          j        |dd>R          dSz   d           t1          t          j        ||	|dTd                     |rdndCS )UNzmissing r7   rN   spoken_sha256rP   T	visual_qarS   zintro authority gate mismatchformatdurationru   r{   r|   encoder
h264_nvencnvenc_availablez-intro GPU endpoint does not verify h264_nvencposter_sha256audio_sha256intro_pronunciation_sha256rt   )rP   reusedr   F)r   r   r   zhuyenan-012-intro-r   job_idscenes)
image_pathr   
audio_pathsubtitle_pathoutput_pathwidthi  heighti8  fpsr5   crf   presetp4burn_subtitles	ken_burnsmotion_presetnone
transitioncuttransition_durationg        fade_infade_out)slice_audioslice_subtitlesz/stickman-render-gpui  r?   z*intro GPU endpoint returned without outputstreamsr   c              3   H   K   | ]}|                     d           dk    V  dS )
codec_nameh264Nr^   r`   s     r   rc   zmain.<locals>.<genexpr>   s3      (\(\S)>)>&)H(\(\(\(\(\(\r   c              3   H   K   | ]}|                     d           dk    V  dS )r   aacNr^   r`   s     r   rc   zmain.<locals>.<genexpr>   se        eX  eX  ILehelelmyezez  D  fD  eX  eX  eX  eX  eX  eXr   g?r   r   	completedfailedr   encoder_requiredserver_healthserver_resultintro_source_sha256"intro_pronunciation_receipt_sha256intro_voice_receipt_sha256r   passed_via_static_posterzEimage/layout-manifest.json visual_qa; no intro video frame inspection)requiredr{   rP   evidencer   )r-   r&   r   r   r   r   )rP   r   r   ),	PROMOTIONLAYOUTPOSTERrh   ri   re   is_filerg   r   r#   rf   r_   rp   r   r   floatr&   r   rL   GPU_BASERECEIPTr   OUTPUTr   printrA   r"   r   r   uuiduuid4hexr   anyabsr   r   r   r   r   r   r   r   r   )r   	promotionlayoutrl   rm   r   full_durationr   r   r   rI   r%   output_prober   r   rP   r   s                    r   mainr     s   FFFJN 2 2||~~ 	20$00111	2
9...@@AAIZ(('(::;;FMM/**Ej!!--J1G1Gt1S1Szz+r**..z::$FFzz/00E99:;;;*511M$UM::M=!!!%++h/
;<<M(Y.//Fzz(t##vzz)'<'<'L'LPVPZPZ[lPmPmuyPyPyJKKK~~ FMMOO j**G*<<==GGJ4''CGG4I,J,Je,S,SGGO,,F;;@W@W[^_c[d[d@d@dGG899S__LLGGO,,F;;$*$$#f++VVejkkklll1
Mt444!DJLL$4SbS$99F ~x  ~3v;;Ta,b,b+c  ~eqsvw{s|s|  ~  N  PT  ~  Vc  eh  io  ep  ep  ~  ry  {  ~  AI  KO  ~  QV  XZ  ~  \a  ce  ~  go  qu  ~  wG  IN  ~  P[  ]b  ~  ds  u{  ~  }I  KP  ~  Rg  il  ~  nw  y|  ~  ~H  JM  ~  ^c  x}  ~  ~  ~G(%;;WcRRRF>> IGHHH==Ly"--G\(+J788H7||q   AS(\(\T[(\(\(\%\%\  Aad  eX  eX  PW  eX  eX  eX  bX  bX  A  ]`  ai  ly  ay  ]z  ]z  ~A  ]AH1 (,4X6[kkS[u&.8BH 	L +:6 DSTZ 	V	 '5c$ii	
 	s6{{
 -I#j// 	-c-.@.@ 	%c-&8&8 	#f((1122 5DS[[ %|0_ [[
 
 l8<00::<<#  G& N555tz'aHHH4OZabbb	$*(f(SSbg
h
h
hiii11ar   __main__zIntro blocked: )file)Nr5   ).r   r   r   r    sysurllib.parserC   urllib.requestr  r.   r   r   pathlibr   __file__resolver   r   r   r   r   rh   ri   re   r   r   r  r  r   r   r   ry   r   r&   r4   rL   rp   r   r   r  __name__exit	Exceptionexcr  stderr r   r   <module>r     sC     				     



           ' ' ' ' ' ' ' '      
$x..
 
 
"
"
*1
-33		/	/	5	5	1	1<<
<<))''	+	+
+
+00&&
  % % %	
 	
 	
# # # #  () ) )X  $:  :  : z z   %%%CJ7777 s   8C D%DD