U
    s*^>                     @   s^   d Z ddlmZ ddlZddlZddlmZ ddlmZmZm	Z	m
Z
mZmZ G dd dZdS )z
This module implements the central object of MoviePy, the Clip, and
all the methods that are common to the two subclasses of Clip, VideoClip
and AudioClip.
    )copyN)tqdm)apply_to_audioapply_to_maskconvert_to_secondsoutplacerequires_durationuse_clip_fps_by_defaultc                   @   sV  e Zd ZdZdZdd Zdd Zedgdd	 Zd4ddZ	d5ddZ
dd Zeeedged6ddZeeedgedd Zeeedged7ddZedd Zedd Zedd Zedd  Zedgd!d" Zed#d$geed8d&d'Zeeed(d)gd*d+ Zeed9d,d-Zd.d/ Zd0d1 Zd2d3 Zd
S ):Clipa  

     Base class of all clips (VideoClips and AudioClips).


     Attributes
     -----------

     start:
       When the clip is included in a composition, time of the
       composition at which the clip starts playing (in seconds).

     end:
       When the clip is included in a composition, time of the
       composition at which the clip stops playing (in seconds).

     duration:
       Duration of the clip (in seconds). Some clips are infinite, in
       this case their duration will be ``None``.

     Z	TEMP_MPY_c                 C   s(   d| _ d | _d | _d| _d | _d | _d S )Nr   F)startenddurationmemoize
memoized_tZmemoize_frameself r   \/var/www/html/hiwar_tounsi_level1/my_env_project/lib/python3.8/site-packages/moviepy/Clip.py__init__0   s    zClip.__init__c                 C   s8   t | }t| drt | j|_t| dr4t | j|_|S )ac   Shallow copy of the clip. 
        
        Returns a shallow copy of the clip whose mask and audio will
        be shallow copies of the clip's mask and audio if they exist.

        This method is intensively used to produce new clips every time
        there is an outplace transformation of the clip (clip.resize,
        clip.subclip, etc.)
        audiomask)r   hasattrr   r   )r   newclipr   r   r   r   :   s    

z	Clip.copytc                 C   s@   | j r2|| jkr| jS | |}|| _|| _|S n
| |S dS )z
        Gets a numpy array representing the RGB picture of the clip at time t
        or (mono or stereo) value for a sound clip
        N)r   r   Zmemoized_frame
make_frame)r   r   framer   r   r   	get_frameM   s    

zClip.get_frameNTc                    s|   |dkrg }  fdd}|s0d|_d|_t|tr@|g}|D ]2}t||d}|dk	rD|j |d}t||| qD|S )a   General processing of a clip.

        Returns a new Clip whose frames are a transformation
        (through function ``fun``) of the frames of the current clip.

        Parameters
        -----------

        fun
          A function with signature (gf,t -> frame) where ``gf`` will
          represent the current clip's ``get_frame`` method,
          i.e. ``gf`` is a function (t->image). Parameter `t` is a time
          in seconds, `frame` is a picture (=Numpy array) which will be
          returned by the transformed clip (see examples below).

        apply_to
          Can be either ``'mask'``, or ``'audio'``, or
          ``['mask','audio']``.
          Specifies if the filter ``fl`` should also be applied to the
          audio or the mask of the clip, if any.

        keep_duration
          Set to True if the transformation does not change the
          ``duration`` of the clip.

        Examples
        --------

        In the following ``newclip`` a 100 pixels-high clip whose video
        content scrolls from the top to the bottom of the frames of
        ``clip``.

        >>> fl = lambda gf,t : gf(t)[int(t):int(t)+50, :]
        >>> newclip = clip.fl(fl, apply_to='mask')

        Nc                    s    j | S N)r   r   funr   r   r   <lambda>       zClip.fl.<locals>.<lambda>keep_duration)set_make_framer   r   
