
    \jCW                        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	c 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jL                  e' e'             Z( ejR                  ejT                  ejT                  ejV                              Z,ejZ                  ej\                   ej^                  d      dej`                  dejb                  fd                     Z2ejZ                  ej\                   ej^                  d      dejT                  ejf                     dejT                  ejT                  ejh                        dejj                  dejb                  dejT                  ejT                  ejl                        dejb                  fd                     Z7ejZ                   ej^                  d      dejT                  ejf                     dejT                  ejT                  ejh                        dejj                  dejb                  dejT                  ejT                  ejl                        dejb                  fd              Z8ejZ                  ej\                   ej^                  d      dejT                  ejf                     dejT                  ejh                     dejb                  fd                     Z9ejZ                   ej^                  d      dejT                  ejf                     dejT                  ejh                     dejb                  fd              Z:ejZ                  ej\                   ej^                  d      dejT                  ejf                     dejv                  fd                     Z< G d de      Z= G d  d!e      Z>ej~                   G d" d#             Z@	 	 	 	 	 	 	 	 	 	 	 d%d$ZAy)&    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                    t        t        j                  t        j                  t           |             }|j
                  dk  ryt        j                  5  t        j                         }||j                  k  r%t        t        t        d      d f       	 d d d        y	 d d d        |j                  |j
                  z   kD  ryy# 1 sw Y   'xY w)Nr   z.Clock has been changed to before timeout start   )r   cythoncastpointerr   timeoutgiltime	monotonic
start_timer   RuntimeError)r   infocurrent_times      D/root/.hermes/venv/lib/python3.12/site-packages/av/container/core.pyinterrupt_cbr'      s     %V[[1Mq%QRD||a 
 ~~'$//)  !QR  ) doo44 s   A CCspburlflagsoptionsc                 l    t         j                  5  t        | ||||      cd d d        S # 1 sw Y   y xY wN)r   r   pyav_io_open_gil)r(   r)   r*   r+   r,   s        r&   pyav_io_openr0   9   s0     
 <2sE7;< < <s   *3c           	         	 t        j                  t        t        |       j                        }|t         j
                  urpt        t        t        j                  t         j                  t         j                  t        j                        |            |j                  |j                        }ni }|j                  |t         j
                  urt        j                  t        |      nd||      }t        ||j                  |t        j                   z  dk7        }||j"                  t        j                  t$        |j&                  j                        <   |j&                  |d<   y# t(        $ r t+               cY S w xY 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_files	            r&   r/   r/   G   s3   !KK	;q>+@+@A	&++%)KKv~~c6F6F'GH'
 #44 00L L  %(%;FKKS!UL
 )''%#2E2E*E!)K
	  	V[[)2E2E2L2LMN ##1 !  !s   E%E( (E>=E>c                 f    t         j                  5  t        | |      cd d d        S # 1 sw Y   y xY wr.   )r   r   pyav_io_close_gil)r(   r)   s     r&   pyav_io_closerJ   u   s*     
 ( B'( ( (s   '0c                    d}	 t        j                  t        t        |       j                        }|j
                  lt        j                  t        |j                        |j
                  v r<t        |      }|j
                  t        j                  t        |j                        = |S t        |      }	 |S # t        $ r t                t        j                  }Y |S w xY wNr   )r   r   r5   r   r6   rA   r   r	   r
   rC   r   r8   AVERROR_UNKNOWN)r(   r)   resultrD   s       r&   rI   rI      s     F%KK	;q>+@+@A	  ,GRYY/93G3GG*2.F $$V[["))%DE M $B'F M	  %$$M	%s   B#B7 )B7 7#CCctxc           
         | j                   t        j                  k7  rOt        | j                        D ]  }| j                   |   t        j                  k7  s$| j                   |   j
                  t        j                  k7  r?t        j                  t        j                  | j                   |   j
                               t        j                  t        j                  t        j                  t        j                  | j                   |                       t        j                  t        j                  t        j                  t        j                  | j                                      d| _        y rL   )chaptersr   r7   rangenb_chaptersmetadatar8   av_dict_freeaddressav_freepr   pp_void)rO   is     r&   _free_chaptersrZ      s    
 ||v{{"s' 	A||A&++-<<?++v{{:$$V^^CLLO4L4L%MNKKs||A0OP		 	V[[1MNOCO    c                      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j2                  Zded<   ej6                  Zded<   ej:                  Zded<   ej>                  Z ded<   y)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__r8   AVFMT_FLAG_GENPTSr^   __annotations__AVFMT_FLAG_IGNIDXr_   AVFMT_FLAG_NONBLOCKr`   AVFMT_FLAG_IGNDTSra   AVFMT_FLAG_NOFILLINrb   AVFMT_FLAG_NOPARSErc   AVFMT_FLAG_NOBUFFERrd   AVFMT_FLAG_CUSTOM_IOre   AVFMT_FLAG_DISCARD_CORRUPTrf   AVFMT_FLAG_FLUSH_PACKETSrg   AVFMT_FLAG_BITEXACTrh   AVFMT_FLAG_SORT_DTSri   AVFMT_FLAG_FAST_SEEKrj   AVFMT_FLAG_AUTO_BSFrk    r[   r&   r]   r]      s   QTQfQfGNf"44G_4ADAXAXI>XCFCXCXG@Xkn  lC  lCIh  C kn  kA  kAH  h  A7:7N7NI4NX[XpXpIUp:=:X:XO7X;>;W;WM8W UX  Ul  UlH  R  lgjg~g~Hd~GJG_G_ID_CFCZCZH@Zr[   r]   c                   X   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j2                  Zej6                  Zej:                  Zej>                  Z ejB                  Z"ejF                  Z$ejJ                  Z&ejN                  Z(ejR                  Z*ejV                  Z,ejZ                  Z.ej^                  Z0ejb                  Z2ejf                  Z4ejj                  Z6ejn                  Z8ejr                  Z:ejv                  Z<ejz                  Z>ej~                  Z@ej                  ZBej                  ZDej                  ZFej                  ZHej                  ZJy)
