
    g5                     l    d Z ddlm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dd	Z	 dd
Zy)z,Main video composition interface of MoviePy.    )reduceN)Image)CompositeAudioClip)	ColorClip	VideoClipc                   .    e Zd ZdZ	 ddZd ZddZd Zy)	CompositeVideoClipaX  
    A VideoClip made of other videoclips displayed together. This is the
    base class for most compositions.

    Parameters
    ----------

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

    clips
      A list of videoclips.

      Clips with a higher ``layer`` attribute will be displayed
      on top of other clips in a lower layer.
      If two or more clips share the same ``layer``,
      then the one appearing latest in ``clips`` will be displayed
      on top (i.e. it has the higher layer).

      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.

    Nc                 V   ||d   j                   }|r|d   j                  d}n|dnd}|s||r|rdnd}n|rdnd}|D cg c]  }t        |dd       s|j                   }}|rt	        |      nd | _        t        j                  |        || _         || _        || _        || _	        |r|d   | _
        |dd  | _        d| _        n+|| _        t        || j                  |	      | _
        d| _        t        | j                  d
       | _        | j                  D cg c]  }|j                   }	}d |	vrt	        |	      }
|
| _        |
| _        | j                  D cg c]  }|j                   |j                    }}|rt#        |      | _        |r | j                  D cg c]  }|j                  |j                  n|j%                         j                  j'                  |j(                        j+                  |j                        j-                  |j.                  d      j1                  |j2                         }}|r0| j                  j                  r| j                  j                  g|z   }t5        || j                   dd      | _        y y c c}w c c}w c c}w c c}w )Nr   FTg        )r   r   r   r   )r   r   r   fps   coloris_maskc                     | j                   S N)layer_index)clips    u/var/www/it7/html/youtubeDownloader/venv/lib/python3.12/site-packages/moviepy/video/compositing/CompositeVideoClip.py<lambda>z-CompositeVideoClip.__init__.<locals>.<lambda>b   s    9I9I     )key)
change_end)r   bg_color)sizemaskgetattrr   maxr   __init__r   clipsr   bg
created_bgr   sortedenddurationaudior   	with_maskwith_positionposwith_end
with_startstartwith_layer_indexr   r	   )selfr   r   r   
use_bgclipr   transparentr   fpssendsr$   v
audioclips	maskclipss                 r   r   zCompositeVideoClip.__init__8   s_    <8==D58==0K"*"2$K
  0")3|")3y%*ITgdE4.HII $3t9$4 	
  AhDGqrDJ#DODJDMM7KDG"DO DJJ,IJ
 &*ZZ0T00t4yH$DMDH (,zzI!QWW5HaggI
I+J7DJ  !JJ   $yy44>>;K;P;Ptxx($((#DJJ59!!$"2"23	4I  dggll!WW\\NY6	*499dSDI K J2 1 Js%   JJJJ!1J!$BJ&c                    | j                   rat        j                  | j                  d   | j                  d   ft              }| j                  |      D ]  }|j                  ||      } |S || j                  j                  z
  }| j                  j                  |      j                  d      }t        j                  |      }| j                  j                  r$|| j                  j                  j                  z
  }| j                  j                  j                  |      dz  j                  d      }t        j                  |      j                  d      }	|	j                  |j                  k7  rk|	j                  \  }
}|j                  \  }}|
|kD  s||kD  r|	j                  dd||f      }	n-t        j                   d||fd      }|j#                  |	d       |}	|j                  d      }|j%                  |	       |}| j                  |      D ]  }|j'                  ||      } t        j(                  |      }|j*                  d	   d
k(  r|ddddddf   S |S )z;The clips playing at time `t` are blitted over one another.r   r   )dtypeuint8   L)r   r   RGBA      N   )r   npzerosr   floatplaying_clipscompose_maskr    r+   	get_frameastyper   	fromarrayr   convertcropnewpasteputalpha
compose_onarrayshape)r-   tr   r   bg_tbg_framebg_imgbgm_tbg_maskbg_mask_img
mask_widthmask_height	img_width
img_heightnew_maskcurrent_imgframes                    r   frame_functionz!CompositeVideoClip.frame_function   s    <<88TYYq\499Q<8FD**1- 2((q12 K 477== 77$$T*11':*77<<***Eww||--e4s:BB7KG//'2::3?K 6;;.*5*:*:'
K(.%	:	)[:-E"-"2"2Aq)Z3P"QK$yyy*.EqIHNN;7"*K^^F+FOOK( &&q) 	:D//+q9K	: % ;;q>QArr?"r   c                 d    | j                   D cg c]  }|j                  |      s| c}S c c}w )ztReturns a list of the clips in the composite clips that are
        actually playing at the given time `t`.
        )r   
