
    lj)                    
   d Z ddlmZ dgZddlZddlZddlZddlmZ ddl	m
Z
 ddlmZ ddlmZmZ dd	lmZ dd
lmZmZ ddlZddlZddlZddlmZ  ej                    5   ej        dde           ddlmZ ddd           n# 1 swxY w Y   ddl m!Z! ddl"m#Z#m$Z$ ddl%m&Z& ddl'm(Z( ddl)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0 ddl1m2Z2 ddl3m4Z4m5Z5 er ddl6m7Z7 ddl8m9Z9 ddl:m;Z; ddl<m=Z= ddl>m?Z?m@Z@ d,d"ZAd-d*ZB G d+ d          ZCdS ).z(The interface between scenes and ffmpeg.    )annotationsSceneFileWriterN)Fraction)Path)Queue)NamedTemporaryFile_TemporaryFileWrapper)Thread)TYPE_CHECKINGAny)Imageignorez.*ffmpeg or avconv.*)messagecategory)AudioSegment)__version__   )configlogger)set_file_logger)RendererType)add_extension_if_not_presentadd_version_before_extensionguarantee_existenceis_gif_formatis_png_formatmodify_atimewrite_to_movie)get_full_sound_file_path   )DefaultSectionTypeSection)OutputContainer)Stream)CairoRenderer)OpenGLRenderer)
PixelArrayStrPathfpsfloatreturnr   c                L   d}d}t          | t                    r| d}}nwt          | t          |           z
            |k     rt          |           d}}nBd}t          | |z  dz            dz  }t          | ||z  z
            |k    rt	          d          t          ||          S )Ng-C6?g{Gz?r    i    zinvalid frame rate)
isinstanceintabsround
ValueErrorr   )r)   epsilon1epsilon2numdenoms        X/home/agentuser/manim-venv/lib/python3.11/site-packages/manim/scene/scene_file_writer.pyto_av_frame_rater8   ;   s    HH#s 3Qe	S5::			)	)c

AeC%K$&''$.sS5[ !!X--1222C    
input_pathr   output_path#Path | _TemporaryFileWrapper[bytes]
codec_namestrNonec                   t          j        |           5 }t          j        |d          5 }|j        j        d         }|                    |          }|                    |          D ]/}|                    |          D ]}|                    |           0|                                D ]}|                    |           	 d d d            n# 1 swxY w Y   d d d            d S # 1 swxY w Y   d S )Nwr   )avopenstreamsaudio
add_streamdecodeencodemux)	r:   r;   r=   input_audiooutput_audioinput_audio_streamoutput_audio_streamframepackets	            r7   convert_audiorP   L   s    	
%*
S!!%%1(06q9*55jAA ''(:;; 	) 	)E-44U;; ) )  (((() *0022 	% 	%FV$$$$	%% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %s5   C)BCC)C	C)C	C))C-0C-c                     e Zd ZdZdZdLdZdMdZdNdZdOdZdPdZ	dQdZ
dNdZdNdZ	 	 dRdSdZ	 	 dRdTd#Z	 dUdVd'ZdWdXd(ZdNd)ZdYd.Z	 dZd[d2Zd\d8Zd]d9ZdNd:Zd^d_d;ZdNd<Zd`d>Z	 	 dadbdEZdNdFZdNdGZdNdHZdNdIZdNdJZdcdKZ dS )dr   a  SceneFileWriter is the object that actually writes the animations
    played, into video files, using FFMPEG.
    This is mostly for Manim's internal use. You will rarely, if ever,
    have to use the methods for this class, unless tinkering with the very
    fabric of Manim's reality.

    Attributes
    ----------
        sections : list of :class:`.Section`
            used to segment scene

        sections_output_dir : :class:`pathlib.Path`
            where are section videos stored

        output_name : str
            name of movie without extension and basis for section video names

    Some useful attributes are:
        "write_to_movie" (bool=False)
            Whether or not to write the animations into a video file.
        "movie_file_extension" (str=".mp4")
            The file-type extension of the outputted video.
        "partial_movie_files"
            List of all the partial-movie files.

    FrendererCairoRenderer | OpenGLRenderer
