U
    a+d                     @   sB   d dl Zd dlmZ d dlmZmZ G dd deZdddZdS )	    N)CompositeAudioClip)	ColorClip	VideoClipc                   @   s,   e Zd ZdZdddZdddZd	d
 ZdS )CompositeVideoClipa   
    
    A VideoClip made of other videoclips displayed together. This is the
    base class for most compositions.
    
    Parameters
    ----------

    size
      The size (height x width) of the final clip.

    clips
      A list of videoclips. Each clip of the list will
      be displayed below the clips appearing after it in the list.
      For each clip:
       
      - The attribute ``pos`` determines where the clip is placed.
          See ``VideoClip.set_pos``
      - The mask of the clip determines which parts are visible.
        
      Finally, if all the clips in the list have their ``duration``
      attribute set, then the duration of the composite video clip
      is computed automatically

    bg_color
      Color for the unmasked and unfilled regions. Set to None for these
      regions to be transparent (will be slower).

    use_bgclip
      Set to True if the first clip in the list should be used as the
      'background' on which all other clips are blitted. That first clip must
      have the same size as the final clip. If it has no transparency, the final
      clip will have no mask. 
    
    The clip with the highest FPS will be the FPS of the composite clip.

    NFc                    sT  |d kr|d j }|r*|d jd kr*d}n|d k}|d krF|rBdnd}dd |D }|r`t|nd  _t  | _ | _| _| _|r|d  _	|dd   _d _
n| _t| jd _	d	 _
d
d  jD }d |krt|}	|	 _|	 _dd  jD }
|
rt|
 _|r>dd  jD }t| j d	dd _ fdd}| _d S )Nr   Fg        )r   r   r   c                 S   s   g | ]}t |d dr|jqS )fpsN)getattrr   .0c r   P/tmp/pip-unpacked-wheel-0yp4gafk/moviepy/video/compositing/CompositeVideoClip.py
<listcomp>?   s      z/CompositeVideoClip.__init__.<locals>.<listcomp>   )colorTc                 S   s   g | ]
}|j qS r   )endr   r   r   r   r   T   s     c                 S   s   g | ]}|j d k	r|j qS )N)audio)r	   vr   r   r   r   [   s     
 c                 S   sB   g | ]:}|j d k	r|j n| j |j|jj|jddqS )NF)Z
change_end)maskZadd_maskset_positionposZset_endr   Z	set_startstartr   r   r   r   r   a   s    )ismaskbg_colorc                    s,    j | } | D ]}||| }q|S )zM The clips playing at time `t` are blitted over one
                another. )bgZ	get_frameplaying_clipsZblit_on)tfr
   selfr   r   
make_framei   s    z/CompositeVideoClip.__init__.<locals>.make_frame)sizer   maxr   r   __init__r   clipsr   r   
created_bgr   durationr   r   r   r   r   )r   r#   r    r   Z
use_bgclipr   ZtransparentZfpssZendsr%   Z
audioclipsZ	maskclipsr   r   r   r   r"   0   sL    




	zCompositeVideoClip.__init__r   c                    s    fdd| j D S )zq Returns a list of the clips in the composite clips that are
            actually playing at the given time `t`. c                    s   g | ]}|  r|qS r   )Z
is_playingr   r   r   r   r   w   s     
 z4CompositeVideoClip.playing_clips.<locals>.<listcomp>)r#   )r   r   r   r&   r   r   t   s    z CompositeVideoClip.playing_clipsc                 C   s@   | j r| jr| j  d | _t| dr<| jr<| j  d | _d S )Nr   )r$   r   closehasattrr   r   r   r   r   r'   y   s    

zCompositeVideoClip.close)NNFF)r   )__name__
__module____qualname____doc__r"   r   r'   r   r   r   r   r      s   &  
D
r   c                 C   sX  t | } t dd | D }|dkrD|dddddf jdd}|dkrj|dddddf jdd}t dgt| }t dgt| }tt|dd |D ]\}\}}	tt|dd |D ]l\}
\}}| |
|f }|j\}}||	k s||k r t|	dg|	|f|d	
|j}|	||f| |
|f< qqt|  |d |d f|d	S )
ag  

    rows_widths
      widths of the different rows in pixels. If None, is set automatically.

    cols_widths
      widths of the different colums in pixels. If None, is set automatically.

    cols_widths
    
    bg_color
       Fill color for the masked and unfilled regions. Set to None for these
       regions to be transparent (will be slower).

    c                 S   s   g | ]}d d |D qS )c                 S   s   g | ]
}|j qS r   )r    r   r   r   r   r      s     z*clips_array.<locals>.<listcomp>.<listcomp>r   )r	   liner   r   r   r      s     zclips_array.<locals>.<listcomp>Nr   )Zaxisr   center)r    r   )nparrayr!   Zcumsumlist	enumeratezipr    r   r   Zset_durationr%   flatten)r1   Zrows_widthsZcols_widthsr   Zsizes_arrayZxxyyjxZcwiyrwZclipwhr   r   r   clips_array   s*    
""
r>   )NNN)	Znumpyr0   Zmoviepy.audio.AudioClipr   Zmoviepy.video.VideoClipr   r   r   r>   r   r   r   r   <module>   s   }  