
    djX                         d dl Z d dl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 d dlmZ d dlmZ d d	lmZmZ d d
lmZmZ ej        defd            Zej         G d de                      ZdS )    N)libav)Codec)CodecContextwrap_codec_context)StreamContainer)
Dictionary)	err_check)Packet)Streamwrap_stream)dict_to_avdictto_avrationalselfc                    t                      | _        | j        dz  dk    r2| j        -t	          | j        j        dd          r| xj        dz  c_        d S 	 |                     t          j        | j                             | j        N| j        j	        j
        t          j        z  s0t          j        t          j        | j        j                             | xj        dz  c_        d S # | j        N| j        j	        j
        t          j        z  s0t          j        t          j        | j        j                             | xj        dz  c_        w xY wd S )N      closedF   )r   streams_myflagfilegetattrr	   libav_write_trailerptroformatflagsAVFMT_NOFILEavio_closepcythonaddresspbr   s    N/home/agentuser/manim-venv/lib/python3.11/site-packages/av/container/output.pyclose_outputr%      s&   "$$DL|bA 9 WTY^Xu%M%M LLALLF	NN3/99:::y $(*:*@3CS*S tx{ ; ;<<<LLALLLL y $(*:*@3CS*S tx{ ; ;<<<LLALLLLLL s   ,C+ +A'Ec                   (   e Zd Zd Zd Zd Zddedz  fdZddede	fd	Z
	 dd
e	dedz  fdZd
e	de	fdZdededefdZddedz  fdZej        d             Zed             Zed             Zed             Zed             Zd Zd ZdefdZdS )OutputContainerc                     t                      | _        i | _        t          j        5  t          j                    | _        d d d            d S # 1 swxY w Y   d S N)r   r   metadatar    nogilr   av_packet_alloc
packet_ptr)r   argskwargss      r$   	__cinit__zOutputContainer.__cinit__%   s    &((\ 	4 	4!133DO	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4s   AAAc                 $    t          |            d S r)   r%   r#   s    r$   __del__zOutputContainer.__del__+       T    c                     t           j        5  t          j        t          j        | j                             d d d            d S # 1 swxY w Y   d S r)   )r    r+   r   av_packet_freer!   r-   r#   s    r$   __dealloc__zOutputContainer.__dealloc__.   s    \ 	@ 	@v~do>>???	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@s   ,AA
A
Noptionsc           	      n   t          |d          }|j        }t          j        | j        j        |j        t          j                  s$t          | j        j	        d|j	        d          t          j
        | j        |          }t          j        |          }|j        t          j        k    rt          j        |_        |                    dd          |_        |                    dd          |_        |                    dd	          |_        |                    d
d          |_        	 t+          |                    d          t-          j        |j                             n# t2          $ r Y nw xY wt+          |pdt-          j        |j                             |j        |_        |j        |_        n|j        t          j        k    rt,          j        }	t          j        t,          j        |t          j        d	t-          j        |	          t,          j                   |	r:t-          j         t,          j!        t          j"                 |	          d	         |_#        |                    dd	          |_        |                    d
d          |_        	 t+          |                    d          t-          j        |j                             n# t2          $ r Y nw xY w|d|_$        n-t          |          tJ          u r||_$        ntM          d          |j        |_        t          j'        t-          j        |j(                  d           | j        j        j)        t          j*        z  r|xj)        t          j+        z  c_)        tY          t          j-        |j.        |                     t_          ||d          }
ta          | ||
          }| j1        2                    |           |r|j3        4                    |           |5                                D ]\  }}tm          |||           |S )a  add_stream(codec_name, rate=None)

        Creates a new stream from a codec name and returns it.
        Supports video, audio, and subtitle streams.

        :param codec_name: The name of a codec.
        :type codec_name: str
        :param dict options: Stream options.
        :param \**kwargs: Set attributes for the stream.
        :rtype: The new :class:`~av.stream.Stream`.

        w format does not support  codecwidthi  heighti  bit_rater   bit_rate_tolerancei  	time_base   i }  Ni  'audio stream `rate` must be: int | None   )7r   r   r   avformat_query_codecr   idFF_COMPLIANCE_NORMAL
