
    kSj                     0   d 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  e	ej
                  dk    r eej
        d                   n
 ed          ZdZd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dZd Zd Zd Zd Zedk    r e             dS dS )u   Build Lời Cổ Nhân subtitles from real ASR word timestamps.

Set PROJECT to the current project path before running, or pass the project path
as argv[1]. This script intentionally refuses to copy clamped/computed subtitles.
    N)Path   zp/data/video-pipeline/Channel-Loi-Co-Nhan/project/208-Biet-giu-mot-ngon-den-nho-trong-long-dem-dai-cung-co-loi-diz-http://192.168.1.103:8001/transcribe-and-savez http://192.168.1.103:8001/healthzaudio/master_narration.wavzsubtitles/asr_word_timing.jsonz5subtitles/master_subtitles_asr_timing_script_text.srtz-subtitles/subtitle_asr_script_text_debug.jsonz'logs/asr_subtitle_rebuild_manifest.json   c                 R    t          t          j        d          | z   d           d S )Nz%F %T T)flush)printtimestrftime)msgs    /data/video-pipeline/Channel-Loi-Co-Nhan/project/208-Biet-giu-mot-ngon-den-nho-trong-long-dem-dai-cung-co-loi-di/scripts_run_asr_and_build_subtitles.pylogr      s*    	$-
!
!C
't444444       c                    |d n(t          j        |d                              d          }t          j                            | ||ddini |dnd          }t          j                            ||	          5 }t          j        |                                	                    d                    cd d d            S # 1 swxY w Y   d S )
NF)ensure_asciiutf-8zContent-Typezapplication/jsonPOSTGET)dataheadersmethodtimeout)
jsondumpsencodeurllibrequestRequesturlopenloadsreaddecode)urlpayloadr   r   reqresps         r   	post_jsonr(      s#   ?44
7(O(O(O(V(VW^(_(_D
.
 
 4ahat.J\9]9]z|  PW  Pc  FL  FL  in
   o  oC			W		5	5 7z$))++,,W55667 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7s   <9CC	Cc                 l   g }|                      dg           pg D ]\}|                     dg           pg D ]A}|                     d          *|                     d          |                    |           B]|s:t          |                      d          t                    rd | d         D             }|S )Nsegmentswordsstartendc                 f    g | ].}|                     d           |                     d          ,|/S )r,   Nr-   )get).0ws     r   
