
    W|h                     v    S r SSKrSSKrSSKJr  SSKJr  SSKJ	r	J
r
   " S S\
5      r\" S5      S
S	 j5       rg)z*Experimental module for subtitles support.    N)convert_path_to_stringconvert_to_seconds)TextClip	VideoClipc                   J    \ rS rSrSrSS jrSS jrS rS rS r	S	 r
S
 rSrg)SubtitlesClip   a   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           
      V  ^ ^ [         R                  " T SS9  [        U[        5      (       d	  [	        XS9nUT l        [        5       T l        UT l        Uc  T R                  c  [        S5      eU 4S jnUT l
        ST l        [        T R
                   VVVs/ s H	  u  u  pVovPM     snnn5      T l        T R                  T l        U 4S jmUU 4S jnUU 4S	 jn	UT l        [!        T R                  S
5      R"                  5      n
U
(       a  [        U	SS9T l        g S T l        g s  snnnf )NF)has_constant_sizeencodingz3Argument font is required if make_textclip is None.c           	      4   > [        TR                  U SSSSS9$ )N   z#ffffffz#000000   )fonttext	font_sizecolorstroke_colorstroke_width)r   r   )txtselfs    W/home/james-whalen/.local/lib/python3.13/site-packages/moviepy/video/tools/subtitles.pymake_textclip-SubtitlesClip.__init__.<locals>.make_textclipJ   s&     #!*!"     r   c                   > TR                   R                  5        VVVs/ s H  u  u  pnXs=::  a  U:  d  M  O  M  X4U4PM!     nnnnU(       dB  TR                   VVVs/ s H  u  u  pnXs=::  a  U:  d  M  O  M  X4U4PM!     nnnnU(       d  gUS   nUTR                   R                  5       ;  a!  TR                  US   5      TR                   U'   U$ s  snnnf s  snnnf )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_none4SubtitlesClip.__init__.<locals>.add_textclip_if_noneY   s     7;nn6I6I6K6K2+jT.h. /. /*'.6K  
  ;?..:H6/*"2(2 32 3j+T2:H  
  a&C$..--//&*&8&8Q&@s#J#s!   CC	C!C :C >	C c                    > T" U 5      nU(       a  TR                   U   R                  U 5      $ [        R                  " / SQ//5      $ )N)r   r   r   )r   	get_framenparrayr"   r%   r&   r   s     r   frame_function.SubtitlesClip.__init__.<locals>.frame_functionq   s;    &q)C7:4>>#&003W9+@WWr   c                    > T" U 5      nU(       a(  TR                   U   R                  R                  U 5      $ [        R                  " S//5      $ )Nr   )r   maskr)   r*   r+   r,   s     r   make_mask_frame/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-   boolr0   )r   r!   r   r   r   tatbr   r-   r1   hasmaskr&   s   `          @r   r5   SubtitlesClip.__init__8   s    459)T**))GI #	 yy  !VWW +
t~~F~OXbsR~FG==	0	X	U -t))#.334@GIot<	T	K Gs   D$c           	         ^^ UU4S jnUU4S jnU R                    VVVs/ s H!  u  u  pVnU" XV5      (       d  M  U" XV5      U4PM#     snnn$ s  snnnf )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                    >  TU s=:*  =(       a    T:  Os  =(       d    TUs=:  =(       a    T:*  $ s  $ ! [          a     gf = f)NF)	Exceptiont1t2end_time
start_times     r   is_in_subclip/SubtitlesClip.in_subclip.<locals>.is_in_subclip   sE    "b3383Ub9T9TH9TU9TU s   -4 4 
A Ac                 \   >  [        U T5      [        UT5      4$ ! [         a    X4s $ f = fN)r<   minrF   rG   s     r   try_cropping.SubtitlesClip.in_subclip.<locals>.try_cropping   s7    2z*CH,=== vs    ++r!   )r   rK   rJ   rL   rQ   rH   rI   r   s    ``     r   
in_subclipSubtitlesClip.in_subclip}   sR    		 $(>>
#1"3R$ (\"!3'#1
 	
 
s
   AAc                 ,    [        U R                  5      $ rO   )iterr!   )r   s    r   __iter__SubtitlesClip.__iter__   s    DNN##r   c                      U R                   U   $ rO   rS   )r   ks     r   __getitem__SubtitlesClip.__getitem__   s    ~~a  r   c                 T   ^ S mSR                  U4S jU R                   5       5      $ )Nc                 V    U u  u  pn[        U5      n[        U5      nU< SU< SU< 3$ )Nz - 
r   )sub_elementrK   rJ   r   formatted_start_timeformatted_end_times         r   to_srt%SubtitlesClip.__str__.<locals>.to_srt   s3    +6("ZD#5j#A !3H!=$8:LdSSr   z

c              3   4   >#    U  H  nT" U5      v   M     g 7frO    ).0r%   rd   s     r   	<genexpr>(SubtitlesClip.__str__.<locals>.<genexpr>   s     A.36#;;.s   )joinr!   )r   rd   s    @r   __str__SubtitlesClip.__str__   s$    	T {{A$..AAAr   c           
          [        U R                   Vs/ s H$  n[        R                  " XS   5      / :w  d  M"  UPM&     sn5      $ s  snf )z?Matches a regular expression against the subtitles of the clip.r   )r	   r!   refindall)r   exprr%   s      r   
match_exprSubtitlesClip.match_expr   s<     NNMNSbjj1v.F".LSNM
 	
Ms
   !AAc                     [        US5       nUR                  [        U 5      5        SSS5        g! , (       d  f       g= f)z5Writes an ``.srt`` file with the content of the clip.zw+N)openwritestr)r   filenamefiles      r   	write_srtSubtitlesClip.write_srt   s*    (D!TJJs4y! "!!s   1
?)	r=   r>   r   r-   r   r0   r;   r!   r   )NNN)NN)__name__
__module____qualname____firstlineno____doc__r5   rT   rX   r\   rl   rr   rz   __static_attributes__rg   r   r   r	   r	      s/    )VCRJ
0$!B
"r   r	   rx   c                    / nSnSn[        U SUS9 nU H  n[        R                  " SU5      nU(       a  U Vs/ s H  n[        U5      PM     nnM>  UR	                  5       S:X  a'  UR                  X4R	                  S5      45        Su  p4My  U(       d  M  XF-  nM     SSS5        U$ s  snf ! , (       d  f       U$ = f)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]*)r`   )Nr   )ru   ro   rp   r   stripappend)	rx   r   times_textscurrent_timescurrent_textry   linetimesr"   s	            r   r8   r8      s     KML	hh	/4DJJ>EE@E F1!3A!6 F#""M3E3Ed3K#LM.6+|$  
0  !G	 
0	/ s#   (B.B)AB.B.)B..
B=rO   )r   ro   numpyr*   moviepy.decoratorsr   moviepy.toolsr   moviepy.video.VideoClipr   r   r	   r8   rg   r   r   <module>r      s?    0 	  5 , 7a"I a"H 
# $r   