
    -i                         d 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
  G d de
          Z ed          d
d	            ZdS )z*Experimental module for subtitles support.    N)convert_path_to_stringconvert_to_seconds)TextClip	VideoClipc                   @    e Zd ZdZd
dZddZd Zd Zd Zd Z	d	 Z
dS )SubtitlesClipax  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 as a string or path-like object, or a list

    font
        Path to a font file to be used. Optional if make_textclip is provided.

    make_textclip
        A custom function to use for text clip generation. If None, a TextClip
        will be generated.

        The function must take a text as argument and return a VideoClip
        to be used as caption

    encoding
        Optional, specifies srt file encoding.
        Any standard Python encoding is allowed (listed at
        https://docs.python.org/3.8/library/codecs.html#standard-encodings)

    Examples
    --------

    .. code:: python

        from moviepy.video.tools.subtitles import SubtitlesClip
        from moviepy.video.io.VideoFileClip import VideoFileClip
        generator = lambda text: TextClip(text, font='./path/to/font.ttf',
                                        font_size=24, color='white')
        sub = SubtitlesClip("subtitles.srt", make_textclip=generator, encoding='utf-8')
        myvideo = VideoFileClip("myvideo.avi")
        final = CompositeVideoClip([clip, subtitles])
        final.write_videofile("final.mp4", fps=myvideo.fps)

    Nc                 *    t          j         d           t          |t                    st	          ||          }| _        t                       _        | _        | j        t          d           fd}| _
        d _        t          d  j        D                        _         j         _         fd fd	} fd
}| _        t!           
                    d          j                  }|rt          |d          nd  _        d S )NF)has_constant_sizeencodingz3Argument font is required if make_textclip is None.c                 8    t          j        | dddd          S )N   z#ffffffz#000000   )fonttext	font_sizecolorstroke_colorstroke_width)r   r   )txtselfs    [/var/www/html/speakWrite/venv/lib/python3.11/site-packages/moviepy/video/tools/subtitles.pymake_textclipz-SubtitlesClip.__init__.<locals>.make_textclipJ   s.     #!*!"       r   c                     g | ]
\  \  }}}|S  r   ).0tatbr   s       r   
<listcomp>z*SubtitlesClip.__init__.<locals>.<listcomp>V   s     FFFOXb"sRFFFr   c                      fdj                                         D             }|s fdj        D             }|sdS |d         }|j                                         vr#                    |d                   j         |<   |S )zWill 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.
            c                 H    g | ]\  \  }}}|cxk    r|k     n n||f|fS r   r   r   
text_starttext_endr   ts       r   r!   zHSubtitlesClip.__init__.<locals>.add_textclip_if_none.<locals>.<listcomp>^   sX       2+j(T!....h..... h'....r   c                 H    g | ]\  \  }}}|cxk    r|k     n n||f|fS r   r   r$   s       r   r!   zHSubtitlesClip.__init__.<locals>.add_textclip_if_none.<locals>.<listcomp>d   sX       6/*h"a2222(22222 !(+T2222r   Fr   r   )	textclipskeys	subtitlesr   )r'   subr   s   ` r   add_textclip_if_nonez4SubtitlesClip.__init__.<locals>.add_textclip_if_noneY   s    
   6:n6I6I6K6K  C
  !   :>.  
  ! 5a&C$.--////&*&8&8Q&@&@s#Jr   c                      |           }|r j         |                             |           nt          j        g dgg          S )N)r   r   r   )r)   	get_framenparrayr'   r,   r-   r   s     r   frame_functionz.SubtitlesClip.__init__.<locals>.frame_functionq   sJ    &&q))C7:W4>#&00333999+@W@WWr   c                      |           }|r%j         |         j                            |           nt          j        dgg          S )Nr   )r)   maskr/   r0   r1   r2   s     r   make_mask_framez/SubtitlesClip.__init__.<locals>.make_mask_frameu   sH    &&q))C<?T4>#&+55a888RXPQse__Tr   TT)is_mask)r   __init__
isinstancelistfile_to_subtitlesr+   dictr)   r   
ValueErrorr   startmaxdurationendr3   boolr5   )	r   r+   r   r   r   r3   r6   hasmaskr-   s	   `       @r   r9   zSubtitlesClip.__init__8   su   459999)T** 	H))hGGGI #	 y  !VWWW     +
FFt~FFFGG=	 	 	 	 	0	X 	X 	X 	X 	X 	X	U 	U 	U 	U 	U 	U -t))#..344@GQIot<<<<T			r   c                 J    fdfdfd| j         D             S )zReturns a sequence of [(t1,t2), text] covering all the given subclip
        from start_time to end_time. The first and last times will be cropped so as
        to be exactly start_time and end_time if possible.
        c                 h    	 | cxk    ok     nc p|cxk     ok    nc S # t           $ r Y dS w xY w)NF)	Exceptiont1t2end_time
