o
    2jU                     @   s  d dl Z d dlZd dlmZmZ d dlmZ d dlZd dlm	  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 d d	lmZ d d
lmZmZmZmZ d dlm Z  d dl!m"Z" d dl#m$Z% e&e'e' Z(e)ej*ej*ej+  Z,ej-ej.ej/dddej0dej1fddZ2ej-ej.ej/dddej*ej3 dej*ej*ej4  dej5dej1dej*ej*ej6  dej1fddZ7ej-ej/dddej*ej3 dej*ej*ej4  dej5dej1dej*ej*ej6  dej1fddZ8ej-ej.ej/dddej*ej3 dej*ej4 dej1fddZ9ej-ej/dddej*ej3 dej*ej4 dej1fdd Z:ej-ej.ej/ddd!ej*ej3 dej;fd"d#Z<G d$d% d%eZ=G d&d' d'eZ>ej?G d(d) d)Z@						*	+	,			d/d-d.ZAdS )0    N)FlagIntEnum)Path)timeout_info)InputContainer)OutputContainer)pyio_close_custom_gilpyio_close_gil)	err_checkstash_exception)build_container_format)avdict_to_dictavrational_to_fractiondict_to_avdictto_avrational)int64_t)dereference)CaptureFcheckpreturnc                 C   s   t ttjt | }|jdk rdS tj# t }||j	k r1t
ttdd f 	 W d    dS W d    n1 s;w   Y  ||j	|j krJdS dS )Nr   z.Clock has been changed to before timeout start   )r   cythoncastpointerr   timeoutgiltime	monotonic
start_timer   RuntimeError)r   infocurrent_time r$   L/var/www/html/whisper/venv/lib/python3.10/site-packages/av/container/core.pyinterrupt_cb   s&   

r&   spburlflagsoptionsc                 C   s:   t j t| ||||W  d    S 1 sw   Y  d S N)r   r   pyav_io_open_gil)r'   r(   r)   r*   r+   r$   r$   r%   pyav_io_open9   s   
$r.   c           	      C   s   zXt tt| j}|t jur&ttt t jt jtj	  ||j
|jd}ni }||t jur5t t|nd||}t||j|tj@ dk}||jt t|jj< |j|d< W dS  tyd   t  Y S w )N)encodingerrors r   )r   r   	Containerr   opaqueNULLr   r   libAVDictionarymetadata_encodingmetadata_errorsio_openstrPyIOFilebuffer_sizeAVIO_FLAG_WRITE
open_filesr   	iocontext	Exceptionr   )	r'   r(   r)   r*   r+   	containeroptions_dictfile	pyio_filer$   r$   r%   r-   G   s4   



r-   c                 C   s4   t j t| |W  d    S 1 sw   Y  d S r,   )r   r   pyav_io_close_gil)r'   r(   r$   r$   r%   pyav_io_closeu   s   $rF   c                 C   s   d}z/t tt| j}|jd ur+t t|j|jv r+t|}|jt t|j= W |S t|}W |S  t	yA   t
  tj}Y |S w Nr   )r   r   r2   r   r3   r>   r   r   r	   r@   r   r5   AVERROR_UNKNOWN)r'   r(   resultrA   r$   r$   r%   rE      s   

rE   ctxc              
   C   s   | j tjkrIt| jD ]/}| j | tjkr:| j | jtjkr*tt| j | j t	t