ValueErrorformatnameavformat_new_streamavcodec_alloc_context3typeAVMEDIA_TYPE_VIDEOAV_PIX_FMT_YUV420Ppix_fmtpopr>   r?   r@   rA   r   r    r!   rB   KeyError	framerateavg_frame_rateAVMEDIA_TYPE_AUDIONULLavcodec_get_supported_configAV_CODEC_CONFIG_SAMPLE_FORMATcastpointerAVSampleFormat
sample_fmtsample_rateint	TypeErrorav_channel_layout_default	ch_layoutr   AVFMT_GLOBALHEADERAV_CODEC_FLAG_GLOBAL_HEADERr	   avcodec_parameters_from_contextcodecparr   r   r   
add_streamr9   updateitemssetattr)r   
codec_namerater9   r/   	codec_objcodecstreamctxoutpy_codec_context	py_streamkvs                 r$   rg   zOutputContainer.add_stream2   s    !S11	;D= 'Heh(@
 
 	 ;#XX	XXX  
 03/FtxQV/W/W252LU2S2S :///0CK

7C00CIHc22CJ!::j!44CL%+ZZ0Df%M%MC"fjj55v~cm7T7TUUUU   $*"fnS]&C&CDDD$'MF!"}F Z3111=C[C,1s##    Y!'V^C<N-OQT!U!UVW!X!::j!44CL%+ZZ0De%L%LC"fjj55v~cm7T7TUUUU    |"'ds"""& IJJJ"}F)&.*G*GKKK 8!C$:: 	9II88II 	#5fosKKLLL *<C)M)M'f6FGG		*** 	.$$W---LLNN 	% 	%DAqIq!$$$$s$   -:E( (
E54E5:K 
K! K!rk   returnc                    |                                 }t          j        |          }t          j        }|t          j        k    rt          j        |          }|t          j        k    r6t          j        |          }|t          j        k    rt          d|          |t          j        k    r|j        }|j	        }n|j        }|j	        }t          j
        | j        j        |t          j                  st          | j        j        d|d          t          j        | j        t          j                  }	|	t          j        k    rt#          d          ||	j        _        ||	j        _        |t          j        k    rj|                    dd          |	j        _        |                    dd          |	j        _        |'t3          |t          j        |	j                             nD|t          j        k    r4|2t          |          t:          u r||	j        _        nt?          d	          tA          | |	d          }
| j!        "                    |
           |#                                D ]\  }}tI          |
||           |
S )
a  add_mux_stream(codec_name, rate=None)

        Creates a new stream for muxing pre-encoded data without creating a
        :class:`.CodecContext`. Use this when you want to mux packets that were
        already encoded externally and no encoding/decoding is needed.

        :param codec_name: The name of a codec.
        :type codec_name: str
        :param \**kwargs: Set attributes for the stream (e.g. ``width``, ``height``,
            ``time_base``).
        :rtype: The new :class:`~av.stream.Stream`.

        zUnknown codec: r<   r=   Could not allocate streamr>   r   r?   NrD   )%encoder   avcodec_find_encoder_by_namer    rW   avcodec_find_decoder_by_nameavcodec_descriptor_get_by_namerI   rG   rN   rF   r   r   rH   rJ   rK   rL   MemoryErrorrf   codec_id
