
    w;3hH              
       0   d Z ddlmZ ddlZddlZddlmZ ddlZddlZ ej                  e
      ZdZdZdZdj                  ej                  egd	z        ed
eg      Zdj#                  dj                  ej                  dez   dz   gd	z        ed
dedg            ZdZdZdZdZ ej.                  e      Z ej.                  d      Z ej.                  dj#                  eeeee      ej4                        Z ed      Zdd fdd fdd ffZdZdZdZ dZ!	 e"ejF                  fZ$ejL                   G d d e'             Z(d! Z)d" Z*d# Z+d/d$Z,d% Z-d0d&Z.d' Z/	 d1d(Z0 G d) d*e1      Z2 G d+ d,e3      Z4 G d- d.e1      Z5y# e%$ r ejF                  fZ$Y lw xY w)2z?A tiny library for parsing, modifying, and composing SRT files.    )unicode_literalsN)	timedeltau   [,.:，．。：]z[0-9]+z[0-9]*    ?z^{}$()z-?[0-9]+\.?[0-9]*z[^\r\n]*z.*?z\r?\nz\n\n+z\s*(?:({idx})\s*{eof})?({ts}) *-[ -] *> *({ts}) ?({proprietary})(?:{eof}|\Z)({content})(?:{eof}|\Z)(?:{eof}|\Z|(?=(?:{idx}\s*{eof}{ts})))(?=(?:(?:{idx}\s*{eof})?{ts}|\Z)))idxtsproprietarycontenteofz
No contentc                 8    | j                   j                          S N)r   stripsubs    C/var/www/html/audio-gradio/venv/lib/python3.12/site-packages/srt.py<lambda>r   Q   s    3;;#4#4#66     zStart time < 0 secondsc                 (    | j                   t        k  S r   )startZERO_TIMEDELTAr   s    r   r   r   R   s    399~+E r   zSubtitle start time >= end timec                 4    | j                   | j                  k\  S r   )r   endr   s    r   r   r   S   s    CII4H r   i  <      i  c                   8    e Zd ZdZd	dZd Zd Zd Zd Zd
dZ	y)Subtitlea:  
    The metadata relating to a single subtitle. Subtitles are sorted by start
    time by default. If no index was provided, index 0 will be used on writing
    an SRT block.

    :param index: The SRT index for this subtitle
    :type index: int or None
    :param start: The time that the subtitle should start being shown
    :type start: :py:class:`datetime.timedelta`
    :param end: The time that the subtitle should stop being shown
    :type end: :py:class:`datetime.timedelta`
    :param str proprietary: Proprietary metadata for this subtitle
    :param str content: The subtitle content. Should not contain OS-specific
                        line separators, only \\n. This is taken care of
                        already if you use :py:func:`srt.parse` to generate
                        Subtitle objects.
    c                 J    || _         || _        || _        || _        || _        y r   indexr   r   r   r   )selfr"   r   r   r   r   s         r   __init__zSubtitle.__init__v   s&    

&r   c                 X    t        t        t        |       j                                     S r   )hash	frozensetvarsitems)r#   s    r   __hash__zSubtitle.__hash__}   s    Id4j..0122r   c                 0    t        |       t        |      k(  S r   )r(   r#   others     r   __eq__zSubtitle.__eq__   s    DzT%[((r   c                     | j                   | j                  | j                  f|j                   |j                  |j                  fk  S r   )r   r   r"   r,   s     r   __lt__zSubtitle.__lt__   s<    

DHHdjj1KKIIKK5
 
 	
r   c           	          t        t        |       dt        t        |       d            }dj                  d  |       D              }t        |       j                  d|dS )N	iteritemsr)   z, c              3   0   K   | ]  \  }}|d |  yw)=N ).0kvs      r   	<genexpr>z$Subtitle.__repr__.<locals>.<genexpr>   s     G41aA.Gs   r   r	   )getattrr(   jointype__name__)r#   	var_items	item_lists      r   __repr__zSubtitle.__repr__   sJ    DJWT$Z5QR	IIG9;GG	:..	::r   c                 ,   | j                   }| j                  }|rd|z   }|rt        |      }|d}n|dk7  r|j                  d|      }d}|j	                  | j
                  xs dt        | j                        t        | j                        |||      S )a  
        Convert the current :py:class:`Subtitle` to an SRT block.

        :param bool strict: If disabled, will allow blank lines in the content
                            of the SRT block, which is a violation of the SRT
                            standard and may cause your media player to explode
        :param str eol: The end of line string to use (default "\\n")
        :returns: The metadata of the current :py:class:`Subtitle` object as an
                  SRT formatted subtitle block
        :rtype: str
         