start_times     r   is_in_subclipz/SubtitlesClip.in_subclip.<locals>.is_in_subclip   sm    "b333383333Ub9T9T9T9TH9T9T9T9TU   uus   # 
11c                 n    	 t          |           t          |          fS # t          $ r | |fcY S w xY wN)r@   minrG   rH   s     r   try_croppingz.SubtitlesClip.in_subclip.<locals>.try_cropping   sO    2z**CH,=,===   2vs   # 44c                 P    g | ]"\  \  }}} ||           ||          |f#S r   r   )r   rI   rJ   r   rM   rQ   s       r   r!   z,SubtitlesClip.in_subclip.<locals>.<listcomp>   sT     
 
 
"b3}R$$
\"b!!3'
 
 
r   r+   )r   rL   rK   rM   rQ   s    ``@@r   
in_subclipzSubtitlesClip.in_subclip}   st    	 	 	 	 	 		 	 	 	 	 	
 
 
 
 
#'>
 
 
 	
r   c                 *    t          | j                  S rO   )iterr+   )r   s    r   __iter__zSubtitlesClip.__iter__   s    DN###r   c                     | j         |         S rO   rS   )r   ks     r   __getitem__zSubtitlesClip.__getitem__   s    ~a  r   c                 V    d d                     fd| j        D                       S )Nc                 b    | \  \  }}}t          |          }t          |          }|d|d|S )Nz - 
r   )sub_elementrL   rK   r   formatted_start_timeformatted_end_times         r   to_srtz%SubtitlesClip.__str__.<locals>.to_srt   sI    +6("ZD#5j#A#A !3H!=!=$8$8$8:L:L:LddSSr   z

c              3   .   K   | ]} |          V  d S rO   r   )r   r,   ra   s     r   	<genexpr>z(SubtitlesClip.__str__.<locals>.<genexpr>   s+      AA366#;;AAAAAAr   )joinr+   )r   ra   s    @r   __str__zSubtitlesClip.__str__   s@    	T 	T 	T {{AAAA$.AAAAAAr   c                 D    t          fd| j        D                       S )z?Matches a regular expression against the subtitles of the clip.c                 R    g | ]#}t          j        |d                    g k    !|$S )r   )refindall)r   r,   exprs     r   r!   z,SubtitlesClip.match_expr.<locals>.<listcomp>   s3    MMMSbjs1v.F.F".L.LS.L.L.Lr   )r	   r+   )r   rj   s    `r   
match_exprzSubtitlesClip.match_expr   s.    MMMMDNMMM
 
 	
r   c                     t          |d          5 }|                    t          |                      ddd           dS # 1 swxY w Y   dS )z5Writes an ``.srt`` file with the content of the clip.zw+N)openwritestr)r   filenamefiles      r   	write_srtzSubtitlesClip.write_srt   s    (D!! 	"TJJs4yy!!!	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	"s   #AAA)NNN)NN)__name__
__module____qualname____doc__r9   rT   rW   rZ   re   rk   rr   r   r   r   r	   r	      s        ) )VCR CR CR CRJ
 
 
 
0$ $ $! ! !B B B
 
 
" " " " "r   r	   rp   c                 X   g }d}d}t          | d|          5 }|D ]u}t          j        d|          }|rd |D             }&|                                dk    r0|                    ||                    d          f           d\  }}n|r||z  }v	 ddd           n# 1 swxY w Y   |S )	zConverts a srt file into subtitles.

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

    Only works for '.srt' format for the moment.
    N rr   z([0-9]*:[0-9]*:[0-9]*,[0-9]*)c                 ,    g | ]}t          |          S r   r   )r   r'   s     r   r!   z%file_to_subtitles.<locals>.<listcomp>   s!     F F F1!3A!6!6 F F Fr   r]   )Nrx   )rm   rh   ri   stripappend)rp   r   times_textscurrent_timescurrent_textrq   linetimess           r   r<   r<      s    KML	hh	/	/	/ 	%4 	% 	%DJ>EEE % F F F F F##""M<3E3Ed3K3K#LMMM.6+|| %$	%	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% s   A9BB#&B#rO   )rv   rh   numpyr0   moviepy.decoratorsr   moviepy.toolsr   moviepy.video.VideoClipr   r   r	   r<   r   r   r   <module>r      s    0 0 				     5 5 5 5 5 5 , , , , , , 7 7 7 7 7 7 7 7a" a" a" a" a"I a" a" a"H 
##   $#  r   