
    g                     t    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y)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
y)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           
      F    t        j                   d       t        |t              st	        ||      }| _        t                _        | _        | j                  t        d       f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_sizeencodingz3Argument font is required if make_textclip is None.c                 :    t        j                  | dddd      S )N   z#ffffffz#000000   )fonttext	font_sizecolorstroke_colorstroke_width)r   r   )txtselfs    f/var/www/it7/html/youtubeDownloader/venv/lib/python3.12/site-packages/moviepy/video/tools/subtitles.pymake_textclipz-SubtitlesClip.__init__.<locals>.make_textclipJ   s&     #!*!"     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 )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.
            Fr   r   )	textclipskeys	subtitlesr   )t
text_starttext_endr   subr   s        r   add_textclip_if_nonez4SubtitlesClip.__init__.<locals>.add_textclip_if_noneY   s     7;nn6I6I6K 2+j(T!.h. h'.C 
  ;?.. 6/*h"a2(2 !(+T2 
  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   frame_functionz.SubtitlesClip.__init__.<locals>.frame_functionq   s;    &q)C7:4>>#&003W9+@WWr   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_frameu   sA    &q)C<?4>>#&++55a8TRXXPQse_Tr   TT)is_mask)r   __init__
isinstancelistfile_to_subtitlesr   dictr   r   
ValueErrorr   startmaxdurationendr*   boolr,   )r   r   r   r   r   tatbr   r*   r-   hasmaskr$   s   `          @r   r0   zSubtitlesClip.__init__8   s    459)T*))hGI #	 yy  !VWW +
t~~FFOXb"sRFG==	0	X	U -t))#.334@GIot<	T	K Gs   Dc           	          fd}fd}| j                   D cg c]  \  \  }}} |||      r |||      |f c}}}S c c}}}w )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  xr k  nc xs |cxk  xr k  S c S # t         $ r Y yw xY w)NF)	Exceptiont1t2end_time
start_times     r   is_in_subclipz/SubtitlesClip.in_subclip.<locals>.is_in_subclip   s?    "b383Ub9TH9TU9TU s   % % 	11c                 \    	 t        |       t        |      fS # t        $ r | |fcY S w xY wN)r7   minr@   rA   s     r   try_croppingz.SubtitlesClip.in_subclip.<locals>.try_cropping   s9    2z*CH,=== 2vs    ++r   )r   rE   rD   rF   rJ   rB   rC   r   s    ``     r   
in_subclipzSubtitlesClip.in_subclip}   sV    		 $(>>
 
"b3R$ "b!3'
 	
 
s   !Ac                 ,    t        | j                        S rH   )iterr   )r   s    r   __iter__zSubtitlesClip.__iter__   s    DNN##r   c                      | j                   |   S rH   rK   )r   ks     r   __getitem__zSubtitlesClip.__getitem__   s    ~~a  r   c                 R    d dj                  fd| j                  D              S )Nc                 R    | \  \  }}}t        |      }t        |      }|d|d|S )Nz - 
r   )sub_elementrE   rD   r   formatted_start_timeformatted_end_times         r   to_srtz%SubtitlesClip.__str__.<locals>.to_srt   s5    +6("ZD#5j#A !3H!=$8:LdSSr   z

c              3   .   K   | ]  } |        y wrH    ).0r#   rY   s     r   	<genexpr>z(SubtitlesClip.__str__.<locals>.<genexpr>   s     A36#;As   )joinr   )r   rY   s    @r   __str__zSubtitlesClip.__str__   s$    	T {{A$..AAAr   c           
          t        | j                  D cg c]!  }t        j                  ||d         g k7  s |# c}      S c c}w )z?Matches a regular expression against the subtitles of the clip.r   )r	   r   refindall)r   exprr#   s      r   
match_exprzSubtitlesClip.match_expr   s:     NNMSbjjs1v.F".LSM
 	
Ms
   !AAc                 z    t        |d      5 }|j                  t        |              ddd       y# 1 sw Y   yxY w)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   s3    (D! 	"TJJs4y!	" 	" 	"s   1:)NNN)NN)__name__
__module____qualname____doc__r0   rL   rO   rR   r_   rd   rk   r[   r   r   r	   r	      s/    )VCRJ
0$!B
"r   r	   ri   c                 \   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)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]*)rU   )Nrq   )rf   ra   rb   r   stripappend)	ri   r   times_textscurrent_timescurrent_textrj   linetimesr    s	            r   r3   r3      s     KML	hh	/ 	%4 	%DJJ>EE@E F1!3A!6 F F#""M<3E3Ed3K#LM.6+|$	%	%  !G		% s#   "B!B	A B!
B!B!!B+rH   )ro   ra   numpyr'   moviepy.decoratorsr   moviepy.toolsr   moviepy.video.VideoClipr   r   r	   r3   r[   r   r   <module>r~      s?    0 	  5 , 7a"I a"H 
# $r   