z9{idx}{eol}{start} --> {end}{prop}{eol}{content}{eol}{eol}r   )r
   r   r   propr   eol)	r   r   make_legal_contentreplaceformatr"   timedelta_to_srt_timestampr   r   )r#   strictrE   output_contentoutput_proprietarytemplates         r   to_srtzSubtitle.to_srt   s     !-- "%'9!9/?N;CD[+33D#>NN

a,TZZ8*4884#"  
 	
r   N)r   )TrC   )
r=   
__module____qualname____doc__r$   r*   r.   r0   r@   rN   r5   r   r   r   r   a   s%    &'3)
;$
r   r   c                     | r| d   dk7  rd| vr| S t         j                  d| j                  d            }t        j	                  d| |       |S )aL  
    Remove illegal content from a content block. Illegal content includes:

    * Blank lines
    * Starting or ending with a blank line

    .. doctest::

        >>> make_legal_content('\nfoo\n\nbar\n')
        'foo\nbar'

    :param str content: The content to make legal
    :returns: The legalised content
    :rtype: srt
    r   rC   z

zLegalised content %r to %r)MULTI_WS_REGEXr   r   LOGinfo)r   legal_contents     r   rF   rF      sP    & 71:%&*?"&&tW]]4-@AMHH)7MBr   c                     t        | j                  t              \  }}|| j                  t        z  z  }t        |t
              \  }}| j                  t        z  }d||||fz  S )a  
    Convert a :py:class:`~datetime.timedelta` to an SRT timestamp.

    .. doctest::

        >>> import datetime
        >>> delta = datetime.timedelta(hours=1, minutes=23, seconds=4)
        >>> timedelta_to_srt_timestamp(delta)
        '01:23:04,000'

    :param datetime.timedelta timedelta_timestamp: A datetime to convert to an
                                                   SRT timestamp
    :returns: The timestamp in SRT format
    :rtype: str
    z%02d:%02d:%02d,%03d)divmodsecondsSECONDS_IN_HOURdaysHOURS_IN_DAYSECONDS_IN_MINUTEmicrosecondsMICROSECONDS_IN_MILLISECOND)timedelta_timestamphrssecs_remainderminssecsmsecss         r   rI   rI      sh    " !!4!<!<oNC##l22C(9:JD$,,0KKE CtU#;;;r   c                     t         j                  |       }|t        dj                  |             |j	                         D cg c]  }|rt        |      nd c}\  }}}}t        ||||      S c c}w )a  
    Convert an SRT timestamp to a :py:class:`~datetime.timedelta`.

    .. doctest::

        >>> srt_timestamp_to_timedelta('01:23:04,000')
        datetime.timedelta(seconds=4984)

    :param str timestamp: A timestamp in SRT format
    :returns: The timestamp as a :py:class:`~datetime.timedelta`
    :rtype: datetime.timedelta
    :raises TimestampParseError: If the timestamp is not parseable
    zUnparseable timestamp: {}r   )hoursminutesrY   milliseconds)TS_REGEXmatchTimestampParseErrorrH   groupsintr   )	timestamprk   mra   rc   rd   re   s          r   srt_timestamp_to_timedeltarq      so     NN9%E}!"="D"DY"OPP:?,,.IQc!fq0ICtU3dOO Js   A1c              #   n  K   d}t        t        |       |      D ]7  \  }}|st        di t        |      }|r	 t	        |       ||z
  |_        | 9 y# t
        $ rS}|j                  t        j                  d|       n!t        j                  d|j                  |       |dz  }Y d}~d}~ww xY ww)a  
    Reorder subtitles to be sorted by start time order, and rewrite the indexes
    to be in that same order. This ensures that the SRT file will play in an
    expected fashion after, for example, times were changed in some subtitles
    and they may need to be resorted.

    If skip=True, subtitles will also be skipped if they are considered not to
    be useful. Currently, the conditions to be considered "not useful" are as
    follows:

    - Content is empty, or only whitespace
    - The start time is negative
    - The start time is equal to or later than the end time

    .. doctest::

        >>> from datetime import timedelta
        >>> one = timedelta(seconds=1)
        >>> two = timedelta(seconds=2)
        >>> three = timedelta(seconds=3)
        >>> subs = [
        ...     Subtitle(index=999, start=one, end=two, content='1'),
        ...     Subtitle(index=0, start=two, end=three, content='2'),
        ... ]
        >>> list(sort_and_reindex(subs))  # doctest: +ELLIPSIS
        [Subtitle(...index=1...), Subtitle(...index=2...)]

    :param subtitles: :py:class:`Subtitle` objects in any order
    :param int start_index: The index to start from
    :param bool in_place: Whether to modify subs in-place for performance
                          (version <=1.0.0 behaviour)
    :param bool skip: Whether to skip subtitles considered not useful (see
                      above for rules)
    :returns: The sorted subtitles
    :rtype: :term:`generator` of :py:class:`Subtitle` objects
    r   )r   Nz"Skipped subtitle with no index: %sz Skipped subtitle at index %d: %s   r5   )		enumeratesortedr   r(   _should_skip_sub_ShouldSkipExceptionr"   rT   rU   )	subtitlesstart_indexin_placeskipskipped_subssub_numsubtitle