AudioCodecz Enumeration for audio codec IDs.N)Krl   rm   rn   __doc__r8   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   r      s   *D''H++J%%G))I))I))I))I))I))I%%G))I))I77))I77))I--K))I77))I))I77))I))I##F11M))I))I))I))I))I))I##F''Hr[   r   c                      e Zd Zd Zd Zd Zd Zd Zd Ze	j                   e	j                  dd	      d
e	j                  de	j                  fd              Zd Ze	j                  d        Ze	j                  d        Ze	j                  d        Zed        Zej(                  d
ej,                  fd       Zed        Zej(                  d
e	j                  fd       Zed        Zedefd       Zd Zd Zy)r5   c           	      
   |t         urt        d      t        | t              }|st        | t              st        d      t        |t
              r|| _        nt        t        |dd            | _        t        |xs d      | _	        t        |xs d      | _
        |xs dD cg c]  }t        |       c}| _        || _        || _        |	| _        || _        || _        |
| _        || _        d }|(d|v r|j'                  d      \  }}t)        |      | _        t-        j.                  | j                        }|}|r| xj0                  dz  c_        | j*                  r| j*                  j2                  n2t5        j6                  t8        j:                  |t8        j:                        }|t8        j:                  k(  rt=        d      t8        j>                  5  t5        j@                  t9        jB                  | jD                        |t8        j:                  |      }d d d        | jG                         nt5        jH                         | _"        | j                  | j                   WtJ        | jD                  jL                  _'        t9        jB                  | jP                        | jD                  jL                  _)        |t        tT        |      | jD                  _+        | jD                  xjX                  t4        jZ                  z  c_,        t9        j\                  t8        j^                  |       | jD                  _)        i | _0        t        |tb              s7te        ||
|      | _3        | jf                  jh                  | jD                  _5        |Wtl        | jD                  _        tn        | jD                  _8        | jD                  xjX                  t4        jr                  z  c_,        |s| j*                  r| j*                  jt                  nt8        j:                  }tw        | j                  | j                        }| jy                  | j                         | j{                          t8        j>                  5  t5        j|                  t9        jB                  | jD                        ||t9        jB                  |jD                              }d d d        | jy                  d        | jG                         | xj0                  d	z  c_        |:t        | jD                  j                  | jD                  j                        | _        y y c c}w # 1 sw Y   0xY w# 1 sw Y   xY w)
Nzcannot construct base Containerz&Container cannot be directly extended.namez<none>r~   :r   z!Could not determine output format   )B_cinit_sentinelr#   
isinstancer   r   r=   r   getattrdictr,   container_optionsstream_optionshwaccelr:   r;   open_timeoutread_timeoutr?   r<   splitContainerFormatformatosfsencode_myflagoptrr8   av_guess_formatr   r7   
ValueErrornogilavformat_alloc_output_context2rV   ptrr   avformat_alloc_contextr'   interrupt_callbackcallbackinterrupt_callback_infor6   r   audio_codec_idr+   ro   r   p_voidrA   
basestringr>   rF   rB   r)   r0   rJ   	io_close2rw   iptr
Dictionaryset_timeoutstart_timeoutavformat_open_inputr   iformatoformat)selfsentinelfile_format_namer,   r   r   r   r:   r;   r?   r   r   r<   	writeablexacodecname_objr   ofmtresifmt	c_optionss                          r&   	__cinit__zContainer.__cinit__   s      ?*@AA!+D/!B	D.!AGHHeS!DIGE68<=DIGMr*!%&7&=2!>0>0D"E1tAwE!2.((&"k!&1&7&7&<#V)+6DK ++dii0& LLAL ;;   ((dFKKH 
 v{{" !DEE 88NN488,KK	 NN3 113DH   ,0A0A0M7C++45;^^006++2 !*1*f*E'#/// ++fmmT: %, Y?DI))--DHHK+DHH!.DHHHHNNc666N '+{{4;;##D"4<<1G1GHIT../  --NN488,dD&..:W T"NN3LLAL01A1A488CSCSTDK o FD Z s    $UAU4AUUUc                     t         j                  5  t        j                  | j                         d d d        y # 1 sw Y   y xY wr.   )r   r   r8   avformat_free_contextr   r   s    r&   __dealloc__zContainer.__dealloc__^  s0    \\ 	0%%dhh/	0 	0 	0s	    :Ac                     | S r.   r~   r   s    r&   	__enter__zContainer.__enter__b  s    r[   c                 $    | j                          y r.   )close)r   exc_typeexc_valexc_tbs       r&   __exit__zContainer.__exit__e  s    

r[   c                     t         r.   )NotImplementedErrorr   s    r&   r  zContainer.closeh  s    !!r[   c                 l    d| j                   j                   d| j                  xs | j                  dS )Nz<av. >)	__class__rl   rF   r   r   s    r&   __repr__zContainer.__repr__k  s0    dnn--.a		0FTYY/IKKr[   Fr   valuer   c                 0    t        || j                        S )N)filename)r   r   r   r  s     r&   r   zContainer.err_checkn  s     33r[   c           
          | j                          t               5 }t        j                  | j                  ddt        | t                     d d d        dj                  d D              S # 1 sw Y   !xY w)Nr   r4   c              3   &   K   | ]	  }|d      yw)r   Nr~   ).0logs     r&   	<genexpr>z)Container.dumps_format.<locals>.<genexpr>w  s     .#s1v.s   )_assert_open