isinstancestrgetattrflsetattr)r   r    apply_tor$   r   attraZnew_ar   r   r   r)   _   s    %
zClip.flFc                    s$   |dkrg }| j  fdd||dS )a  
        Returns a Clip instance playing the content of the current clip
        but with a modified timeline, time ``t`` being replaced by another
        time `t_func(t)`.

        Parameters
        -----------

        t_func:
          A function ``t-> new_t``

        apply_to:
          Can be either 'mask', or 'audio', or ['mask','audio'].
          Specifies if the filter ``fl`` should also be applied to the
          audio or the mask of the clip, if any.

        keep_duration:
          ``False`` (default) if the transformation modifies the
          ``duration`` of the clip.

        Examples
        --------

        >>> # plays the clip (and its mask and sound) twice faster
        >>> newclip = clip.fl_time(lambda: 2*t, apply_to=['mask', 'audio'])
        >>>
        >>> # plays the clip starting at t=3, and backwards:
        >>> newclip = clip.fl_time(lambda: 3-t)

        Nc                    s   |  |S r   r   )Zgfr   t_funcr   r   r!      r"   zClip.fl_time.<locals>.<lambda>r#   )r)   )r   r/   r+   r$   r   r.   r   fl_time   s
    zClip.fl_timec                 O   s   || f||S )a[  

        Returns the result of ``func(self, *args, **kwargs)``.
        for instance

        >>> newclip = clip.fx(resize, 0.2, method='bilinear')

        is equivalent to

        >>> newclip = resize(clip, 0.2, method='bilinear')

        The motivation of fx is to keep the name of the effect near its
        parameters, when the effects are chained:

        >>> from moviepy.video.fx import volumex, resize, mirrorx
        >>> clip.fx( volumex, 0.5).fx( resize, 0.3).fx( mirrorx )
        >>> # Is equivalent, but clearer than
        >>> resize( volumex( mirrorx( clip ), 0.5), 0.3)

        r   )r   funcargskwargsr   r   r   fx   s    zClip.fxc                 C   s>   || _ | jdk	r"|r"|| j | _n| jdk	r:| j| j  | _dS )a  
        Returns a copy of the clip, with the ``start`` attribute set
        to ``t``, which can be expressed in seconds (15.35), in (min, sec),
        in (hour, min, sec), or as a string: '01:03:05.35'.


        If ``change_end=True`` and the clip has a ``duration`` attribute,
        the ``end`` atrribute of the clip will be updated to
        ``start+duration``.

        If ``change_end=False`` and the clip has a ``end`` attribute,
        the ``duration`` attribute of the clip will be updated to
        ``end-start``

        These changes are also applied to the ``audio`` and ``mask``
        clips of the current clip, if they exist.
        N)r   r   r   r   r   Z
change_endr   r   r   	set_start   s
    
zClip.set_startc                 C   sN   || _ | j dkrdS | jdkr<| jdk	rJtd|tj | _n| j | j | _dS )a0  
        Returns a copy of the clip, with the ``end`` attribute set to
        ``t``, which can be expressed in seconds (15.35), in (min, sec),
        in (hour, min, sec), or as a string: '01:03:05.35'.
        Also sets the duration of the mask and audio, if any,
        of the returned clip.
        Nr   )r   r   r   maxr   )r   r   r   r   r   set_end   s    
 

zClip.set_endc                 C   sF   || _ |r$|dkrdn| j| | _n| j dkr6td| j| | _dS )a  
        Returns a copy of the clip, with the  ``duration`` attribute
        set to ``t``, which can be expressed in seconds (15.35), in (min, sec),
        in (hour, min, sec), or as a string: '01:03:05.35'.
        Also sets the duration of the mask and audio, if any, of the
        returned clip.
        If change_end is False, the start attribute of the clip will
        be modified in function of the duration and the preset end
        of the clip.
        Nz1Cannot change clip start when newduration is None)r   r   r   	Exceptionr5   r   r   r   set_duration  s    
zClip.set_durationc                 C   s
   || _ dS )z|
        Sets a ``make_frame`` attribute for the clip. Useful for setting
        arbitrary/complicated videoclips.
        N)r   )r   r   r   r   r   r%   '  s    zClip.set_make_framec                 C   s
   || _ dS )zo Returns a copy of the clip with a new default fps for functions like
        write_videofile, iterframe, etc. N)fps)r   r;   r   r   r   set_fps/  s    zClip.set_fpsc                 C   s
   || _ dS )z< Says wheter the clip is a mask or not (ismask is a boolean)N)ismask)r   r=   r   r   r   
set_ismask6  s    zClip.set_ismaskc                 C   s
   || _ dS )z@ Sets wheter the clip should keep the last frame read in memory N)r   )r   r   r   r   r   set_memoize;  s    zClip.set_memoizec                 C   s   t |tjrn| |  }}| jdk	r6|| jkr6dS || jk rDdS d|| jk }| jdk	rj||| jk9 }|S || jko| jdkp|| jk S dS )a  

        If t is a time, returns true if t is between the start and
        the end of the clip. t can be expressed in seconds (15.35),
        in (min, sec), in (hour, min, sec), or as a string: '01:03:05.35'.
        If t is a numpy array, returns False if none of the t is in
        theclip, else returns a vector [b_1, b_2, b_3...] where b_i
        is true iff tti is in the clip.
        NF   )r&   npZndarrayminr7   r   r   )r   r   ZtminZtmaxresultr   r   r   