tjt| j |  qt	t
tjt| j  d| _d S rG   )chaptersr   r4   rangenb_chaptersmetadatar5   av_dict_freeaddressav_freepr   pp_void)rJ   ir$   r$   r%   _free_chapters   s   
rT   c                   @   s   e Zd ZU ejZded< ejZded< ej	Z
ded< ejZded< ejZd	ed
< ejZded< ejZded< ejZded< ejZded< ejZded< ejZded< ejZded< ejZded< ejZ ded< dS )Flagsz?Generate missing pts even if it requires parsing future frames.gen_ptszIgnore index.ign_idxz-Do not block when reading packets from input.	non_blockz1Ignore DTS on frames that contain both DTS & PTS.ign_dtszWDo not infer any values from other values, just return what is stored in the container.	no_fillinzDo not use AVParsers, you also must set AVFMT_FLAG_NOFILLIN as the fill in code works on frames and no parsing -> no frames. Also seeking to frames can not work if parsing to find frame boundaries has been disabled.no_parsez#Do not buffer frames when possible.	no_bufferzDThe caller has supplied a custom AVIOContext, don't avio_close() it.	custom_ioz Discard frames marked corrupted.discard_corruptz#Flush the AVIOContext every packet.flush_packetszWhen muxing, try to avoid writing any random/volatile data to the output. This includes any random IDs, real-time timestamps/dates, muxer version, etc. This flag is mainly intended for testing.bitexactzTTry to interleave outputted packets by dts (using this flag can slow demuxing down).sort_dtsz3Enable fast, but inaccurate seeks for some formats.	fast_seekz0Add bitstream filters as requested by the muxer.auto_bsfN)!__name__
__module____qualname__r5   AVFMT_FLAG_GENPTSrV   __annotations__AVFMT_FLAG_IGNIDXrW   AVFMT_FLAG_NONBLOCKrX   AVFMT_FLAG_IGNDTSrY   AVFMT_FLAG_NOFILLINrZ   AVFMT_FLAG_NOPARSEr[   AVFMT_FLAG_NOBUFFERr\   AVFMT_FLAG_CUSTOM_IOr]   AVFMT_FLAG_DISCARD_CORRUPTr^   AVFMT_FLAG_FLUSH_PACKETSr_   AVFMT_FLAG_BITEXACTr`   AVFMT_FLAG_SORT_DTSra   AVFMT_FLAG_FAST_SEEKrb   AVFMT_FLAG_AUTO_BSFrc   r$   r$   r$   r%   rU      s   
 rU   c                   @   s   e Zd ZdZejZejZej	Z
ejZejZejZejZejZejZejZejZejZejZejZ ej!Z"ej#Z$ej%Z&ej'Z(ej)Z*ej+Z,ej-Z.ej/Z0ej1Z2ej3Z4ej5Z6ej7Z8ej9Z:ej;Z<ej=Z>ej?Z@ejAZBejCZDejEZFejGZHejIZJdS )
AudioCodecz Enumeration for audio codec IDs.N)Krd   re   rf   __doc__r5   AV_CODEC_ID_NONEnoneAV_CODEC_ID_PCM_ALAWpcm_alawAV_CODEC_ID_PCM_BLURAY
pcm_blurayAV_CODEC_ID_PCM_DVDpcm_dvdAV_CODEC_ID_PCM_F16LE	pcm_f16leAV_CODEC_ID_PCM_F24LE	pcm_f24leAV_CODEC_ID_PCM_F32BE	pcm_f32beAV_CODEC_ID_PCM_F32LE	pcm_f32leAV_CODEC_ID_PCM_F64BE	pcm_f64beAV_CODEC_ID_PCM_F64LE	pcm_f64leAV_CODEC_ID_PCM_LXFpcm_lxfAV_CODEC_ID_PCM_MULAW	pcm_mulawAV_CODEC_ID_PCM_S16BE	pcm_s16beAV_CODEC_ID_PCM_S16BE_PLANARpcm_s16be_planarAV_CODEC_ID_PCM_S16LE	pcm_s16leAV_CODEC_ID_PCM_S16LE_PLANARpcm_s16le_planarAV_CODEC_ID_PCM_S24BE	pcm_s24beAV_CODEC_ID_PCM_S24DAUDpcm_s24daudAV_CODEC_ID_PCM_S24LE	pcm_s24leAV_CODEC_ID_PCM_S24LE_PLANARpcm_s24le_planarAV_CODEC_ID_PCM_S32BE	pcm_s32beAV_CODEC_ID_PCM_S32LE	pcm_s32leAV_CODEC_ID_PCM_S32LE_PLANARpcm_s32le_planarAV_CODEC_ID_PCM_S64BE	pcm_s64beAV_CODEC_ID_PCM_S64LE	pcm_s64leAV_CODEC_ID_PCM_S8pcm_s8AV_CODEC_ID_PCM_S8_PLANARpcm_s8_planarAV_CODEC_ID_PCM_U16BE	pcm_u16beAV_CODEC_ID_PCM_U16LE	pcm_u16leAV_CODEC_ID_PCM_U24BE	pcm_u24beAV_CODEC_ID_PCM_U24LE	pcm_u24leAV_CODEC_ID_PCM_U32BE	pcm_u32beAV_CODEC_ID_PCM_U32LE	pcm_u32leAV_CODEC_ID_PCM_U8pcm_u8AV_CODEC_ID_PCM_VIDCpcm_vidcr$   r$   r$   r%   rv      sJ    
rv   c                   @   s   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Ze	j
e	jdddde	jde	jfddZdd Ze	j
dd Ze	j
dd Ze	j
dd Zedd ZejdejfddZedd  Zejde	jfd!d Zed"d# Zd$d% Zd&d' Zd(S ))r2   c              	   C   s  |t urtdt| t}|st| tstdt|tr!|| _n	tt|dd| _t|p.d| _	t|p5d| _
dd |p>dD | _|| _|| _|	| _|| _|| _|
| _|| _d }|d urmd|v rh|d\}}t|| _t| j}|}|r|  jd	O  _| jr| jjnttj|tj}|tjkrtd
tj t t!| j"|tj|}W d    n1 sw   Y  | #| n(t$ | _"| jd us| jd urt%| j"j&_'t!| j(| j"j&_)|d urtt*|| j"_+| j" j,tj-O  _,t.tj/| | j"_)i | _0t|t1st2||
|| _3| j3j4| j"_5|d ur#t6| j"_t7| j"_8| j" j,tj9O  _,|sw| jr.| jj:ntj}t;| j	| j
}| <| j | =  tj t>t!| j"||t!|j"}W d    n	1 saw   Y  | <d  | #| |  jdO  _|d u rt?| j"j@| j"jA| _d S d S )Nzcannot construct base Containerz&Container cannot be directly extended.namez<none>r$   c                 S   s   g | ]}t |qS r$   )dict).0xr$   r$   r%   
<listcomp>  s    z'Container.__cinit__.<locals>.<listcomp>:r   z!Could not determine output format   )B_cinit_sentinelr!   
isinstancer   r   r:   r   getattrr   r+   container_optionsstream_optionshwaccelr7   r8   open_timeoutread_timeoutr<   r9   splitContainerFormatformatosfsencode_myflagoptrr5   av_guess_formatr   r4   
ValueErrornogilavformat_alloc_output_context2rP   ptrr
   avformat_alloc_contextr&   interrupt_callbackcallbackinterrupt_callback_infor3   rv   audio_codec_idr*   rg   r   p_voidr>   
basestringr;   rC   r?   r(   r.   rF   	io_close2ro   iptr
Dictionaryset_timeoutstart_timeoutavformat_open_inputr   iformatoformat)selfsentinelfile_format_namer+   r   r   r   r7   r8   r<   r   r   r9   	writeableacodecname_objr   ofmtresifmt	c_optionsr$   r$   r%   	__cinit__   s   












zContainer.__cinit__c                 C   s8   t j t| j W d    d S 1 sw   Y  d S r,   )r   r   r5   avformat_free_contextr   r   r$   r$   r%   __dealloc__^  s   "zContainer.__dealloc__c                 C   s   | S r,   r$   r   r$   r$   r%   	__enter__b     zContainer.__enter__c                 C   s   |    d S r,   )close)r   exc_typeexc_valexc_tbr$   r$   r%   __exit__e  s   zContainer.__exit__c                 C   s   t r,   )NotImplementedErrorr   r$   r$   r%   r   h  r   zContainer.closec                 C   s   d| j j d| jp| jdS )Nz<av. >)	__class__rd   rC   r   r   r$   r$   r%   __repr__k  s   zContainer.__repr__Fr   valuer   c                 C   s   t || jdS )N)filename)r
   r   r   r  r$   r$   r%   r
   n  s   zContainer.err_checkc              	   C   sZ   |    t }t| jddt| t W d    n1 sw   Y  ddd |D S )Nr   r1   c                 s   s    | ]}|d  V  qdS )r   Nr$   )r   logr$   r$   r%   	<genexpr>w  s    z)Container.dumps_format.<locals>.<genexpr>)_assert_open
LogCapturer5   av_dump_formatr   r   r   join)r   logsr$   r$   r%   dumps_formats  s
   zContainer.dumps_formatc                 C   s    |d u r
d| j _d S || j _d S )Ng      )r   r   )r   r   r$   r$   r%   r   y  s   zContainer.set_timeoutc                 C   s   t  | j_d S r,   )r   r   r   r    r   r$   r$   r%   r     s   zContainer.start_timeoutc                 C   s   | j tjkr
tdd S )NzContainer is not open)r   r   r4   AssertionErrorr   r$   r$   r%   r	    s   zContainer._assert_openc                 C      |    | jjS r,   r	  r   video_codec_idr   r$   r$   r%   r       zContainer.video_codec_idc                 C      |    || j_d S r,   r  r  r$   r$   r%   r       c                 C   r  r,   r	  r   r*   r   r$   r$   r%   r*     r  zContainer.flagsc                 C   r  r,   r  r  r$   r$   r%   r*     r  c                 C   s
   | j d@ S )Nr   )r   r   r$   r$   r%   input_was_opened  s   
zContainer.input_was_openedc                 C   sd   |    g }t| jjD ]#}| jj| }||j|j|jt	t
|jt|j| j| jd q|S )N)idstartend	time_baserN   )r	  rL   r   rM   rK   appendr  r  r  r   r   rP   r  r   rN   r7   r8   )r   rI   rS   chr$   r$   r%   rK     s   zContainer.chaptersc              
   C   sl  |    t|}tj t| j W d    n1 sw   Y  |dkr/d| j_tj| j_d S t	t
t|ttjtj  }|tjkrJtdt|D ]]}|| }t	tjtj tttj}|tjkrntd|d |_t	t|d |_t	t|d |_t|d t|j tj|_d|v rtt|j|d | j| j |||< qN|| j_|| j_d S )	Nr   zav_malloc failed for chapterszav_malloc failed for chapterr  r  r  r  rN   )r	  lenr   r   rT   r   rM   r4   rK   r   AVChapterPtrPtrr5   	av_mallocsizeofr   	AVChapterMemoryErrorrL   r  r   r  r  r   rP   r  rN   r   r7   r8   )r   rK   countch_arrayrS   entryr  r$   r$   r%   set_chapters  sL   






zContainer.set_chaptersN)rd   re   rf   r   r   r   r   r   r  r   cfunc	exceptvalintr
   r  r   r   r	  propertyr  setterr5   	AVCodecIDr*   r  rK   r'  r$   r$   r$   r%   r2      s:    x





r2   utf-8strict   c                 C   s   |du st |tr|dks|dkstd| t | trnt | tr'|  } n
|du r1t| dd}|du r7d}t |	trKt|	dksFtd|	\}}n|	}|	}|drdtt	| |||||||||||
S |rjtdt
t	| ||||d||||||
S )	a	  open(file, mode='r', **kwargs)

    Main entrypoint to opening files/streams.

    :param str file: The file to open, which can be either a string or a file-like object.
    :param str mode: ``"r"`` for reading and ``"w"`` for writing.
    :param str format: Specific format to use. Defaults to autodect.
    :param dict options: Options to pass to the container and all streams.
    :param dict container_options: Options to pass to the container.
    :param list stream_options: Options to pass to each stream.
    :param str metadata_encoding: Encoding to use when reading or writing file metadata.
        Defaults to ``"utf-8"``.
    :param str metadata_errors: Specifies how to handle encoding errors; behaves like
        ``str.encode`` parameter. Defaults to ``"strict"``.
    :param int buffer_size: Size of buffer for Python input/output operations in bytes.
        Honored only when ``file`` is a file-like object. Defaults to 32768 (32k).
    :param timeout: How many seconds to wait for data before giving up, as a float, or a
        ``(open timeout, read timeout)`` tuple.
    :param callable io_open: Custom I/O callable for opening files/streams.
        This option is intended for formats that need to open additional
        file-like objects to ``file`` using custom I/O.
        The callable signature is ``io_open(url: str, flags: int, options: dict)``, where
        ``url`` is the url to open, ``flags`` is a combination of AVIO_FLAG_* and
        ``options`` is a dictionary of additional options. The callable should return a
        file-like object.
    :param HWAccel hwaccel: Optional settings for hardware-accelerated decoding.
    :rtype: Container

    For devices (via ``libavdevice``), pass the name of the device to ``format``,
    e.g.::

        >>> # Open webcam on MacOS.
        >>> av.open('0', format='avfoundation') # doctest: +SKIP

    For DASH and custom I/O using ``io_open``, add a protocol prefix to the ``file`` to
    prevent the DASH encoder defaulting to the file protocol and using temporary files.
    The custom I/O callable can be used to remove the protocol prefix to reveal the actual
    name for creating the file-like object. E.g.::

        >>> av.open("customprotocol://manifest.mpd", "w", io_open=custom_io) # doctest: +SKIP

    .. seealso:: :ref:`garbage_collection`

    More information on using input and output devices is available on the
    `FFmpeg website <https://www.ffmpeg.org/ffmpeg-devices.html>`_.
    Nrwz%mode must be 'r', 'w', or None, got: moder   z0timeout must be `float` or `tuple[float, float]`zAProvide stream options via Container.add_stream(..., options={}).)r   r:   r   r   r   tupler  
startswithr   r   r   )rC   r3  r   r+   r   r   r7   r8   r<   r   r9   r   r   r   r$   r$   r%   open  sf   "=




r6  )NNNNNr.  r/  r0  NNN)Br   r   enumr   r   pathlibr   r   cython.cimports.libavcimportslibavr5   !cython.cimports.av.container.corer   "cython.cimports.av.container.inputr   #cython.cimports.av.container.outputr   !cython.cimports.av.container.pyior   r	   cython.cimports.av.errorr
   r   cython.cimports.av.formatr   cython.cimports.av.utilsr   r   r   r   cython.cimports.libc.stdintr   cython.operatorr   
av.loggingr   r
  declareobjectr   typedefr   r"  r  r(  r   r)  r   r*  r&   AVFormatContextAVIOContextp_const_charr6   r.   r-   rF   rE   voidrT   rU   rv   cclassr2   r6  r$   r$   r$   r%   <module>   s    




,






"(  