LogCapturer8   av_dump_formatr   r   r   join)r   logss     r&   dumps_formatzContainer.dumps_formats  sb    \ 	STtxxB
40QR	Sww....	S 	Ss   1A,,A5c                 N    |d| j                   _        y || j                   _        y )Ng      )r   r   )r   r   s     r&   r   zContainer.set_timeouty  s#    ?37D((03:D((0r[   c                 J    t        j                         | j                  _        y r.   )r    r!   r   r"   r   s    r&   r   zContainer.start_timeout  s    26..2B$$/r[   c                 T    | j                   t        j                  k(  rt        d      y )NzContainer is not open)r   r   r7   AssertionErrorr   s    r&   r  zContainer._assert_open  s#    88v{{" !899 #r[   c                 N    | j                          | j                  j                  S r.   r  r   video_codec_idr   s    r&   r'  zContainer.video_codec_id  s    xx&&&r[   c                 F    | j                          || j                  _        y r.   r&  r  s     r&   r'  zContainer.video_codec_id  s    "'r[   c                 N    | j                          | j                  j                  S r.   r  r   r+   r   s    r&   r+   zContainer.flags  s    xx~~r[   c                 F    | j                          || j                  _        y r.   r*  r  s     r&   r+   zContainer.flags  s    r[   c                      | j                   dz  S )Nr   )r   r   s    r&   input_was_openedzContainer.input_was_opened  s    ||ar[   c                 @    | j                   i | _         | j                   S r.   )	_metadatar   s    r&   rT   zContainer.metadata  s     >>!DN~~r[   c                    | j                          g }t        | j                  j                        D ]  }| j                  j                  |   }|j                  |j                  |j                  |j                  t        t        j                  |j                              t        |j                  | j                  | j                         d        |S )N)idstartend	time_baserT   )r  rR   r   rS   rQ   appendr1  r2  r3  r   r   rV   r4  r   rT   r:   r;   )r   rN   rY   chs       r&   rQ   zContainer.chapters  s    txx++, 	A""1%BMM%%XX66!7r||8T!U .T%;%;T=Q=Q!
	 r[   c           
         | j                          t        |      }t        j                  5  t	        | j
                         d d d        |dk(  r1d| j
                  _        t        j                  | j
                  _        y t        j                  t        t        j                  |t        j                  t        j                  t        j                           z              }|t        j                  k(  rt!        d      t#        |      D ]i  }||   }t        j                  t        j                  t        j                     t        j                  t        j                  t        j                                    }|t        j                  k(  rt!        d      |d   |_        t        j                  t&        |d         |_        t        j                  t&        |d         |_        t-        |d   t        j.                  |j0                               t        j                  |_        d|v rBt5        t        j.                  |j2                        |d   | j6                  | j8                         |||<   l t        j                  t        j:                  |      | j
                  _        || j
                  _        y # 1 sw Y   }xY w)	Nr   zav_malloc failed for chapterszav_malloc failed for chapterr1  r2  r3  r4  rT   )r  lenr   r   rZ   r   rS   r7   rQ   r   AVChapterPtrPtrr8   	av_mallocsizeofr   	AVChapterMemoryErrorrR   r1  r   r2  r3  r   rV   r4  rT   r   r:   r;   uint)r   rQ   countch_arrayrY   entryr6  s          r&   set_chapterszContainer.set_chapters  s   #&x= \\ 	%488$	% A:#$DHH  &DHH;;MM%&--s}}0M"NNO
 v{{"=>>u 	AQKEs}}-fmmCMM:;B V[[ !"@AA$KBE{{7E'N;BH[[%,7BF%,fnnR\\.JK ++BKU"NN2;;/*%**((	 HQK)	,  &{{6;;>$M	% 	%s   J==KN)rl   rm   rn   r   r  r  r	  r  r  r   cfunc	exceptvalintr   r   r   r   r  propertyr'  setterr8   	AVCodecIDr+   r-  r   rT   rQ   rB  r~   r[   r&   r5   r5      sa   vUp0"L \\Vb&4vzz 4fjj 4 ' 4/ \\; ; \\C C \\: : ' ' (CMM ( (   \\6::       $  &/%r[   r5   c                    |(t        |t              r|dk(  s|dk(  st        d|       t        | t              rn#t        | t              r|  } n|t	        | dd      }|d}t        |	t
              rt        |	      dk(  st        d      |	\  }}n|	}|	}|j                  d      rt        t        | |||||||||||
      S |rt        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   tupler8  
startswithr   r   r   )rF   rL  r   r,   r   r   r:   r;   r?   r   r<   r   r   r   s                 r&   openrO    s.   z LZc2ts{dck@GHH$	D$		tVT*|'5!7|q OPP%,"ls
 	
  O
 	
  r[   )NNNNNzutf-8stricti   NNN)Br   r    enumr   r   pathlibr   r   cython.cimports.libavcimportslibavr8   !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<  r9  rC  r   rD  r   rE  r'   AVFormatContextAVIOContextp_const_charr9   r0   r/   rJ   rI   voidrZ   r]   r   cclassr5   rO  r~   r[   r&   <module>rh     s   	     # # : = ? S ? <  0 ' , &..2 &..s}}0M!NO FMM fjj    0 <~~c))*<v~~coo67< 
		< ::	<
 ^^FNN3+;+;<=< ZZ<   < )!~~c))*)!v~~coo67)! 
		)! ::	)!
 ^^FNN3+;+;<=)! ZZ)!  )!X (~~c))*(06s0O(ZZ(   ( ~~c))*06s0OZZ  2 s':':;     [D [ $( $(P F% F% F%V 
vr[   