
    kjO                     X    d dl Z d dlZd dlmZmZmZ d dlZd dlmZ  G d d          Z	dS )    N)AnyOptionalUnion)Timerc                       e Zd ZdZ	 ddeej        ej        f         deee	f         fdZ
deddfd	Zdefd
ZddZdeeef         fdZddZ	 ddee         deee	f         ddfdZddZddZdS )BaseVideoCapturea^  
    ``BaseVideoCapture`` is a base class to video capture

    Args:
        source (moderngl.Texture, moderngl.Framebuffer): the source of the capture
        framerate (int, float) : the framerate of the video, by thefault is 60 fps

    if the source is texture there are some requirements:
        - dtype = 'f1';
        - components >= 3.
    <   source	frameratec                     || _         || _        d| _        d| _        d| _        d | _        d | _        t                      | _        d| _	        t          | j         t          j                  r| j         j        | _	        d S d S )NFg         r   )_source
_framerate
_recording
_last_time	_filename_width_heightr   _timer_components
isinstancemodernglTexture
components)selfr
   r   s      W/home/agentuser/manim-venv/lib/python3.11/site-packages/moderngl_window/capture/base.py__init__zBaseVideoCapture.__init__   s|    
 #*/!$ %)&*gg !dlH$455 	7#|6D	7 	7    framereturnNc                      t          d          )zq
        custom function called during self.save()

        Args:
            frame: frame data in bytes
        override this functionNotImplementedError)r   r   s     r   _dump_framezBaseVideoCapture._dump_frame-   s     "":;;;r   c                      t          d          )z
        custom function called during self.start_capture()

        must return a True if this function complete without errors
        r"   r#   r   s    r   _start_funczBaseVideoCapture._start_func6   s     "":;;;r   c                      t          d          )z>
        custom function called during self.release()
        r"   r#   r'   s    r   _release_funczBaseVideoCapture._release_func>   s     "":;;;r   c                 2    | j         j        | j         j        fS )zJ
        Return a tuple of the width and the height of the source
        )r   widthheightr'   s    r   _get_whzBaseVideoCapture._get_whD   s     |!4<#666r   c                     t           j                            | j                  rt          j        | j                   dS dS )z,Remove the filename of the video is it existN)ospathexistsr   remover'   s    r   _remove_filezBaseVideoCapture._remove_fileJ   s<    7>>$.)) 	&Idn%%%%%	& 	&r   filenamec                    | j         rt          d           dS t          | j        t          j                  r=| j        j        dk    rt          d           dS | j        dk     rt          d           dS | j        t          d           dS |s%t          j        	                                }d|d	d
}|| _
        || _        |                                 \  | _        | _        |                                 s%|                                  t          d           dS | j                                         | j        j        | _        d| _         dS )a  
        Start the capturing process

        Args:
            filename (str): name of the output file
            framerate (int, float): framerate of the video

        if filename is not specified it will be generated based
        on the datetime.
        zCapturing is already startedNf1z0source type: moderngl.Texture must be type `f1`    z=source type: moderngl.Texture must have at least 3 componentsz-No source defined, there is nothing to recordvideo_z%Y%m%d_%H%M%Sz.mp4zCapturing failedT)r   printr   r   r   r   dtyper   datetimenowr   r   r.   r   r   r(   releaser   starttimer   )r   r5   r   r=   s       r   start_capturezBaseVideoCapture.start_captureO   sR    ? 	0111F dlH$455 	|!T))HIII!##UVVV<ABBBF 	8#''))C77777H!#$(LLNN!T\
 !! 	LLNNN$%%%F+*r   c                    | j         sdS | j        dS d| j        z  }| j        j        | j        z
  |k    r| j        j        | _        t          | j        t          j                  r2| j        	                    d          }| 
                    |           dS | j        	                                }| 
                    |           dS dS )zE
        Save function to call at the end of render function
        Ng      ?r8   )r   )r   r   r   r   r@   r   r   r   Framebufferreadr%   )r   dtr   s      r   savezBaseVideoCapture.save   s      	F<F4?";do-22"k.DO$,(<== ())Q)77  ''''' ))++  ''''' 32r   c                     | j         rD|                                  | j                                         t	          d| j                    d| _         dS )z,
        Stop the recording process
        z Video file succesfully saved as N)r   r*   r   stopr:   r   r'   s    r   r>   zBaseVideoCapture.release   sY     ? 	G   KET^EEFFFr   )r	   )r    N)Nr	   )__name__
__module____qualname____doc__r   r   r   rC   intfloatr   r   r%   boolr(   r*   tupler.   r4   r   strrA   rF   r>    r   r   r   r   
   s]       
 
 (*7 7h&(<<=7 e$7 7 7 7,< < < < < <<T < < < << < < <7sCx 7 7 7 7& & & & NP1 1 19>sEz9J1	1 1 1 1f( ( ( (2	 	 	 	 	 	r   r   )
r<   r0   typingr   r   r   r   moderngl_window.timers.clockr   r   rR   r   r   <module>rU      s     				 ' ' ' ' ' ' ' ' ' '  . . . . . .Z Z Z Z Z Z Z Z Z Zr   