
    kj                     N    d dl Z d dlmZmZ d dlZddlmZ  G d de          ZdS )    N)AnyOptional   )BaseVideoCapturec                   P     e Zd ZdZdeddf fdZdefdZd
dZdeddfd	Z	 xZ
S )FFmpegCapturea&  
    ``FFmpegCapture`` it's an utility class to capture runtime render
    and save it as video.

    Args:


    Example:

    .. code:: python

        import moderngl_window
        from moderngl_window.capture.ffmpeg import FFmpegCapture

        class CaptureTest(modenrgl_window.WindowConfig):

            def __init__(self, **kwargs):
                super().__init__(**kwargs)
                # do other initialization

                # define VideoCapture class
                self.cap = FFmpegCapture(source=self.wnd.fbo)

                # start recording
                self.cap.start_capture(
                    filename="video.mp4",
                    framerate=30
                )

            def render(self, time, frametime):
                # do other render stuff

                # call record function after
                self.cap.save()

            def close(self):
                # if realease func is not called during
                # runtime. make sure to do it on the closing event
                self.cap.release()

    kwargsreturnNc                 H     t                      j        di | d | _        d S )N )super__init___ffmpeg)selfr	   	__class__s     Y/home/agentuser/manim-venv/lib/python3.11/site-packages/moderngl_window/capture/ffmpeg.pyr   zFFmpegCapture.__init__4   s+    ""6""":>    c                 V   d}t          | j        t          j                  r| j        dk    rd}dddddd	d
dddd| j         d| j         d|d| j         ddddd| j        g}	 t          j
        |t          j        d          | _        n # t          $ r t          d           Y dS w xY wdS )z
        choose the right pixel format based on the number of components
        and start a ffmpeg pipe with a subprocess.
        rgb24   rgbaffmpegz-hide_bannerz	-loglevelerrorz-statsz-yz-frawvideoz-vcodecz-sxz-pix_fmtz-rz-i-z-vfvflipz-anr   )stdinbufsizez3ffmpeg command not found. Be sure to add it to PATHFT)
isinstance_sourcemodernglTexture_components_width_height
_framerate	_filename
subprocessPopenPIPEr   FileNotFoundErrorprint)r   pix_fmtcommands      r   _start_funczFFmpegCapture._start_func8   s    
  dlH$455 	$:Ja:O:OG {++T\++ N-
4	%+G:?TUVVVDLL  	 	 	GHHH55	 ts   "&B	 	B&%B&c                     | j         | j         j        dS | j         j                                         | j                                         }dS )z,
        Safely release the capture
        N)r   r   closewait)r   _s     r   _release_funczFFmpegCapture._release_funcf   sI     L dl&8&@F  """Lr   framec                 n    | j         | j         j        dS | j         j                            |           dS )z<
        write the frame data in to the ffmpeg pipe
        N)r   r   write)r   r6   s     r   _dump_framezFFmpegCapture._dump_frameo   s:     L dl&8&@F  '''''r   )r
   N)__name__
__module____qualname____doc__r   r   boolr0   r5   r9   __classcell__)r   s   @r   r   r   	   s        ( (T? ? ? ? ? ? ? ?,T , , , ,\       ( ( ( ( ( ( ( ( ( (r   r   )r)   typingr   r   r"   baser   r   r   r   r   <module>rB      s                         " " " " " "l( l( l( l( l($ l( l( l( l( l(r   