codec_typerO   rR   r>   r?   r   r!   rU   rV   r_   r^   r`   r   r   rg   ri   rj   )r   rk   rl   r/   codec_name_bytesrn   codec_descriptorr~   r   ro   rs   rt   ru   s                r$   add_mux_streamzOutputContainer.add_mux_stream   sl    #-"3"3"5"5,-=>> 	 K 	 FK45EFFEFK"ABRSS6;.. !A:!A!ABBB FKxHJJ'*H).J 'Hh(@
 
 	 ;#TT
TTT  
 03/FHfk0
 0
 V[  9:::#+ %/"///$*JJw$:$:FO!%+ZZ!%<%<FO"dFN63H$I$IJJJ3111::$$26FO//#$MNNN (fd;;		***LLNN 	% 	%DAqIq!$$$$r5   templateopaquec                    ||j         dk    }|j         | j        |fi |S |r|j        j        }nt	          |j        j        j        d          }|j        }t          j        | j        j	        |j
        t          j                  s$t          | j        j        d|j        d          t          j        | j        |          }t          j        |          }t!          t          j        ||j        j                             d|_        | j        j	        j        t          j        z  r|xj        t          j        z  c_        |s|j        j        |_        t!          t          j        |j        |                     t1          ||d          }|xj        dz  c_        t5          | ||          }	| j                            |	           |                                D ]\  }
}t=          |	|
|           |	S )a  
        Creates a new stream from a template. Supports video, audio, subtitle, data and attachment streams.

        :param template: Copy codec from another :class:`~av.stream.Stream` instance.
        :param opaque: If True, copy opaque data from the template's codec context.
        :param \**kwargs: Set attributes for the stream.
        :rtype: The new :class:`~av.stream.Stream`.
        Nvideor;   r<   r=   r      )rN   codec_context'_add_stream_without_codec_from_templatern   r   rK   r   r   rF   r   rG   rH   rI   rJ   rL   rM   r	   avcodec_parameters_to_contextrf   	codec_tagr   rc   rd   re   r   	_ctxflagsr   r   rg   ri   rj   )r   r   r   r/   rm   rn   ro   rp   rr   rs   rt   ru   s               r$   add_stream_from_templatez(OutputContainer.add_stream_from_template   s    >]g-F!)?4?SSFSSS  	F .4IIh4:?EEI;D= 'Heh(@
 
 	 ;#XX	XXX  
 03/FtxQV/W/W252LU2S2S#3C9NOOPPP 8!C$:: 	9II88II  	5 .4CI 	#5fosKKLLL *<C)M)M""a'""'f6FGG		***LLNN 	% 	%DAqIq!$$$$r5   c                    |j         j        j        }|t          j        t          j        hvrt          d|j         d          t          j        | j         t          j
                  }|t          j
        k    rt          d          t          t          j        |j        |j         j                             |j         j        |_        |j         j        |_        |j         j        |_        |j         j        |_        t%          | |d           }| j                            |           t+          |j                  |_        |                                D ]\  }}t1          |||           |S )Nztemplate stream of type z has no codec contextrx   )r   rf   r   r   AVMEDIA_TYPE_ATTACHMENTAVMEDIA_TYPE_DATArI   rN   rL   r    rW   r}   r	   avcodec_parameters_copyrB   
start_timedurationdispositionr   r   rg   dictr*   ri   rj   )r   r   r/   r   ro   rs   rt   ru   s           r$   r   z7OutputContainer._add_stream_without_codec_from_template!  sK    "*!6!A
c93;PQQQO8=OOO   03/FHfk0
 0
 V[  9:::#-fox|?TUUVVV $<1$L3",/%\5'fd;;		***!("344	LLNN 	% 	%DAqIq!$$$$r5   rK   mimetypedatac                    t          j        | j        t          j                  }|t          j        k    rt          d          t           j        |j        _        t           j	        |j        _
        t          |          }|rt          j        t          j        t          j        |dz                       }|t          j        k    rt          d          t          |          D ]}||         ||<   d||<   t          j        t          j        |          |j        _        ||j        _        t          j        |j                  }t)          t          j        |d|                                d                     |                                }	t)          t          j        |d|	d                     t/          | |d          }
| j                            |
           |
S )a  
        Create an attachment stream and embed its payload into the container header.

        - Only supported by formats that support attachments (e.g. Matroska).
        - No per-packet muxing is required; attachments are written at header time.
        rx   r   z"Could not allocate attachment datar   s   filenames   mimetypeN)r   rL   r   r    rW   r}   r   rf   r   AV_CODEC_ID_NONEr~   lenrZ   p_uchar	av_mallocrange	extradataextradata_sizer!   r*   r	   av_dict_setry   r   r   rg   )r   rK   r   r   ro   payload_sizebufimeta_ptr
mime_bytesrs   s              r$   add_attachmentzOutputContainer.add_attachmentB  s    03/FHfk0
 0
 V[  9:::%(%@"#&#7  '*$ii 		:+fncmL1<L.M.MNNCfk!!!"FGGG<(( ! !aA !C(.FNC(H(HFO%-9FO* >&/22#/(KJJKKK__&&
#/(KQGGHHH'fd;;		***r5   c                 >   t           j        }t           j        }||                                }t          j        |          }|t           j        k    rt          j        |          }|t           j        k    r6t          j        |          }|t           j        k    rt          d|           |t           j        k    r|j        n|j        }t          j	        | j
        j        |t          j                  st          | j        j        d|d          t          j        | j
        |          }|t           j        k    rt!          d          t           j        }|t           j        k    rt          j        |          }|t           j        k    rt!          d          | j
        j        j        t          j        z  r|xj        t          j        z  c_        t+          t          j        |j        |                     n7t          j        |j        _        |t           j        k    r|j        |j        _        d}	|t           j        k    rt7          ||d          }	t9          | ||	          }
| j                            |
           |r|
j                             |           |
S )a4  add_data_stream(codec_name=None)

        Creates a new data stream and returns it.

        :param codec_name: Optional name of the data codec (e.g. 'klv')
        :type codec_name: str | None
        :param dict options: Stream options.
        :rtype: The new :class:`~av.data.stream.DataStream`.
        NzUnknown data codec: r<   r=   rx   z Could not allocate codec context)!r    rW   ry   r   rz   r{   r|   rI   rG   rF   r   r   rH   rJ   rK   rL   r}   rM   r   rc   rd   r	   re   rf   r   r   r~   r   r   r   rg   r9   rh   )r   rk   r9   rn   r   r   r~   ro   rp   rr   rs   s              r$   add_data_streamzOutputContainer.add_data_streamj  sq    <B;K 	 !&0&7&7&9&945EFFE##89IJJ###&#EFV#W#W #v{22$%HJ%H%HIII $)FK#7#7uxx=M=PH+ (C,D   !{'XXJXXX  
 03/FtxQV/W/WV[  9::: 39+FK,U33Cfk!!!"DEEE x%(>> =		S<<		 c9&/3OOPPPP *-)>FO&6;..+;+>( 15&+1#udCC'f6FGG		*** 	.$$W---r5   c                 X   | j         dz  rdS t                      | j        D ]}|j        }||j        s| j                                        D ] \  }}|j                            ||           !|j        dz  s<|	                                 | j        D ] }||j        vr
                    |           !|                                 t          j        | j        | j        nd          }|}| j        j        t$          j        k    rg| j        j        j        t,          j        z  sIt1          t-          j        t%          j        | j        j                  |t,          j                             t9          t%          j        | j        j                  | j        | j        | j                   tA          | j        | j!                  }|"                                }|                     t-          j#        | j        t%          j        |j                                       |D ]}||vr
                    |           fd| j                                        D             }	|	r6ddl$}
|
%                    tL                    }|'                    d|	z             | xj         dz  c_         dS )	z,Write the file header! Called automatically.r   Nr    )encodingerrorsc                 $    i | ]\  }}|v	||S  r   ).0rt   ru   used_optionss      r$   
<dictcomp>z2OutputContainer.start_encoding.<locals>.<dictcomp>  s0     
 
 
QQl5J5JAq5J5J5Jr5   r   zSome options were not used: %s)(r   setr   r   is_openr9   ri   
setdefaultr   openadd_finalize_for_outputosfsencoder   rK   r   r"   r    rW   r   r   r   r   r	   	avio_openr!   AVIO_FLAG_WRITEr   r*   metadata_encodingmetadata_errorsr   container_optionscopyavformat_write_headerlogging	getLogger__name__warning)r   ro   rp   rt   ru   name_objrK   all_optionsr9   unused_optionsr   logr   s               @r$   start_encodingzOutputContainer.start_encoding  s    <! 	F  EE l 	* 	*F&Cs{ L..00 1 1DAqK**1a0000) 0HHJJJ "\ 0 0CK//(,,Q///'')))) +493Ddii"MM&8;&+%%dh.>.DsGW.W%fnTX[994ATUU  
 	N48,--M+'		
 	
 	
 	
 #-T\4;Q"R"R)..00s06>'+;V;VWWXXX  	$ 	$A  ###
 
 
 
!\//11
 
 
  	KNNN##H--CKK8>IJJJr5   c                 ^   t                      }t          j        }t          j        }	 t          j        t          j        |                    }|t          j        k    rnNt          j        | j        j        |j	        t          j
                  dk    r|                    |j                   |S )zC
        Returns a set of all codecs this format supports.
        Tr   )r   r    rW   r   av_codec_iterater!   rF   r   r   rG   rH   r   rK   )r   resultrn   r   s       r$   supported_codecsz OutputContainer.supported_codecs  s    
 ee;A; &	'()?)?@@E## (H$eh0H   
 

5:&&&	' r5   c                 H    t          j        | j        j        j                  S )zL
        Returns the default video codec this container recommends.
        )r   avcodec_get_namerJ   optrvideo_codecr#   s    r$   default_video_codecz#OutputContainer.default_video_codec
      
 #DK$4$@AAAr5   c                 H    t          j        | j        j        j                  S )zL
        Returns the default audio codec this container recommends.
        )r   r   rJ   r   audio_codecr#   s    r$   default_audio_codecz#OutputContainer.default_audio_codec  r   r5   c                 H    t          j        | j        j        j                  S )zO
        Returns the default subtitle codec this container recommends.
        )r   r   rJ   r   subtitle_codecr#   s    r$   default_subtitle_codecz&OutputContainer.default_subtitle_codec  s    
 #DK$4$CDDDr5   c                 $    t          |            d S r)   r2   r#   s    r$   closezOutputContainer.close  r4   r5   c                     t          |t                    r|                     |           d S |D ]}|                     |           d S r)   )
isinstancer
   mux_one)r   packetspackets      r$   muxzOutputContainer.mux"  s\     gv&& 	%LL!!!!!! % %V$$$$% %r5   r   c                 Z   |                                   |j        j        dk     s7t          j        t          j        |j        j                  | j        j        k    rt          d          | j        j        |j        j                 }|	                    |j
                   |                     t          j        | j        |j                             t          j        5  t          j        | j        | j                  }d d d            n# 1 swxY w Y   |                     |           d S )Nr   zBad Packet stream_index.)r   r   stream_indexr    rZ   uint
nb_streamsrI   r   _rebase_timerB   r	   r   av_packet_refr-   r+   av_interleaved_write_frame)r   r   ro   rets       r$   r   zOutputContainer.mux_one+  sD    J#a''{6;
(?@@DHDWWW7888/3x/?
@W/XF,--- 	s(&*EEFFF\ 	X 	X!<TXtWWC	X 	X 	X 	X 	X 	X 	X 	X 	X 	X 	X 	X 	X 	X 	Xss    DDD)NNr)   )r   
__module____qualname__r0   r3   r8   r   rg   strr   r   boolr   r   bytesr   r   r    ccallr   propertyr   r   r   r   r   r   r
   r   r   r5   r$   r'   r'   #   s       4 4 4  @ @ @_ _ _ _ _ _BK K Kf K K K K\ 7;? ??(,t? ? ? ?B	   B&3 &# &U & & & &PE Et E E E EN \> > \>@   X. B B XB B B XB E E XE  % % %f      r5   r'   )r   r    cython.cimportsr   r   cython.cimports.av.codec.codecr    cython.cimports.av.codec.contextr   r   $cython.cimports.av.container.streamsr   cython.cimports.av.dictionaryr   cython.cimports.av.errorr	   cython.cimports.av.packetr
   cython.cimports.av.streamr   r   cython.cimports.av.utilsr   r   cfuncr'   r%   cclass	Containerr   r5   r$   <module>r      s@   				  ( ( ( ( ( ( 0 0 0 0 0 0 M M M M M M M M @ @ @ @ @ @ 4 4 4 4 4 4 . . . . . . , , , , , , 9 9 9 9 9 9 9 9 B B B B B B B B     & Z Z Z Z Zi Z Z Z Z Zr5   