thrown_excs           r   sort_and_reindexr      s     J L&vi'8L 1$x.1H
 * !</' ( >>)HHA:NHH:HNNJ !s/   7B5AB5	B2A	B-(B5-B22B5c                 F    t         D ]  \  }} ||       st        |       y)z
    Check if a subtitle should be skipped based on the rules in
    SUBTITLE_SKIP_CONDITIONS.

    :param subtitle: A :py:class:`Subtitle` to check whether to skip
    :raises _ShouldSkipException: If the subtitle should be skipped
    N)SUBTITLE_SKIP_CONDITIONSrw   )r~   info_msgsub_skippers      r   rv   rv   <  s+     ": 1+x &x001r   c              #   *  K   d}t        | t              r| j                         } t        j	                  |       D ]  }|j                         }t        | |||       |j                         \  }}}}}	|	j                  dd      }		 t        |      }t        |t        |      t        |      |	|       |j                         } t        | |t!        |       |       y# t        $ r  t        |j                  d      d         }Y vt        $ r Y w xY ww)aZ  
    Convert an SRT formatted string (in Python 2, a :class:`unicode` object) to
    a :term:`generator` of Subtitle objects.

    This function works around bugs present in many SRT files, most notably
    that it is designed to not bork when presented with a blank line as part of
    a subtitle's content.

    .. doctest::

        >>> subs = parse("""\
        ... 422
        ... 00:31:39,931 --> 00:31:41,931
        ... Using mainly spoons,
        ...
        ... 423
        ... 00:31:41,933 --> 00:31:43,435
        ... we dig a tunnel under the city and release it into the wild.
        ...
        ... """)
        >>> list(subs)  # doctest: +ELLIPSIS
        [Subtitle(...index=422...), Subtitle(...index=423...)]

    :param srt: Subtitles in SRT format
    :type srt: str or a file-like object
    :param ignore_errors: If True, garbled SRT data will be ignored, and we'll
                          continue trying to parse the rest of the file,
                          instead of raising :py:class:`SRTParseError` and
                          stopping execution.
    :returns: The subtitles contained in the SRT file as :py:class:`Subtitle`
              objects
    :rtype: :term:`generator` of :py:class:`Subtitle` objects
    :raises SRTParseError: If the matches are not contiguous and
                           ``ignore_errors`` is False.
    r   z