scene_namer>   kwargsr   r+   r?   c                    || _         |                     |           |                                  d| _        g | _        g | _        g | _        |                     dt          j	        d           d S )Nr   autocreatedF)nametype_skip_animations)
rR   init_output_directories
init_audioframe_countpartial_movie_filessubcaptionssectionsnext_sectionr!   NORMAL)selfrR   rT   rU   s       r7   __init__zSceneFileWriter.__init__{   s     !$$Z00057 /1') 	&8&?QV 	 	
 	
 	
 	
 	
r9   c                   t           d         rdS t           d         rt          j        d          j        nd}t          j        rt          |          | _        nHt           d         r't           d         st          j        d          | _        nt          |          | _        t           d         rAt          t          j        d||	                    }|t          | j        d
          z  | _	        t                      r]t          t          j        d||	                    }|t          | j        t           d                   z  | _        t          d          | _        t           j        r)t          t          j        d||	                    | _        t                      rOt          | j        d          | _        t           d         st!          | j                  | _        || j        z  | _        t          t          j        d||                    | _        t           d         r7t          t          j        d                    }t%          |||           dS dS dS )zInitialise output directories.

        Notes
        -----
        The directories are read from ``config``, for example
        ``config['media_dir']``.  If the target directories don't already
        exist, they will be created.

        dry_runN
