
    i_                     P    d Z ddlZddlZddlmZ ddlmZmZ  G d de      Z	d Z
y)z, Experimental module for subtitles support.     Ncvsecs)TextClip	VideoClipc                   >    e Zd ZdZd
dZddZd Zd Zd Zd Z	d	 Z
y)SubtitlesClipa   A Clip that serves as "subtitle track" in videos.
    
    One particularity of this class is that the images of the
    subtitle texts are not generated beforehand, but only if
    needed.

    Parameters
    ==========

    subtitles
      Either the name of a file, or a list

    Examples
    =========
    
    >>> from moviepy.video.tools.subtitles import SubtitlesClip
    >>> from moviepy.video.io.VideoFileClip import VideoFileClip
    >>> generator = lambda txt: TextClip(txt, font='Georgia-Regular', fontsize=24, color='white')
    >>> sub = SubtitlesClip("subtitles.srt", generator)
    >>> myvideo = VideoFileClip("myvideo.avi")
    >>> final = CompositeVideoClip([clip, subtitles])
    >>> final.write_videofile("final.mp4", fps=myvideo.fps)
    
    Nc           
         	 t        j                   d       t        |t              rt	        |      }| _        t                _        |d }| _        d _	        t         j
                  D cg c]
  \  \  }}}| c}}}       _         j                   _         fd		 fd}	 fd}| _        t         j                  d      j                        }|rt        |d	
       _        y d  _        y c c}}}w )NF)has_constant_sizec                 $    t        | ddddd      S )NzGeorgia-Bold   whiteblackg      ?)fontfontsizecolorstroke_colorstroke_width)r   )txts    [/var/www/html/eduruby.in/venv/lib/python3.12/site-packages/moviepy/video/tools/subtitles.py<lambda>z(SubtitlesClip.__init__.<locals>.<lambda>1   s    >1375<3)P     r   c                    j                   j                         D cg c]  \  \  }}}|| cxk  r|k  r	n n||f|f }}}}|s8j                  D cg c]  \  \  }}}|| cxk  r|k  r	n n||f|f }}}}|sy|d   }|j                   j                         vr!j                  |d         j                   |<   |S c c}}}w c c}}}w )z Will generate a textclip if it hasn't been generated asked
            to generate it yet. If there is no subtitle to show at t, return
            false. Fr      )	textclipskeys	subtitlesmake_textclip)ttatbr   subselfs        r   add_textclip_if_nonez4SubtitlesClip.__init__.<locals>.add_textclip_if_none:   s     6:^^5H5H5J " "MWbS1xRx r73- "C ":>.. # #'2b#Ab B} # # a&C$..--//&*&8&8Q&@s#J"#s   !C!C
c                      |       }|rj                   |   j                  |       S t        j                  g dgg      S )N)r   r   r   )r   	get_framenparrayr   r!   r#   r"   s     r   
make_framez*SubtitlesClip.__init__.<locals>.make_frameK   s@    &q)C8;DNN3'11!4 0G9+.0r   c                      |       }|r(j                   |   j                  j                  |       S t        j                  dgg      S )Nr   )r   maskr%   r&   r'   r(   s     r   make_mask_framez/SubtitlesClip.__init__.<locals>.make_mask_frameP   sE    &q)C=@DNN3',,66q9 *A3%*r   TT)ismask)r   __init__
isinstancestrfile_to_subtitlesr   dictr   r   startmaxdurationendr)   boolr+   )
r"   r   r   r   r    r   r)   r,   hasmaskr#   s
   `        @r   r/   zSubtitlesClip.__init__%   s    459i%))4I # PM +
dnnEENWbcREF	"	0
	*
 %t))#.334?FIod;	D	A Fs   3C:c           	          fd}fd}| j                   D cg c]  \  \  }}} |||      r |||      |f c}}}S c c}}}w )z Returns a sequence of [(t1,t2), txt] covering all the given subclip
        from t_start to t_end. The first and last times will be cropped so as
        to be exactly t_start and t_end if possible. c                 X    	 | cxk  xr k  nc xs |cxk  xr k  S c S #  Y yxY w)NF t1t2t_endt_starts     r   is_in_subclipz/SubtitlesClip.in_subclip.<locals>.is_in_subclip^   s7    )E)Cw/BU/BC/BCs   % % )c                 L    	 t        |       t        |      fS #  | |fcY S xY wN)r5   minr=   s     r   try_croppingz.SubtitlesClip.in_subclip.<locals>.try_croppingc   s0     B(#b%.99 Bxs    #r   )r"   rA   r@   rB   rF   r>   r?   r   s    ``     r   
in_subclipzSubtitlesClip.in_subclipY   s\    
	
	 
 BF H H~B2?22F b$c* H 	H Hs   !Ac                 ,    t        | j                        S rD   )iterr   )r"   s    r   __iter__zSubtitlesClip.__iter__m   s    DNN##r   c                      | j                   |   S rD   rG   )r"   ks     r   __getitem__zSubtitlesClip.__getitem__r   s    ~~a  r   c                 R    d dj                  fd| j                  D              S )Nc                 R    | \  \  }}}t        |      }t        |      }|d|d|S )Nz - 
r   )sub_elementr   r    r   ftaftbs         r   to_srtz%SubtitlesClip.__str__.<locals>.to_srty   s.    'MHRc*C*C"%sC00r   z

c              3   .   K   | ]  } |        y wrD   r<   ).0srU   s     r   	<genexpr>z(SubtitlesClip.__str__.<locals>.<genexpr>   s     =6!9=s   )joinr   )r"   rU   s    @r   __str__zSubtitlesClip.__str__w   s#    	1 {{=dnn===r   c           
          t        | j                  D cg c]   }t        j                  ||d         g k7  r|" c}      S c c}w )Nr   )r   r   refindall)r"   expres      r   
match_exprzSubtitlesClip.match_expr   sE     ?A!#D!A$!72!=   ? @ 	@ ?s   %A c                 z    t        |d      5 }|j                  t        |              d d d        y # 1 sw Y   y xY w)Nzw+)openwriter1   )r"   filenamefs      r   	write_srtzSubtitlesClip.write_srt   s3    (D! 	QGGCI	 	 	s   1:rD   )NN)__name__
__module____qualname____doc__r/   rH   rK   rN   r[   ra   rg   r<   r   r   r   r      s.    22QhH($
!
>@r   r   c                 X   g }d}d}t        | d      5 }|D ]v  }t        j                  d|      }|r|D cg c]  }t        |       }}4|j	                         dk(  r(|j                  ||j	                  d      f       d\  }}o|sr||z  }x 	 ddd       |S c c}w # 1 sw Y   |S xY w)z Converts a srt file into subtitles.

    The returned list is of the form ``[((ta,tb),'some text'),...]``
    and can be fed to SubtitlesClip.

    Only works for '.srt' format for the moment.
    N rz([0-9]*:[0-9]*:[0-9]*,[0-9]*)rQ   )Nrm   )rc   r]   r^   r   stripappend)re   times_textscurrent_timescurrent_textrf   linetimesr   s           r   r2   r2      s     KML	hs	 	%q 	%DJJ>EE49 :q : :#""M<3E3Ed3K#LM.6+|$	%	%  !;		% s#   "BBA BBBB))rk   r]   numpyr&   moviepy.toolsr   moviepy.video.VideoClipr   r   r   r2   r<   r   r   <module>ry      s(    2 	    7@I @Fr   