
    QЦiiF                        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
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r\R,                  R/                  \R,                  R0                  5        \" \R4                  R6                  R8                  S5      (       d  \" S	5      r \R<                  rS0S jr!S\"4S jr#S q$Sq%      S1S\&S\RN                  S\(S\&S\\	\&\4      S\\RN                     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$\RN                  S%\
S"   S&\*S'\(S\RN                  4
S( jr,    S2S\&S)\\(\4   S*\\\(\4      S\&S+\&S\\RN                  \RN                  \	\&\4   4   4S, jjr-SSS\"4S- jr.SSS\
\*   4S. jr/S3S\&S\&S\\
\*   \\(   4   4S/ jjr0g! \ a    \R@                  r GN6f = f! \ a    \" S
5      r GNKf = f)4    N)Fraction)AnyDictListOptionalTupleUnion   )_log_api_usage_once   )
_video_opt	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     S/var/www/html/ai-image-ml/venv/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   0   s    "i(((r   
   filenamevideo_arrayfpsvideo_codecoptionsaudio_array	audio_fpsaudio_codecaudio_optionsc	                 <   [         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      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	R,                  R.                  S   R0                  R2                  n[        R4                  " X   5      n[         R                  " U5      R                  SS9R7                  U5      n[        R8                  R;                  X_US9nUUl        UR?                  U5       H  nU	RA                  U5        M     UR?                  5        H  nU	RA                  U5        M     U HR  n[        RB                  R;                  USS9n SUl"        U
R?                  U5       H  nU	RA                  U5        M     MT     U
R?                  5        H  nU	RA                  U5        M     SSS5        g! [F         a    SSK$J%n  URL                  Ul"         Nf = f! , (       d  f       g= f)a  
Writes a 4d tensor in [T, H, W, C] format in a video file.

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>`_.

.. warning::

    In the near future, we intend to centralize PyTorch's video decoding
    capabilities within the `torchcodec
    <https://github.com/pytorch/torchcodec>`_ project. We encourage you to
    try it out and share your feedback, as the torchvision video decoders
    will eventually be deprecated.

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)'torchjitis_scripting
is_tracingr   write_videor   	as_tensoruint8numpyr   float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   rE   rE   9   s   j 99!!##EII,@,@,B,BK(//+U[[AGGdGSK #uhhsm		$	%%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K&AL&!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collectintmathfloor	time_baserI   ceilwarningswarntypecodec_context	extradatafindresearchgroupmaxseekFFmpegError	enumeratedecodeptssortedleninsert)r^   ri   rj   rk   r_   rl   framesshould_buffermax_buffer_sizer   posdoseek_offsetbuffer_count_idxrf   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   rN   rz   )
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           
         [         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   R.                  nUR*                  R0                  (       a]  [3        UUUUUR*                  R0                  S   SS05      n	UR*                  R0                  S   R4                  nUb  [#        U5      US'   UR*                  R,                  (       aO  [3        UUUUUR*                  R,                  S   SS05      n
UR*                  R,                  S   R6                  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>                  " [@        RB                  " U5      5      nO#[         RD                  " S[         RF                  S9nU(       a  [@        RH                  " US5      n[         R>                  " U5      nUS:X  aY  [K        [L        RN                  " USU-  -  5      5      nU[#        S	5      :w  a%  [K        [L        RP                  " USU-  -  5      5      n[S        XzX5      nO#[         RD                  " S[         RT                  S9nUS:X  a  URW                  SSSS5      nXgU4$ ! , (       d  f       GN= f! [8         a     GNf = fs  snf s  snf )aB  
Reads a video from a file, returning both the video frames and the audio frames

.. warning::

    In the near future, we intend to centralize PyTorch's video decoding
    capabilities within the `torchcodec
    <https://github.com/pytorch/torchcodec>`_ project. We encourage you to
    try it out and share your feedback, as the torchvision video decoders
    will eventually be deprecated.

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: Nro   z7end_pts should be larger than start_pts, got start_pts=z and end_pts=ignoremetadata_errorsrq   	video_fpsrS   r#   )r   r   r   rr   r'   r   rn   )r   r   r   rr   r
   ),rA   rB   rC   rD   r   
read_videoupper
ValueErrortorchvisionr   ospathexistsRuntimeErrorr   _read_videor   rI   default_timebaser   rL   rR   rS   r}   rq   r   average_rater,   r   to_rgb
to_ndarrayrF   rJ   stackemptyrG   concatenaterz   r{   r|   r~   r   float32permute)r   r   r   rk   r   r   vframesr   infovideo_framesr   audio_timebaser^   r   rf   vframes_listaframes_lists                    r   r   r     sg   B 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=   O1 4D!OO1 !'PP
O.)O1 .O1 1
O?>O?c                 `    U R                   S   R                  R                  nUc  gSU;   a  gg)Nr   Fs   LavcT)rR   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   )rq   )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                 b   [         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R/                  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,  
List the video frames timestamps.

.. warning::

    In the near future, we intend to centralize PyTorch's video decoding
    capabilities within the `torchcodec
    <https://github.com/pytorch/torchcodec>`_ project. We encourage you to
    try it out and share your feedback, as the torchvision video decoders
    will eventually be deprecated.

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: rn   )rA   rB   rC   rD   r   read_video_timestampsr   r   r   _read_video_timestampsr   r   rL   rR   rq   r}   r   r   r   r   rI   r   RuntimeWarningsort)r   rk   r   r   r   r^   video_streamvideo_time_baseemsgr   s              r   r   r     sh   2 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   E2 !AE!#D8.E!E2 %F,8#EE!EE!!
E/+E2 /E2 2
F)<#F$$F))r   N)libx264NNNNN)r   Nr   r   )r   )1rx   r{   r   r   r   	fractionsr   typingr   r   r   r   r   r	   rH   rJ   rA   utilsr    r   r   logging	set_levelERRORhasattrrq   rf   r[   ImportErrorr   AttributeErrorAVErrorr   boolr   rv   rw   strTensorrI   rE   rz   r   r   r   r   r   r   r   r   r   <module>r      s+   	  	 	   : :   ' JJ))*288>>,,k::
!nn
)t )
   !(,*.!%!%.2u"u"u" 
u" 	u"
 d38n%u" %,,'u" u" #u" DcN+u" 
u"p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w  !jj! 		
Bs1    A'G. (G G+'G. *G++G. .H ?H 