is_playing@  s    


zClip.is_playingt_startt_endr   c                    s    dk r| j    | j dk	r@ | j kr@td  d d| j   | j fddg d}|dkrp| j dk	rp| j }n>|dk	r|dk r| j dkrtd	t |f d
  n
| j | }|dk	r|  |_ |j|j  |_|S )ad  
        Returns a clip playing the content of the current clip
        between times ``t_start`` and ``t_end``, which can be expressed
        in seconds (15.35), in (min, sec), in (hour, min, sec), or as a
        string: '01:03:05.35'.
        If ``t_end`` is not provided, it is assumed to be the duration
        of the clip (potentially infinite).
        If ``t_end`` is a negative value, it is reset to
        ``clip.duration + t_end. ``. For instance: ::

            >>> # cut the last two seconds of the clip:
            >>> newclip = clip.subclip(0,-2)

        If ``t_end`` is provided or if the clip has a duration attribute,
        the duration of the returned clip is set automatically.

        The ``mask`` and ``audio`` of the resulting subclip will be
        subclips of ``mask`` and ``audio`` the original clip, if
        they exist.
        r   Nzt_start (%.02f) z"should be smaller than the clip's zduration (%.02f).c                    s   |   S r   r   r   rE   r   r   r!     r"   zClip.subclip.<locals>.<lambda>)r+   z,Error: subclip with negative times (here %s)z5 can only be extracted from clips with a ``duration``)r   
ValueErrorr0   printr'   r   r   )r   rE   rF   r   r   rG   r   subclipb  s*    



zClip.subcliptatbc                    s>    fdd}|  |}| jdk	r6|| j   S |S dS )a	  
        Returns a clip playing the content of the current clip but
        skips the extract between ``ta`` and ``tb``, which can be
        expressed in seconds (15.35), in (min, sec), in (hour, min, sec),
        or as a string: '01:03:05.35'.
        If the original clip has a ``duration`` attribute set,
        the duration of the returned clip  is automatically computed as
        `` duration - (tb - ta)``.

        The resulting clip's ``audio`` and ``mask`` will also be cutout
        if they exist.
        c                    s   | |  k    S r   r   r   rK   rL   r   r   r!     r"   zClip.cutout.<locals>.<lambda>N)r0   r   r:   )r   rK   rL   r)   r   r   rM   r   cutout  s
    

zClip.cutoutc                 c   sl   t |}|jtd| jd| dD ]@}| |}|dk	rP|j|krP||}|r`||fV  q&|V  q&dS )a   Iterates over all the frames of the clip.

        Returns each frame of the clip as a HxWxN np.array,
        where N=1 for mask clips and N=3 for RGB clips.

        This function is not really meant for video editing.
        It provides an easy way to do frame-by-frame treatment of
        a video, for fields like science, computer vision...

        The ``fps`` (frames per second) parameter is optional if the
        clip already has a ``fps`` attribute.

        Use dtype="uint8" when using the pictures to write video, images...

        Examples
        ---------

        >>> # prints the maximum of red that is contained
        >>> # on the first line of each frame of the clip.
        >>> from moviepy.editor import VideoFileClip
        >>> myclip = VideoFileClip('myvideo.mp4')
        >>> print ( [frame[0,:,0].max()
                     for frame in myclip.iter_frames()])
        r   g      ?r   N)	proglogZdefault_bar_loggerZiter_barrA   Zaranger   r   dtypeZastype)r   r;   Z
with_timesloggerrP   r   r   r   r   r   iter_frames  s    
 

zClip.iter_framesc                 C   s   dS )z= 
            Release any resources that are in use.
        Nr   r   r   r   r   close  s    z
Clip.closec                 C   s   | S r   r   r   r   r   r   	__enter__  s    zClip.__enter__c                 C   s   |    d S r   )rS   )r   exc_type	exc_value	tracebackr   r   r   __exit__  s    zClip.__exit__)NT)NF)T)T)r   N)NFNN)__name__
__module____qualname____doc__Z_TEMP_FILES_PREFIXr   r   r   r   r)   r0   r4   r   r   r   r6   r8   r:   r%   r<   r>   r?   rD   rJ   rN   r   r	   rR   rS   rT   rX   r   r   r   r   r
      sd   


:
%




!
9
  $r
   )r\   r   numpyrA   rO   r   Zmoviepy.decoratorsr   r   r   r   r   r	   r
   r   r   r   r   <module>   s    