<listcomp>z!collect_words.<locals>.<listcomp>+   s7    dddqAEE'NN,F155QV<<KcKcKcKcr   )r/   append
isinstancelist)docr+   segr1   s       r   collect_wordsr8   $   s    Ewwz2&&,"    "%%+ 	  	 AuuW~~)aeeEll.FQ	   eZ 0 0$77 eddCLdddLr   c                     t          t          t          d|           dz                      }|dz  }|dz  }|dz  }|dz  }|dz  }|dz  }|dd|dd|dd|dS )	Ng        i  i6 i`  02:,03)introundmax)secmshmss        r   srt_timerF   /   s    	U3sC==4'((	)	)B
gArW}r
eAR5[R
d
AB$JB***Q***A***2****r   c                  "   t          j        t          dz                      d                    } g }| d         D ]Rt	          j        dd                             dd                    }|                    fd	|D                        S|S )
Nzscript/scenes.jsonr   encodingscenesz\S+	narration
 c              3   ,   K   | ]}d          |fV  dS )scene_idN )r0   r1   scs     r   	<genexpr>z(script_words_by_scene.<locals>.<genexpr><   s,      88Qbna(888888r   )r   r!   PROJECT	read_textrefindallreplaceextend)r   itemsr+   rQ   s      @r   script_words_by_scenerZ   7   s    :w!55@@'@RRSSDE8n 9 9
62k?#:#:4#E#EFF8888%8888888Lr   c                  	   t                                           st          dt                      t          j                            dd           t          j                            dd           t          t          d d          } t          t                     t          t                    dddd}t          d	t                      t          t          |d
          }t                                          s1t                              t          j        |dd          d           t          j        t                              d                    }t#          |          }t%                      }|st          d          t'          |          t'          |          dz  k     r/t          dt'          |           dt'          |                     g }d}t)          t'          |          t'          |                    }||k     rt)          t*          ||z
            }	||z
  t*          dz   k    rd}	||||	z            }
||||	z            }t-          |
d         d                   }t/          t-          |
d         d                   |dz             }|                    t'          |          dz   |d         d         ||d                    d |D                       d           ||	z  }||k     g }|D ]O}|t          |d                   t5          |d                    dt5          |d                    |d          d!gz  }Pt6                              d"                    |          d           t8                              t          j        t'          |          t'          |          t'          |          t/          dt'          |                    z  |d#dd          d           t                              t          j        d$t          | t          t                     t          t                    t          t6                    t'          |          t'          |          t'          |          d%d&
dd          d           t          d't'          |           d(t'          |           d)t'          |                      d S )*Nzmissing master audio: T)parentsexist_ok   r   vi
transcribe)
input_pathoutput_pathlanguagetaskword_timestampsz	POST ASR r   F   )r   indentr   rH   zASR returned no word timestampsg      ?zASR word count too low: asr=z script=r   r      r,   r-   gffffff?rM   c              3       K   | ]	\  }}|V  
d S )NrP   )r0   _r1   s      r   rR   zmain.<locals>.<genexpr>]   sR        xT  xT  C~  BCxy  xT  xT  xT  xT  xT  xTr   )idxrO   r,   r-   textrl   z --> rm    rL   )	asr_wordsscript_wordsratiocues	completedz_Subtitle timing rebuilt from real ASR word timestamps; display text comes from original script.)
statusendpointhealthinput_audio_path
asr_output
srt_outputro   rp   rr   notezDONE ASR subtitle cues=z asr_words=z script_words=)MASTER_AUDIOexists
SystemExit
ASR_OUTPUTparentmkdirMANIFESTr(   HEALTH_ENDPOINTstrr   ENDPOINT
write_textr   r   r!   rT   r8   rZ   lenminWORDS_PER_CUEfloatr@   r3   joinrF   
SRT_OUTPUTDEBUG_OUTPUT)rv   r%   responseasr_docro   rp   rr   rl   ntake	group_asrgroup_scriptr,   r-   linescs                   r   mainr   @   s      B@,@@AAAD4888O$666b999F ..s:\`jv  LP  Q  QGH7D999H ddjaPPP[bcccj--w-??@@Gg&&I(**L <:;;;
9~~L))D000cIccPST`PaPaccdddD
CC	NNC--..A
''=!c'**s7ma'''Dc#*n-	#Cd
N3il7+,,%	"e,--ut|<<CIIM|Aq7ITYbeorowow  xT  xT  GS  xT  xT  xT  pT  pT  U  U  	V  	V  	Vt '' E d d#ah--HQwZ$8$8!S!Sx%?Q?Q!S!SUVW]U^`bcc$))E**W===DJS^^UXYeUfUfqtu~qq  CF  GH  JM  NZ  J[  J[  C\  C\  r\  fj  (k  (k  z  HI  J  J  J  U\  ]  ]  ]
kx[awz  |H  xI  xI  Y\  ]g  Yh  Yh  x{  |F  xG  xG  VY  Zc  Vd  Vd  vy  zF  vG  vG  QT  UY  QZ  QZ  dE  $F  $F  UZ  cd  e  e  e  pw  x  x  xi#d))iiIiiVYZfVgVgiijjjjjr   __main__)Nr   )__doc__r   rU   sysr	   urllib.requestr   pathlibr   r   argvrS   r   r   r{   r~   r   r   r   r   r   r(   r8   rF   rZ   r   __name__rP   r   r   <module>r      sb   
  				 



           "s38}}q00$$sx{


dd  <n  7o  7o:45577
NN
HH>>5 5 57 7 7 7  + + +  %k %k %kP zDFFFFF r   