is_playing)r-   rN   r   s      r   rA   z CompositeVideoClip.playing_clips   s'     "&Btq/ABBBs   --c                     | j                   r-| j                  r!| j                  j                          d| _        t        | d      r/| j                  r"| j                  j                          d| _        yyy)z1Closes the instance, releasing all the resources.Nr%   )r!   r    closehasattrr%   )r-   s    r   r`   zCompositeVideoClip.close   sT    ??tww GGMMODG4!djjJJDJ '1!r   )NNFF)r   )__name__
__module____qualname____doc__r   r\   rA   r`    r   r   r	   r	      s'    )X JOHT1fC	r   r	   c           
      r   t        j                  |       } t        j                  | D cg c]  }|D cg c]  }|j                   c} c}}      }||dddddf   j                  d      }||dddddf   j                  d      }t        j                  dgt        |      z         }t        j                  dgt        |      z         }t        t        |dd |            D ]  \  }	\  }
}t        t        |dd |            D ]y  \  }\  }}| ||	f   }|j                  \  }}||k  s||k  r9t        |j                  d      g||f|      j                  |j                        }|j                  |
|f      | ||	f<   {  t        | j                         |d   |d   f|      S c c}w c c}}w )u  Given a matrix whose rows are clips, creates a CompositeVideoClip where
    all clips are placed side by side horizontally for each clip in each row
    and one row on top of the other for each row. So given next matrix of clips
    with same size:

    ```python
    clips_array([[clip1, clip2, clip3], [clip4, clip5, clip6]])
    ```

    the result will be a CompositeVideoClip with a layout displayed like:

    ```
    ┏━━━━━━━┳━━━━━━━┳━━━━━━━┓
    ┃       ┃       ┃       ┃
    ┃ clip1 ┃ clip2 ┃ clip3 ┃
    ┃       ┃       ┃       ┃
    ┣━━━━━━━╋━━━━━━━╋━━━━━━━┫
    ┃       ┃       ┃       ┃
    ┃ clip4 ┃ clip5 ┃ clip6 ┃
    ┃       ┃       ┃       ┃
    ┗━━━━━━━┻━━━━━━━┻━━━━━━━┛
    ```

    If some clips doesn't fulfill the space required by the rows or columns
    in which are placed, that space will be filled by the color defined in
    ``bg_color``.

    array
      Matrix of clips included in the returned composited video clip.

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

    cols_heights
      Heights of the different columns in pixels. If ``None``, is set automatically.

    bg_color
       Fill color for the masked and unfilled regions. Set to ``None`` for these
       regions to be transparent (processing will be slower).
    Nr   )axisr   center)r   r   )r>   rL   r   r   cumsumlist	enumeratezipr	   r'   with_durationr$   flatten)rL   rows_widthscols_heightsr   liner   sizes_arrayxsysjxchiyrwwhs                    r   clips_arrayr      s   R HHUOE((UKT484TYY8KLK !!Q'*..A.6"1a7+//Q/7 
A3l++	,B	A3k**	+BBsG\ :; 
5
7Ar#C3B$=> 		5JAw2A;D99DAqBAF)''12"bH-.  ,,aV4E!Q$K		5
5 emmoRVRV4DxXX1 9Ks   	F3
F.F3
.F3
c           
      v    |* dd D cg c]  }||g }}t        d |       d   gz    d}t        j                  dg D cg c]  }|j                   c}z          D cg c]  }|j                   }	}t        d |	D              }
t        d |	D              }t        j                  d|t        j                  t                    z  z         dxx   |z  cc<   |dk(  rk fd}d	 }t        ||
      }t         D cg c]  }|j                  du c}      r D cg c]
  } ||       }}t        |dd      |_         |_        n]|dk(  rMt        t               D cg c]%  \  }}|j!                  |      j#                  d      ' c}}|
|f||      }nt%        d      |_        dd |_        dd   d   c|_        |_        |_        t               D cg c]   \  }}|j.                  |j.                  |f" }}}|r2t1        |D cg c]  \  }}|j!                  |       c}}      |_         D cg c]  }t3        |dd      |j4                   }}|rt        |      |_        |S d|_        |S c c}w c c}w c c}w c c}w c c}w c c}}w c c}}w c c}}w c c}w )a  Concatenates several video clips.

    Returns a video clip made by clip by concatenating several video clips.
    (Concatenated means that they will be played one after another).

    There are two methods:

    - method="chain": will produce a clip that simply outputs
      the frames of the successive clips, without any correction if they are
      not of the same size of anything. If none of the clips have masks the
      resulting clip has no mask, else the mask is a concatenation of masks
      (using completely opaque for clips that don't have masks, obviously).
      If you have clips of different size and you want to write directly the
      result of the concatenation to a file, use the method "compose" instead.

    - method="compose", if the clips do not have the same resolution, the final
      resolution will be such that no clip has to be resized.
      As a consequence the final clip has the height of the highest clip and the
      width of the widest clip of the list. All the clips with smaller dimensions
      will appear centered. The border will be transparent if mask=True, else it
      will be of the color specified by ``bg_color``.

    The clip with the highest FPS will be the FPS of the result clip.

    Parameters
    ----------
    clips
      A list of video clips which must all have their ``duration``
      attributes set.
    method
      "chain" or "compose": see above.
    transition
      A clip that will be played between each two clips of the list.

    bg_color
      Only for method='compose'. Color of the background.
      Set to None for a transparent clip

    padding
      Only for method='compose'. Duration during two consecutive clips.
      Note that for negative padding, a clip will partly play at the same
      time as the clip it follows (negative padding is cool for clips who fade
      in on one another). A non-null padding automatically sets the method to
      `compose`.

    Nri   c                     | |z   S r   rf   )rx   r{   s     r   r   z(concatenate_videoclips.<locals>.<lambda>?  s
    AE r   r   c              3   &   K   | ]	  }|d      yw)r   Nrf   .0r   s     r   	<genexpr>z)concatenate_videoclips.<locals>.<genexpr>F       &DG&   c              3   &   K   | ]	  }|d      yw)r   Nrf   r   s     r   r   z)concatenate_videoclips.<locals>.<genexpr>G  r   r   chainc                     t        t              D cg c]  \  }}|| k  s| c}}      }|   j                  | |   z
        S c c}}w r   )r   rm   rC   )rN   rz   er   timingss      r   r\   z.concatenate_videoclips.<locals>.frame_functionN  sJ    9W#5@41aaQ@AA8%%a'!*n55 As
   A	
A	
c                     | j                   xs t        | j                  dd      }|j                  | j                  |_        |S )Nr   Tr   )r   r   r   r$   )r   r   s     r   get_maskz(concatenate_videoclips.<locals>.get_maskR  s7    99K	$))1d KD}}$ $Kr   )r   r\   T)methodr   composerj   )r   r   r   z[MoviePy Error: The 'method' argument of concatenate_videoclips must be 'chain' or 'compose'r   )r   r>   rk   r$   r   r   maximumarangelenr   anyr   concatenate_videoclipsr   r	   rn   r*   r'   	Exceptionr   start_timesr+   r#   r%   r   r   r   )r   r   
transitionr   r   paddingr2   clip_transition_pairsr   sizesr}   r~   r\   r   resultmasksrN   audio_tar0   r   s   `                   @r   r   r     s   b :?* EQ!Z E E)+@AU2YKO
ii?t}}??@G#()4TYY)E)&&&A&&&AjjGg		#g,0G&GGHGBK7K	6	 7>J%8$		%89056Xd^6E60wPTUFK FL	9	# "%UG!4T1 "00: Q
 B
 	

 FN "F0172;-FL&/6: (+5'':#D!djj>TQG  )w*Otq!1<<?*OP!&Q'$t*D*PDHHQDQ"TFJM )-FJMy !F @)* 96
& +PQsA   JJ
$JJ&J+*J$
+J* J*J0
J6J6)NNN)r   NNFr   )re   	functoolsr   numpyr>   PILr   moviepy.audio.AudioClipr   moviepy.video.VideoClipr   r   r	   r   r   rf   r   r   <module>r      s;    2    6 8x xvBYL STnr   