input_file output_file	write_all	media_dir
images_dir)module_namerT   z.png	video_dirmovie_file_extensionsections_dirz.gifpartial_movie_dir)rT   rm   log_to_filelog_dir)rT   rm   rs   )r   get_dirstemr   force_output_as_scene_namer   output_namer   r   image_file_pathr   movie_file_pathsections_output_dirsave_sectionsr   gif_file_pathr   partial_movie_directoryr   )rc   rT   rm   	image_dir	movie_dirrs   s         r7   r[   z'SceneFileWriter.init_output_directories   s    ) 	F;A,;OWfn\2277UW5 	0#J//DM" 	06++> 	0%~m<<D#J//D+ 	+ kj   I
 $-/K &0 0 $D   +	+[Z   I
 $-/K &)?"@0 0 $D 
 (,BxxD$# +>N&KJ  , ,(  
D%A$f& &" m, )E** *D& &/1C%C"+>') +  , ,D( m$ -fnY.G.GHH){G     S+	 +	N r9   c                    t          | j                  r:| j        d                                         r| j                                         dS dS dS )z&Delete current section if it is empty.N)lenr`   is_emptypoprc   s    r7   finish_last_sectionz#SceneFileWriter.finish_last_section   s\    t} 	 $-"3"<"<">"> 	 M	  	  	  	 r9   rX   rY   rZ   boolc                2   |                                   d}t          j        sJt                      r<t          j        r0|s.| j         dt          | j                  dd| t          j         }| j        	                    t          ||||                     dS )zCreate segmentation cut here.N_04)r   r   rf   r   r{   rw   r   r`   ro   appendr"   )rc   rX   rY   rZ   section_videos        r7   ra   zSceneFileWriter.next_section   s      """ %) 	n  	n $	n $		n  $/mm#dm2D2Dmmm$mPVPkmmM	 	
 	
 	
 	
 	
r9   hash_animation
str | Nonec                   t          | d          rt                      sdS |A| j                            d           | j        d         j                            d           dS t          | j        | t          d          z            }| j                            |           | j        d         j                            |           dS )a^  Adds a new partial movie file path to ``scene.partial_movie_files``
        and current section from a hash.

        This method will compute the path from the hash. In addition to that it
        adds the new animation to the current section.

        Parameters
        ----------
        hash_animation
            Hash of the animation.
        r}   Nr   ro   )hasattrr   r^   r   r`   r>   r}   r   )rc   r   new_partial_movie_files      r7   add_partial_movie_filez&SceneFileWriter.add_partial_movie_file   s     t677 	~?O?O 	F !$++D111M"188>>>>>%(,#EV,B%CEEF& &" $++,BCCCM"1889OPPPPPr9   c                D    t           d         }t           d         }| d| S )a  Get the name of the resolution directory directly containing
        the video file.

        This method gets the name of the directory that immediately contains the
        video file. This name is ``<height_in_pixels_of_video>p<frame_rate>``.
        For example, if you are rendering an 854x480 px animation at 15fps,
        the name of the directory that immediately contains the video,  file
        will be ``480p15``.

        The file structure should look something like::

            MEDIA_DIR
                |--Tex
                |--texts
                |--videos
                    |--<name_of_file_containing_scene>
                        |--<height_in_pixels_of_video>p<frame_rate>
                            |--partial_movie_files
                            |--<scene_name>.mp4
                            |--<scene_name>.srt

        Returns
        -------
        :class:`str`
            The name of the directory.
        pixel_height
frame_ratep)r   )rc   r   r   s      r7   get_resolution_directoryz(SceneFileWriter.get_resolution_directory  s,    6 n-L)
-----r9   c                    d| _         dS )z/Preps the writer for adding audio to the movie.FN)includes_soundr   s    r7   r\   zSceneFileWriter.init_audio5  s    #r9   c                6    t          j                    | _        dS )z(Creates an empty, silent, Audio Segment.N)r   silentaudio_segmentr   s    r7   create_audio_segmentz$SceneFileWriter.create_audio_segment9  s    )022r9   Nnew_segmentr   timefloat | Nonegain_to_backgroundc           
        | j         sd| _         |                                  | j        }|j        }||}|dk     rt	          d          ||j        z   }||z
  }|dk    rK|                    t          j        t          t          j
        |dz                                d          }|                    |t          d|z            |          | _        dS )am  This method adds an audio segment from an AudioSegment type object
        and suitable parameters.

        Parameters
        ----------
        new_segment
            The audio segment to add

        time
            the timestamp at which the sound should be added.

        gain_to_background
            The gain of the segment from the background.
        TNr   zAdding sound at timestamp < 0r-   )	crossfade)positiongain_during_overlay)r   r   r   duration_secondsr2   r   r   r   r/   npceiloverlay)rc   r   r   r   segmentcurr_endnew_enddiffs           r7   add_audio_segmentz!SceneFileWriter.add_audio_segment=  s    ( " 	("&D%%'''$+<D!88<===55!!88nn#Ct(<(<$=$=>> %  G %__%% 2 - 
 
r9   
sound_filer(   gainc                   t          |          }|j        dvrt          dd          5 }t          ||d           t	          j        |j                  }t          j        d| d           ddd           n# 1 swxY w Y   t          |j                  
                                 nt	          j        |          }|r|                    |          } | j        ||fi | dS )	a  This method adds an audio segment from a sound file.

        Parameters
        ----------
        sound_file
            The path to the sound file.

        time
            The timestamp at which the audio should be added.

        gain
            The gain of the given audio segment.

        **kwargs
            This method uses add_audio_segment, so any keyword arguments
            used there can be referenced here.

        ).wavz.rawr   F)suffixdelete	pcm_s16lezAutomatically converted z to .wavN)r   r   r   rP   r   	from_filerX   r   infor   unlink
apply_gainr   )rc   r   r   r   rU   	file_pathwav_file_pathr   s           r7   	add_soundzSceneFileWriter.add_soundh  sJ   2 -Z88	 #333 $6%@@@ LMiDDD*4]5GHHJyJJJKKKL L L L L L L L L L L L L L L #$$++----&0;;K 	7%0066K{D;;F;;;;;s   AA99A= A=allow_writer   StrPath | Nonec                Z    t                      r|r|                     |           dS dS dS )zUsed internally by manim to stream the animation to FFMPEG for
        displaying or writing to a file.

        Parameters
        ----------
        allow_write
            Whether or not to write to a video file.
        )r   N)r   open_partial_movie_stream)rc   r   r   s      r7   begin_animationzSceneFileWriter.begin_animation  sP      	@ 	@**Y*?????	@ 	@ 	@ 	@r9   c                V    t                      r|r|                                  dS dS dS )zInternally used by Manim to stop streaming to FFMPEG gracefully.

        Parameters
        ----------
        allow_write
            Whether or not to write to a video file.
        N)r   close_partial_movie_stream)rc   r   s     r7   end_animationzSceneFileWriter.end_animation  sE      	. 	.++-----	. 	. 	. 	.r9   c                r    	 | j                                         \  }}|dS |                     ||           7)zHFor internal use only: blocks until new frame is available on the queue.TN)queuegetencode_and_write_frame)rc   
num_frames
frame_datas      r7   listen_and_writez SceneFileWriter.listen_and_write  sE    	@%)Z^^%5%5"J
!''
J???	@r9   rN   r'   r   r/   c                    t          |          D ]Z}t          j                            |d          }| j                            |          D ]}| j                            |           [dS )zpFor internal use only: takes a given frame in ``np.ndarray`` format and
        writes it to the stream
        rgba)formatN)rangerB   
VideoFramefrom_ndarrayvideo_streamrH   video_containerrI   )rc   rN   r   r   av_framerO   s         r7   r   z&SceneFileWriter.encode_and_write_frame  s     z"" 		1 		1A }11%1GGH+228<< 1 1$((00001		1 		1r9   r    frame_or_rendererPixelArray | OpenGLRendererc                   t                      rkt          |t          j                  r|}n0t          j        t          j        k    r|                                n|}||f}| j	        
                    |           t                      rt          d         st          |t          j                  rt          j        |          }nBt          j        t          j        k    r|                                nt          j        |          }| j        j        | j        j        z  }| j        j        }|                     |||t          d                    dS dS dS )zUsed internally by Manim to write a frame to the FFMPEG input buffer.

        Parameters
        ----------
        frame_or_renderer
            Pixel array of the frame.
        num_frames
            The number of times to write frame.
        rf   zero_padN)r   r.   r   ndarrayr   rR   r   OPENGL	get_framer   putr   r   	fromarray	get_imagerx   parentru   r   output_image)rc   r   r   rN   msgimage
target_dir	extensions           r7   write_framezSceneFileWriter.write_frame  s^     	 +RZ88 ) ,*=== &//111*  u%CJNN3?? 	6)#4 	+RZ88 (9:: ,*=== &//111):;; 
 -4t7K7PPJ,3Iz"	    	 	 	 	r9   r   Image.Imager   extr   c                    |rA|                     | t          | j                                      |           |            n |                     | | j         |            | xj        dz  c_        d S )Nr    )saver>   r]   zfill)rc   r   r   r   r   s        r7   r   zSceneFileWriter.output_image  s      	?JJ*Rc$*:&;&;&A&A(&K&KRSRRSSSSJJ*=d&6===>>>Ar9   c                    t           d         rdS t           d         st          | j                  | _        |                    | j                   |                     | j                   dS )zThis method saves the image passed to it in the default image directory.

        Parameters
        ----------
        image
            The pixel array of the image to save.
        rf   Nri   )r   r   rx   r   print_file_ready_message)rc   r   s     r7   
save_imagezSceneFileWriter.save_image  si     ) 	Fm$ 	V#?@T#U#UD 

4'(((%%d&:;;;;;r9   c                   t                      rk|                                  t          j        r|                                  t          d         r|                                  nq|                                  n\t                      rNt          d         sA| j        j	        | j        j
        z  }t          j        d| j        t          |                     | j        r|                                  dS dS )zFinishes writing to the FFMPEG buffer or writing images to output directory.
        Combines the partial movie files into the whole scene.
        If save_last_frame is True, saves the last frame in the default image directory.
        flush_cacherf   z
%i images ready at %s
N)r   combine_to_movier   r{   combine_to_section_videosflush_cache_directoryclean_cacher   rx   r   ru   r   r   r]   r>   r_   write_subcaption_file)rc   r   s     r7   finishzSceneFileWriter.finish	  s    
  
	X!!#### 1..000m$ #**,,,,  """"__ 	XVI%6 	X-4t7K7PPJK3T5Es:WWW 	)&&(((((	) 	)r9   c                N   || j         | j        j                 }|| _        t	          t
          j                  }d}d}ddd}t
          j        dk    rd}d|d	<   t
          j        rd
}nt
          j        rd}d}t          j
        |d          }|                    |||          }||_        t
          j        |_        t
          j        |_        || _        || _        t'                      | _        t+          | j        d          | _        | j                                         dS )zOpen a container holding a video stream.

        This is used internally by Manim initialize the container holding
        the video stream of a partial movie file.
        Nlibx264yuv420p123)ancrf.webmz
libvpx-vp9z-auto-alt-refyuva420pqtrleargbrA   mode)rateoptions )targetargs)r^   rR   	num_playspartial_movie_file_pathr8   r   r   ro   transparentrB   rC   rF   pix_fmtpixel_widthwidthr   heightr   r   r   r   r
   r   writer_threadstart)rc   r   r)   partial_movie_file_codecpartial_movie_file_pix_fmt
av_optionsr   streams           r7   r   z)SceneFileWriter.open_partial_movie_stream  s=    01HII'0$v011#, %."
 


 &'11'3$*-J'! 8-7* 	0'.$)/&')#666 ++$ , 
 

 4)+0?$*;@77
#4+@rJJJ  """""r9   c                f   | j                             d           | j                                         | j                                        D ]}| j                            |           | j                                         t          j
        d| j        j         ddd| j         di           dS )zClose the currently opened video container.

        Used internally by Manim to first flush the remaining packages
        in the video stream holding a partial file, and then close
        the corresponding container.
        )r   Nz
Animation z) : Partial movie file written in %(path)spath'N)r   r   r  joinr   rH   r   rI   closer   r   rR   r  r	  )rc   rO   s     r7   r   z*SceneFileWriter.close_partial_movie_streamJ  s     	
z"""!!!'..00 	- 	-F $$V,,,,""$$$[0[[[858889	
 	
 	
 	
 	
r9   hash_invocationc                    t          | d          rt                      sdS | j        | t          d          z  }|                                S )a9  Will check if a file named with `hash_invocation` exists.

        Parameters
        ----------
        hash_invocation
            The hash corresponding to an invocation to either `scene.play` or `scene.wait`.

        Returns
        -------
        :class:`bool`
            Whether the file exists.
        r}   Fro   )r   r   r}   r   exists)rc   r  r  s      r7   is_already_cachedz!SceneFileWriter.is_already_cached^  s^     t677 	~?O?O 	5( B&)?"@BBC 	 {{}}r9   input_files	list[str]ri   r   
create_gifr   c                   | j         dz  }t          j        dt          |           dd|d d         i           |                    dd          5 }|                    d	           |D ]<}t          |                                          }|                    d
| d           =	 d d d            n# 1 swxY w Y   ddi}|sd|d<   t          j        t          |          |d          }	|	j
        j        d         }
t          j        t          |          d          }dt           |j        d<   |rs	 |                    d          }d|_        t           j        rd|_        t           j        |_        t           j        |_        t-          t           j                  |_        t          j                                        }|                    |
          }|                    d          }|                    dd          }|                    dd          }|                    d           }|                    |           |                    |dd           |                    |d!d           |                    |dd!           |                    |           |                                 |	                    d"          D ]}|                     |           |                     d            d}	 	 |!                                }|j"        j#        |j"        j#        |_#        ||_$        |d!z  }|%                    |&                    |                     n# t          j'        j(        $ r Y nw xY w|&                                D ]}|%                    |           n||)                    |
          }t           j        rt           j*        d$k    rd%|_        |	+                    |
          D ]-}|j,        
d |_,        ||_-        |%                    |           .|	.                                 |.                                 d S )&Npartial_movie_file_list.txtz Partial movie files to combine (z files): %(p)sr      rA   utf-8encodingz*# This file is used internally by FFMPEG.
zfile 'file:z'
safe0r   r   concat)r  r   r   r  zRendered with Manim Community vcommentgif)r=   rgb8pal8templatesplit
palettegenzstats_mode=diff
paletteusez.dither=bayer:bayer_scale=5:diff_mode=rectangle
buffersinkr    )videoTr   r   )/r}   r   debugr   rC   writer   as_posixrB   r>   rD   r4  r   metadatarF   r  r   r
  r  r  r   r  r8   r   r  filterGraph
add_bufferaddlink_to	configurerG   pushpullcodec_context	time_baseptsrI   rH   errorEOFErroradd_stream_from_templatero   demuxdtsr  r  )rc   r  ri   r   r   	file_listfppf_pathr  partial_movies_inputpartial_movies_streamoutput_containeroutput_streamgraphinput_bufferr0  r1  r2  output_sinkrN   frames_writtenrO   s                         r7   combine_fileszSceneFileWriter.combine_filess  s    03PP	Os;/?/?OOO+bqb/"	
 	
 	
 ^^C'^22 	5bHHBCCC& 5 5w--00223w33344445	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 C

  	#"Jt!w	NNJx 
  
  
 !5 < B1 E73{#3#3#>>>;k;; 	!),  D	-
 -77  8  M %+M!! /(.%"("4M#)#6M !1&2C!D!DMIOO%%E ++5J+KKLIIg&&E<1BCCJN J  ))L11K  '''MM*a+++MM*a+++z1a000{+++OO-4414== " "

5!!!!JJtN	!JJLLE$2<H*7*E*O .EI"a'N$(()=)=e)D)DEEEEx(   E	 (..00 - - $$V,,,,- -EE. F  M ! 3f&AW&L&L(2%.445JKK 
- 
-:%!
 !. $$V,,,,""$$$     s&   AB44B8;B8=A%M# #M:9M:c                   d | j         D             }| j        }t                      r| j        }t	          |          dk    rt          j        d           dS t          j        d           |                     ||t                      | j                   | j        rt          j
        dk    r|                    d          }|                     t          j        d                     | j                            |dd	
           t          j        dk    r)|                    d          }t%          ||d           |}n8t          j        dk    r(|                    d          }t%          ||d           |}|                    |j         d|j                   }ddt,           d}t/          j        |          5 }t/          j        |          5 }	|j        j        d         }
|	j        j        d         }t/          j        t9          |          d|          }|                    |
          }|                    |          }|                    |
          D ]&}|j        
||_         |!                    |           '|	                    |          D ]&}|j        
||_         |!                    |           '|"                                 ddd           n# 1 swxY w Y   ddd           n# 1 swxY w Y   tG          j$        t9          |          t9          |                     |%                                 | &                    t9          |                     tO                      r|D ]}tQ          |           dS dS )zUsed internally by Manim to combine the separate
        partial movie files that make up a Scene into a single
        video file for that Scene.
        c                    g | ]}||S Nr  ).0els     r7   
<listcomp>z4SceneFileWriter.combine_to_movie.<locals>.<listcomp>  s    WWWbrr9   r   z*No animations are contained in this scene.NzCombining to Movie file.r+  r   wav312k)r   bitrater   z.ogg	libvorbisz.mp4z.aacaac_tempr   z'comment=Rendered with Manim Community v)shortestr8  rA   )r  r  r.  ))r^   ry   r   r|   r   r   r   rT  r   r   r   with_suffixr   r   r   r   exportro   rP   	with_nameru   r   r   rB   rC   rD   r4  rE   r>   rF  rG  rH  r  rI   r  shutilmover   r   r   r   )rc   r^   ry   sound_file_pathogg_sound_file_pathaac_sound_file_pathtemp_file_pathr  video_inputaudio_inputr   audio_streamrN  output_video_streamrM   rO   r   s                    r7   r   z SceneFileWriter.combine_to_movie  sa   
 XWD,DWWW .?? 	1"0O"##q((KDEEEF.///OO		
 	
 	
  G	%6=E#9#9-99&AAO""<#6q#9#9:::%% &    *g55&5&A&A&&I&I#o/BKPPP"5,66 '6&A&A&&I&I#o/BEJJJ"5,66"'FFo.DFF N  SkSS J (("),7(("),7*28;*28;#%7''c:$ $ $  '7&O&O) 'P ' '# '7&O&O) 'P ' '# *//== 1 1Fz)  %8FM$((0000)//== 1 1Fz)  %8FM$((0000 &&(((E") ") ") ") ") ") ") ") ") ") ") ") ") ") ") ") ") ") ") ") ") ") ") ") ") ") ") ") ") ")H KN++S-A-ABBB""$$$%%c/&:&:;;; 	(0 ( (	Y''''	( 	(( (s7   <K5DKK5K"	"K5%K"	&K55K9<K9c                   |                                   g }| j        D ]}|j        t          j        d|j         d           |                     |                                | j        |j        z             |	                    |
                    | j                             | j        | j         dz                      d          5 }t          j        ||d           ddd           dS # 1 swxY w Y   dS )z1Concatenate partial movie files for each section.Nz%Combining partial files for section 'r  z.jsonrA      )indent)r   r`   r4  r   r   rX   rT  get_clean_partial_movie_filesrz   r   get_dictrw   rC   jsondump)rc   sections_indexsectionfiles       r7   r   z)SceneFileWriter.combine_to_section_videosI  sO     """/1} 	R 	RG}(SGLSSSTTT""99;;,w}<   %%g&6&6t7O&P&PQQQ&D,<)C)C)CCII#NN 	6RVInd15555	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6s   C11C58C5c                ~     fd j                                         D             }t          |          t          d         k    rxt          |          t          d         z
  }t	          |d           d|         }|D ]}|                                 t          j        dt          d          d| d           dS dS )	z@Will clean the cache by removing the oldest partial_movie_files.c                0    g | ]}|d k    j         |z  S r"  r}   rX  	file_namerc   s     r7   rZ  z/SceneFileWriter.clean_cache.<locals>.<listcomp>[  s7     !
 !
 !
999 )I5999r9   max_files_cachedc                4    |                                  j        S rW  )statst_atime)r  s    r7   <lambda>z-SceneFileWriter.clean_cache.<locals>.<lambda>f  s    !5 r9   )keyNz'The partial movie directory is full (> z* files). Therefore, manim has removed the zV oldest file(s). You can change this behaviour by changing max_files_cached in config.)r}   iterdirr   r   sortedr   r   r   )rc   cached_partial_moviesnumber_files_to_deleteoldest_files_to_deletefile_to_deletes   `    r7   r   zSceneFileWriter.clean_cacheY  s#   !
 !
 !
 !
!9AACC!
 !
 !

 $%%/A(BBB)**V4F-GG # &,%55& & & &%%&'" #9 ( (%%''''KY&AS:T Y Y  AW Y Y Y     CBr9   c                      fd j                                         D             }|D ]}|                                 t          j        dt          |           dd j         i           dS )z)Delete all the cached partial movie filesc                0    g | ]}|d k    j         |z  S r{  r|  r}  s     r7   rZ  z9SceneFileWriter.flush_cache_directory.<locals>.<listcomp>q  s7     !
 !
 !
999 (94999r9   zCache flushed. z  file(s) deleted in %(par_dir)s.par_dirN)r}   r  r   r   r   r   )rc   r  fs   `  r7   r   z%SceneFileWriter.flush_cache_directoryo  s    !
 !
 !
 !
!9AACC!
 !
 !

 ' 	 	AHHJJJJZc"788ZZZ45	
 	
 	
 	
 	
r9   c                   t           j        dS t          t           j                                      d          }|                    t          j        | j                  d           t          j	        d|            dS )zWrites the subcaption file.Nz.srtr$  r%  z$Subcaption file has been written as )
r   ri   r   rb  
write_textsrtcomposer_   r   r   )rc   subcaption_files     r7   r   z%SceneFileWriter.write_subcaption_file}  ss    %Fv122>>vFF""3;t/?#@#@7"SSSL?LLMMMMMr9   c                P    |t           d<   t          j        ddd| di           dS )z*Prints the "File Ready" message to STDOUT.ri   z
File ready at %(file_path)s
r   r  N)r   r   r   )rc   r   s     r7   r   z(SceneFileWriter.print_file_ready_message  s6     )}5EUEUEUEU7VWWWWWr9   )rR   rS   rT   r>   rU   r   r+   r?   )rT   r>   r+   r?   )r+   r?   )rX   r>   rY   r>   rZ   r   r+   r?   )r   r   r+   r?   )r+   r>   )NN)r   r   r   r   r   r   r+   r?   )
r   r(   r   r   r   r   rU   r   r+   r?   )FN)r   r   r   r   r+   r?   )F)r   r   r+   r?   )rN   r'   r   r/   r+   r?   )r    )r   r   r   r/   r+   r?   )
r   r   r   r(   r   r>   r   r/   r+   r?   )r   r   r+   r?   rW  )r   r   r+   r?   )r  r>   r+   r   )FF)
r  r  ri   r   r   r   r   r   r+   r?   )r   r(   r+   r?   )!__name__
__module____qualname____doc__rv   rd   r[   r   ra   r   r   r\   r   r   r   r   r   r   r   r   r   r   r   r   r   r  rT  r   r   r   r   r   r   r  r9   r7   r   r   ]   s        6 "'
 
 
 
&K K K KZ       

 
 
 
2Q Q Q Q8. . . .@$ $ $ $3 3 3 3 "+/	)
 )
 )
 )
 )
\ "!	)< )< )< )< )<Z FJ@ @ @ @ @	. 	. 	. 	. 	.@ @ @ @1 1 1 1  QR) ) ) ) )V   < < < < ) ) ) )&,# ,# ,# ,# ,#\
 
 
 
(   2 !$h! h! h! h! h!Tj( j( j( j(X6 6 6 6    ,
 
 
 
N N N NX X X X X Xr9   )r)   r*   r+   r   )r:   r   r;   r<   r=   r>   r+   r?   )Dr  
__future__r   __all__rt  re  warnings	fractionsr   pathlibr   r   r   tempfiler   r	   	threadingr
   typingr   r   rB   numpyr   r  PILr   catch_warningsfilterwarningsRuntimeWarningpydubr   manimr   rh   r   r   _config.logger_utilsr   	constantsr   utils.file_opsr   r   r   r   r   r   r   utils.soundsr   rw  r!   r"   av.container.outputr#   	av.streamr$   manim.renderer.cairo_rendererr%   manim.renderer.opengl_rendererr&   manim.typingr'   r(   r8   rP   r   r  r9   r7   <module>r     s5   . . " " " " " "
                      > > > > > > > >       % % % % % % % % 				     



      
 X # #H'   
 #"""""# # # # # # # # # # # # # # #               2 2 2 2 2 2 $ $ $ $ $ $                  4 3 3 3 3 3 0 0 0 0 0 0 0 0 1333333      ;;;;;;======00000000       "% % % %"kX kX kX kX kX kX kX kX kX kXs   !BBB