rC   .r!   N)
isinstance
FILE_TYPESread	SRT_REGEXfinditerr   _check_contiguityrm   rG   rn   
ValueErrorsplit	TypeErrorr   rq   r   len)
srtignore_errorsexpected_startrk   actual_start	raw_index	raw_startraw_endr   r   s
             r   parser   I  s    J N #z"hhj##C( %{{}#~|]K>Clln;	9g{G
 //&$/	II ,Y7*73#
 	
 ?%B c>3s8]C+  	5
 IOOC034I 	 	s7   BDCAD&DDDDDDc                     ||k7  rF| || }|dk(  r|j                         s|dk(  ry|rt        j                  d|       yt        |||      y)a<  
    If ``warn_only`` is False, raise :py:class:`SRTParseError` with diagnostic
    info if expected_start does not equal actual_start. Otherwise, log a
    warning.

    :param str srt: The data being matched
    :param int expected_start: The expected next start, as from the last
                               iteration's match.end()
    :param int actual_start: The actual start, as from this iteration's
                             match.start()
    :raises SRTParseError: If the matches are not contiguous and ``warn_only``
                           is False
    r   u   ﻿Nz Skipped unparseable SRT data: %r)isspacerT   warningSRTParseError)r   r   r   	warn_onlyunmatched_contents        r   r   r     sb     %|<Q%%'+<+H KK:<MN>OPP &r   c                 \    |rt        | ||      } dj                  fd| D              S )a  
    Convert an iterator of :py:class:`Subtitle` objects to a string of joined
    SRT blocks.

    .. doctest::

        >>> from datetime import timedelta
        >>> start = timedelta(seconds=1)
        >>> end = timedelta(seconds=2)
        >>> subs = [
        ...     Subtitle(index=1, start=start, end=end, content='x'),
        ...     Subtitle(index=2, start=start, end=end, content='y'),
        ... ]
        >>> compose(subs)  # doctest: +ELLIPSIS
        '1\n00:00:01,000 --> 00:00:02,000\nx\n\n2\n00:00:01,000 --> ...'

    :param subtitles: The subtitles to convert to SRT blocks
    :type subtitles: :term:`iterator` of :py:class:`Subtitle` objects
    :param bool reindex: Whether to reindex subtitles based on start time
    :param int start_index: If reindexing, the index to start reindexing from
    :param bool strict: Whether to enable strict mode, see
                        :py:func:`Subtitle.to_srt` for more information
    :param str eol: The end of line string to use (default "\\n")
    :returns: A single SRT formatted string, with each input
              :py:class:`Subtitle` represented as an SRT block
    :param bool in_place: Whether to reindex subs in-place for performance
                          (version <=1.0.0 behaviour)
    :rtype: str
    )ry   rz   r   c              3   D   K   | ]  }|j                           yw))rJ   rE   N)rN   )r6   r~   rE   rJ   s     r   r9   zcompose.<locals>.<genexpr>  s     Ux8??&c?:Us    )r   r;   )rx   reindexry   rJ   rE   rz   s      `` r   composer     s1    @ $;
	 77U9UUUr   c                   "     e Zd ZdZ fdZ xZS )r   aR  
    Raised when part of an SRT block could not be parsed.

    :param int expected_start: The expected contiguous start index
    :param int actual_start: The actual non-contiguous start index
    :param str unmatched_content: The content between the expected start index
                                  and the actual start index
    c                 f    d|||fz  }t         t        |   |       || _        || _        || _        y )NzmExpected contiguous start of match or end of input at char %d, but started at char %d (unmatched content: %r))superr   r$   r   r   r   )r#   r   r   r   message	__class__s        r   r$   zSRTParseError.__init__  sG    =|->?@ 	
 	mT+G4,(!2r   )r=   rO   rP   rQ   r$   __classcell__)r   s   @r   r   r     s    
3 
3r   r   c                       e Zd ZdZy)rl   z;
    Raised when an SRT timestamp could not be parsed.
    Nr=   rO   rP   rQ   r5   r   r   rl   rl         r   rl   c                       e Zd ZdZy)rw   z3
    Raised when a subtitle should be skipped.
    Nr   r5   r   r   rw   rw     r   r   rw   )rs   FT)F)Trs   TNF)6rQ   
__future__r   	functoolsredatetimer   loggingio	getLoggerr=   rT   RGX_TIMESTAMP_MAGNITUDE_DELIMRGX_TIMESTAMP_FIELDRGX_TIMESTAMP_FIELD_OPTIONALr;   RGX_TIMESTAMPrH   RGX_TIMESTAMP_PARSEABLE	RGX_INDEXRGX_PROPRIETARYRGX_CONTENTRGX_POSSIBLE_CRLFcompilerj   rS   DOTALLr   r   r   rZ   r]   r\   r_   fileIOBaser   	NameErrortotal_orderingobjectr   rF   rI   rq   r   rv   r   r   r   	Exceptionr   r   rl   rw   r5   r   r   <module>r      s9   F '  	   	 g!
 !5  ( %**,?+@1+DE%$	 "..GG)..6I0IC0O/PST/TU)(	
	  !	 2::-.H%BJJ)" *0# *0 * II3	8 1 67EF&(HI   " 		"J
 R
v R
 R
j6<0P,9x
1MD`Q> MR%VP3I 3.* 9   ))Js   F   FF