
    hG                        S SK r S SKrS SKrS SKrS SKrS SKJr  S SKJrJ	r	J
r
  S SKrS SKrSSKJr  SSKJr  SSKJr   S SKr\R*                  R-                  \R*                  R.                  5        \" \R2                  R4                  R6                  S	5      (       d  \" S
5      r \R:                  rS1S jr S\!4S jr"S q#Sq$      S2S\%S\RL                  S\'S\%S\	\(\%\4      S\	\RL                     S\	\'   S\	\%   S\	\(\%\4      SS4S jjr)SSS\'S\'S\%SS S!\(\%\	\
\*\+\*S"4   \,\*   4      4   S\,S#   4S$ jr-S%\RL                  S&\,S#   S'\*S(\'S\RL                  4
S) jr.    S3S\%S*\
\'\4   S+\	\
\'\4      S\%S,\%S\+\RL                  \RL                  \(\%\4   4   4S- jjr/SSS\!4S. jr0SSS\,\*   4S/ jr1S4S\%S\%S\+\,\*   \	\'   4   4S0 jjr2g! \ a    \R>                  r GN6f = f! \ a    \" S5      r GNKf = f)5    N)Fraction)AnyOptionalUnion   )_log_api_usage_once   )
_video_opt) _raise_video_deprecation_warning	pict_typea+  Your version of PyAV is too old for the necessary video operations in torchvision.
If you are on Python 3.5, you will have to build from source (the conda-forge
packages are not up-to-date).  See
https://github.com/mikeboers/PyAV#installation for instructions on how to
install PyAV on your system.
zPyAV is not installed, and is necessary for the video operations in torchvision.
See https://github.com/mikeboers/PyAV#installation for instructions on how to
install PyAV on your system.
returnc                  B    [        [        [        5      (       a  [        eg N
isinstanceav	Exception     L/var/www/auris/envauris/lib/python3.13/site-packages/torchvision/io/video.py_check_av_availabler   ,   s    "i   !r   c                  4    [        [        [        5      (       + $ r   r   r   r   r   _av_availabler   1   s    "i(((r   
   filenamevideo_arrayfpsvideo_codecoptionsaudio_array	audio_fpsaudio_codecaudio_optionsc	                 b   [        5         [        R                  R                  5       (       d2  [        R                  R	                  5       (       d  [        [        5        [        5         [        R                  " U[        R                  S9R                  SS9n[        U[        5      (       a  [        [        R                  " U5      5      n[         R"                  " U SS9 n	U	R%                  X2S9n
UR&                  S   U
l        UR&                  S   U
l        US	:w  a  S
OSU
l        U=(       d    0 U
l        UGb2  SSSSSSSSSSS.
nU	R%                  XvS9nU=(       d    0 Ul        UR&                  S   nUS:  a  SOSnU	R0                  R2                  S   R4                  R6                  n[        R8                  " X   5      n[        R                  " U5      R                  SS9R;                  U5      n[         R<                  R?                  X_US9nUUl         URC                  U5       H  nU	RE                  U5        M     URC                  5        H  nU	RE                  U5        M     U HR  n[         RF                  R?                  USS9n SUl$        U
RC                  U5       H  nU	RE                  U5        M     MT     U
RC                  5        H  nU	RE                  U5        M     SSS5        g! [J         a    SSK&J'n  URP                  Ul$         Nf = f! , (       d  f       g= f)a  
[DEPRECATED] Writes a 4d tensor in [T, H, W, C] format in a video file.

.. warning::

    DEPRECATED: All the video decoding and encoding capabilities of torchvision
    are deprecated from version 0.22 and will be removed in version 0.24.  We
    recommend that you migrate to
    `TorchCodec <https://github.com/pytorch/torchcodec>`__, where we'll
    consolidate the future decoding/encoding capabilities of PyTorch

This function relies on PyAV (therefore, ultimately FFmpeg) to encode
videos, you can get more fine-grained control by referring to the other
options at your disposal within `the FFMpeg wiki
<http://trac.ffmpeg.org/wiki#Encoding>`_.

Args:
    filename (str): path where the video will be saved
    video_array (Tensor[T, H, W, C]): tensor containing the individual frames,
        as a uint8 tensor in [T, H, W, C] format
    fps (Number): video frames per second
    video_codec (str): the name of the video codec, i.e. "libx264", "h264", etc.
    options (Dict): dictionary containing options to be passed into the PyAV video stream.
        The list of options is codec-dependent and can all
        be found from `the FFMpeg wiki <http://trac.ffmpeg.org/wiki#Encoding>`_.
    audio_array (Tensor[C, N]): tensor containing the audio, where C is the number of channels
        and N is the number of samples
    audio_fps (Number): audio sample rate, typically 44100 or 48000
    audio_codec (str): the name of the audio codec, i.e. "mp3", "aac", etc.
    audio_options (Dict): dictionary containing options to be passed into the PyAV audio stream.
        The list of options is codec-dependent and can all
        be found from `the FFMpeg wiki <http://trac.ffmpeg.org/wiki#Encoding>`_.

Examples::
    >>> # Creating libx264 video with CRF 17, for visually lossless footage:
    >>>
    >>> from torchvision.io import write_video
    >>> # 1000 frames of 100x100, 3-channel image.
    >>> vid = torch.randn(1000, 100, 100, 3, dtype = torch.uint8)
    >>> write_video("video.mp4", options = {"crf": "17"})

dtypeT)forcew)mode)rater   r	   
libx264rgbyuv420prgb24Nz<f8z<f4z<i2z<i4u1)
dbldblpfltfltps16s16ps32s32pu8u8pr   stereomono)formatlayout)r;   NONE)PictureType))r   torchjitis_scripting
is_tracingr   write_videor   	as_tensoruint8numpyr   floatintnproundr   open
add_streamshapewidthheightpix_fmtr   streamsaudior;   namer&   astype
AudioFramefrom_ndarraysample_rateencodemux
VideoFramer   	TypeErrorav.video.framer>   r=   )r   r   r   r   r   r    r!   r"   r#   	containerstreamaudio_format_dtypesa_streamnum_channelsaudio_layoutaudio_sample_fmtformat_dtypeframepacketimgr>   s                        r   rC   rC   :   s   j %&99!!##EII,@,@,B,BK(//+U[[AGGdGSK #u"((3- 		$	%%k%<"((+#))!,&1\&Aw B"# !++K+HH,2H&,,Q/L'3a'78VL(0066q9@@EE88$7$IJL//+6<<4<HOOP\]KMM..{\h.iE )E"//%0f% 1 #//+f% , CMM..s7.CE3"( !--.f% /  mmoFMM&! &k 
%	$X  36"-"2"23Y 
%	$s2   #F0L K9AL 9!LL LL  
L.r]   zav.container.Containerstart_offset
end_offsetpts_unitr^   zav.stream.Streamstream_name.zav.frame.Framec                 v   [         S-  q [         [        -  [        S-
  :X  a  [        R                  " 5         US:X  an  [	        [
        R                  " USUR                  -  -  5      5      nU[        S5      :w  a/  [	        [
        R                  " USUR                  -  -  5      5      nO[        R                  " S5        0 nSnSnUR                  S:X  a  UR                  R                  n	U	(       ac  SU	;   a]  U	R                  S5      n
XS  n[         R"                  " S	U5      nUc  [         R"                  " S
U5      nUb  UR%                  S5      S:H  nUn['        US-
  S5      nU(       a  ['        X-
  S5      n U R)                  USSUS9  Sn [-        U R.                  " S0 UD65       H:  u  nnUUUR0                  '   UR0                  U:  d  M'  U(       a  X:  a  US-  nM:    O    [3        U5       Vs/ s H'  nXU   R0                  s=::  a  U::  d  M  O  M"  UU   PM)     nn[5        U5      S:  aT  US:  aN  X;  aI  U Vs/ s H  nUU:  d  M  UPM     nn[5        U5      S:  a   ['        U5      nUR7                  SUU   5        U$ ! [*         a    / s $ f = f! [*         a     Nf = fs  snf s  snf )Nr	   secinfzBThe pts_unit 'pts' gives wrong results. Please use pts_unit 'sec'.T   videos   DivXs   DivX(\d+)Build(\d+)(\w)s   DivX(\d+)b(\d+)(\w)      pr   F)	any_framebackwardr^   r   )_CALLED_TIMES_GC_COLLECTION_INTERVALgccollectrH   mathfloor	time_baserG   ceilwarningswarntypecodec_context	extradatafindresearchgroupmaxseekFFmpegError	enumeratedecodeptssortedleninsert)r]   rh   ri   rj   r^   rk   framesshould_buffermax_buffer_sizer   posdoseek_offsetbuffer_count_idxre   iresultpreceding_framesfirst_frame_ptss                        r   _read_from_streamr      s    QM..2IA2MM


5 4::la&:J:J6J&KLMu%TYYzQ9I9I5I'JKLJZ[FMO{{g ((22	 I-..)C$A		5q9AyII5q9} !
d 2KkAoq)K +7;{ed6R
 L
$Y%5%5%D%DEKD% %F599yyJ& \%C A%L F "(_A<!9==3^T^3^i3^ifQiF_
6{Q<!+0J (.Bv!\1AAvB 1$!"23OMM!VO45M5   	   `
 CsN   >J >J! J! *J! :J1J1	J1
J6J6JJ!
J.-J.aframesaudio_frames	ref_startref_endc                     US   R                   US   R                   pTU R                  S   nXT-
  S-   U-  nSnUn	XB:  a  [        X$-
  U-  5      nXS:  a  [        X5-
  U-  5      n	U S S 2X24   $ )Nr   r	   )r   rM   rH   )
r   r   r   r   startendtotal_aframesstep_per_aframes_idxe_idxs
             r   _align_audio_framesr     s     a$$l2&6&:&:3MM!$M{Q-7OEEY&/9:
}W]o561ek>""r   	start_ptsend_ptsoutput_formatc           
      ,   [        5         [        R                  R                  5       (       d2  [        R                  R	                  5       (       d  [        [        5        UR                  5       nUS;  a  [        SU S35      eSSK	J
n  U" 5       S:w  aN  [        R                  R                  U 5      (       d  [        SU  35      e[        R                   " XX#5      u  pgnGO[#        5         Uc  [%        S	5      nX!:  a  [        S
U SU 35      e0 n/ n	/ n
[        R&                  n [(        R*                  " U SS9 nUR,                  R.                  (       a#  UR,                  R.                  S   R0                  nUR,                  R2                  (       a]  [5        UUUUUR,                  R2                  S   SS05      n	UR,                  R2                  S   R6                  nUb  [%        U5      US'   UR,                  R.                  (       aO  [5        UUUUUR,                  R.                  S   SS05      n
UR,                  R.                  S   R8                  US'   SSS5        U	 Vs/ s H   oR=                  5       R?                  5       PM"     nnU
 Vs/ s H  oR?                  5       PM     nnU(       a+  [        R@                  " [B        RD                  " U5      5      nO#[        RF                  " S[        RH                  S9nU(       a  [B        RJ                  " US5      n[        R@                  " U5      nUS:X  aY  [M        [N        RP                  " USU-  -  5      5      nU[%        S	5      :w  a%  [M        [N        RR                  " USU-  -  5      5      n[U        XzX5      nO#[        RF                  " S[        RV                  S9nUS:X  a  URY                  SSSS5      nXgU4$ ! , (       d  f       GN= f! [:         a     GNf = fs  snf s  snf )ar  [DEPRECATED] Reads a video from a file, returning both the video frames and the audio frames

.. warning::

    DEPRECATED: All the video decoding and encoding capabilities of torchvision
    are deprecated from version 0.22 and will be removed in version 0.24.  We
    recommend that you migrate to
    `TorchCodec <https://github.com/pytorch/torchcodec>`__, where we'll
    consolidate the future decoding/encoding capabilities of PyTorch

Args:
    filename (str): path to the video file. If using the pyav backend, this can be whatever ``av.open`` accepts.
    start_pts (int if pts_unit = 'pts', float / Fraction if pts_unit = 'sec', optional):
        The start presentation time of the video
    end_pts (int if pts_unit = 'pts', float / Fraction if pts_unit = 'sec', optional):
        The end presentation time
    pts_unit (str, optional): unit in which start_pts and end_pts values will be interpreted,
        either 'pts' or 'sec'. Defaults to 'pts'.
    output_format (str, optional): The format of the output video tensors. Can be either "THWC" (default) or "TCHW".

Returns:
    vframes (Tensor[T, H, W, C] or Tensor[T, C, H, W]): the `T` video frames
    aframes (Tensor[K, L]): the audio frames, where `K` is the number of channels and `L` is the number of points
    info (Dict): metadata for the video and audio. Can contain the fields video_fps (float) and audio_fps (int)
)THWCTCHWz5output_format should be either 'THWC' or 'TCHW', got .r   get_video_backendpyavzFile not found: Nrn   z7end_pts should be larger than start_pts, got start_pts=z and end_pts=ignoremetadata_errorsrp   	video_fpsrR   r!   )r   r	   r	   rq   r%   r	   rm   )r	   r   r   rq   r   )-r   r?   r@   rA   rB   r   
read_videoupper
ValueErrortorchvisionr   ospathexistsRuntimeErrorr
   _read_videor   rG   default_timebaser   rK   rQ   rR   r{   rp   r   average_rater*   r   to_rgb
to_ndarrayrD   rI   stackemptyrE   concatenaterH   ry   rz   r|   r   float32permute)r   r   r   rj   r   r   vframesr   infovideo_framesr   audio_timebaser]   r   re   vframes_listaframes_lists                    r   r   r     sm   @ %&99!!##EII,@,@,B,BJ'!'')M,,PQ^P__`abb-f$ww~~h''!1(<==!+!7!7W!_$?ElGI)Tabiajk  #44	8<	$$**%.%6%6%<%<Q%?%I%IN$$**#4!! !))//2 !$L !* 1 1 7 7 : G GI ,,1),<[)$$**#4!! !))//2 !$L )2(9(9(?(?(B(G(GD%5 =@ BNN113N8DEu((*Eoobhh|&<=Gkk,ekkBGnn\15Goog.G5 

9N8J+K LM	eEl*!$))Gq>7I,J"KLG)'TGkk&>G//!Q1-T!!o =<8  		 OEs=   )O; >D!O)O; +'PP)
O83O; 8O; ;
P	P	c                 `    U R                   S   R                  R                  nUc  gSU;   a  gg)Nr   Fs   LavcT)rQ   r   r   )r]   r   s     r   !_can_read_timestamps_from_packetsr     s5    !!!$22<<I)r   c                    [        U 5      (       a8  U R                  SS9 Vs/ s H  oR                  c  M  UR                  PM     sn$ U R                  SS9 Vs/ s H  oR                  c  M  UR                  PM     sn$ s  snf s  snf )Nr   )rp   )r   demuxr   r   )r]   xs     r   _decode_video_timestampsr     sq    (33(Q7M7!557MM(//a/8N8!EE8NN NNs   BBB.Bc                 v   [        5         [        R                  R                  5       (       d2  [        R                  R	                  5       (       d  [        [        5        SSKJn  U" 5       S:w  a  [        R                  " X5      $ [        5         Sn/ n [        R                  " U SS9 nUR                  R                  (       aF  UR                  R                  S   nUR                   n [#        U5      n[+        UR,                  5      nSSS5        UR1                  5         US
:X  a  U V
s/ s H  oW-  PM	     nn
XC4$ ! [$         a    [&        R(                  " SU  35         Nof = f! , (       d  f       Nd= f! [$         a-  nSU  S	U 3n	[&        R(                  " U	[.        5         SnANSnAff = fs  sn
f )a\  [DEPREACTED] List the video frames timestamps.

.. warning::

    DEPRECATED: All the video decoding and encoding capabilities of torchvision
    are deprecated from version 0.22 and will be removed in version 0.24.  We
    recommend that you migrate to
    `TorchCodec <https://github.com/pytorch/torchcodec>`__, where we'll
    consolidate the future decoding/encoding capabilities of PyTorch

Note that the function decodes the whole video frame-by-frame.

Args:
    filename (str): path to the video file
    pts_unit (str, optional): unit in which timestamp values will be returned
        either 'pts' or 'sec'. Defaults to 'pts'.

Returns:
    pts (List[int] if pts_unit = 'pts', List[Fraction] if pts_unit = 'sec'):
        presentation timestamps for each one of the frames in the video.
    video_fps (float, optional): the frame rate for the video

r   r   r   Nr   r   z Failed decoding frames for file zFailed to open container for z; Caught error: rm   )r   r?   r@   rA   rB   r   read_video_timestampsr   r   r
   _read_video_timestampsr   r   rK   rQ   rp   r{   r   r   r}   r~   rG   r   RuntimeWarningsort)r   rj   r   r   r   r]   video_streamvideo_time_baseemsgr   s              r   r   r     sn   0 %&99!!##EII,@,@,B,B12-f$00DDI
C+WWXx8I  &&(0066q9"."8"8Q29=C ",";";<	 9 HHJ5,/0Cq?"C0> # QMM$DXJ"OPQ 98  +-hZ7GsKc>**+ 1sa   E< +AE+-E8E+E< /F6#E(%E+'E((E++
E95E< 9E< <
F3#F..F3)r   N)libx264NNNNN)r   Nr   r   )r   )3rw   ry   r   r   r}   	fractionsr   typingr   r   r   rF   rI   r?   utilsr    r
   _video_deprecation_warningr   r   logging	set_levelERRORhasattrrp   re   rZ   ImportErrorr   AttributeErrorAVErrorr   boolr   ru   rv   strTensorrG   dictrC   rH   tuplelistr   r   r   r   r   r   r   r   r   <module>r      s.   	  	 	   ' '   '  HJJ))*288>>,,k::
!nn
)t )
   !(,*.!%!%.2v"v"v" 
v" 	v"
 d38n%v" %,,'v" v" #v" DcN+v" 
v"rM'MM M 	M
 M c8E#uS#XS	*I$JKKLM 

M`#\\#)-.>)?#LO#Z_#
\\#" )*04v"v"UH_%v" eE8O,-v" 	v"
 v" 5<<tCH~56v"r1I d O(@ OT#Y O8C 83 85cT\]bTcIcCd 8y  !jj! 		
Bs1    A'G. (G G+'G. *G++G. .H ?H 