
    ڧgA                        d dl Z d dlZd dlZd dlZd dlZd dlmZ d dlmZm	Z	m
Z
mZmZmZ d dlZd dlZddlmZ ddlmZ 	 d dlZej                            ej        j                    eej        j        j        d          s ed	          Zn# e$ r  ed
          ZY nw xY wd3dZdefdZ d a!da"	 	 	 	 	 	 d4de#dej$        de%de#dee	e#ef                  deej$                 dee%         dee#         dee	e#ef                  ddfdZ&ddde%de%de#dd d!e	e#eee'ee'd"f         e
e'         f                  f         de
d#         fd$Z(d%ej$        d&e
d#         d'e'd(e%dej$        f
d)Z)	 	 	 	 d5de#d,ee%ef         d-eee%ef                  de#d.e#deej$        ej$        e	e#ef         f         fd/Z*dddefd0Z+ddde
e'         fd1Z,d6de#de#dee
e'         ee%         f         fd2Z-dS )7    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                  H    t          t          t                    rt          d S N
isinstanceav	Exception     P/var/www/html/ai-engine/env/lib/python3.11/site-packages/torchvision/io/video.py_check_av_availabler   '   s#    "i    r   c                  8    t          t          t                     S r   r   r   r   r   _av_availabler   ,   s    "i((((r   
   libx264filenamevideo_arrayfpsvideo_codecoptionsaudio_array	audio_fpsaudio_codecaudio_optionsc	                    t           j                                        s2t           j                                        st	          t
                     t                       t          j        |t           j                  	                    d          }t          |t                    rt          j        |          }t          j        | d          5 }	|	                    ||          }
|j        d         |
_        |j        d         |
_        |d	k    rd
nd|
_        |pi |
_        |7ddddddddddd
}|	                    ||          }|pi |_        |j        d         }|dk    rdnd}|	j        j        d         j        j        }t          j        ||                   }t          j        |          	                    d                              |          }t          j                            |||          }||_        |                    |          D ]}|	                     |           |                                D ]}|	                     |           |D ]W}t          j!                            |d          }d|_"        |
                    |          D ]}|	                     |           X|
                                D ]}|	                     |           	 ddd           dS # 1 swxY w Y   dS )a  
    Writes a 4d tensor in [T, H, W, C] format in a video file

    .. 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
        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
    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)#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   )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imgs                       r   rE   rE   5   sO   F 9!!## )EI,@,@,B,B )K(((/+U[AAAGGdGSSK #u hsmm		$	$	$ 0"	%%k%<<"(+#)!,&1\&A&Aw B"# # !++Ki+HHH,2H&,Q/L'3a'7'788VL(06q9@E8$78H$IJJL/+66<<4<HHOOP\]]KM..{CS\h.iiE )E"//%00 & &f%%%%"//++ & &f%%%% 	& 	&CM..s7.CCE$EO --.. & &f%%%%& mmoo 	" 	"FMM&!!!!	"_0" 0" 0" 0" 0" 0" 0" 0" 0" 0" 0" 0" 0" 0" 0" 0" 0" 0"s   HK44K8;K8r\   zav.container.Containerstart_offset
end_offsetpts_unitr]   zav.stream.Streamstream_name.zav.frame.Framec                    t           dz  a t           t          z  t          dz
  k    rt          j                     |dk    rlt	          t          j        d|j        z  z                      t          d          k    r,t	          t          j	        d|j        z  z                      nt          j        d           i d}d}|j        dk    rx|j        j        }|rjd|v rf|                    d          }	||	d          }
t!          j        d	|
          }|t!          j        d
|
          }||                    d          dk    }}t'          |dz
  d          }|rt'          ||z
  d          }	 |                     |dd|           n# t*          j        $ r g cY S w xY wd}	 t/           | j        di |          D ]*\  }}||j        <   |j        k    r|r||k     r|dz  }( n+n# t*          j        $ r Y nw xY wfdt5                    D             }t7                    dk    rVdk    rPvrLfdD             }t7          |          dk    r+t'          |          }|                    d|                    |S )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]   c                 V    g | ]%}|         j         cxk    rk    n n|         &S r   pts).0irh   framesrg   s     r   
<listcomp>z%_read_from_stream.<locals>.<listcomp>   sE    ___A<6!9=3^3^3^3^T^3^3^3^3^3^fQi3^3^3^r   c                      g | ]
}|k     |S r   r   )rw   rx   rg   s     r   rz   z%_read_from_stream.<locals>.<listcomp>   s#    BBB!\1A1AA1A1A1Ar   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seekr   AVError	enumeratedecoderv   sortedleninsert)r\   rg   rh   ri   r]   rj   should_buffermax_buffer_sizer   posdoseek_offsetbuffer_count_idxrd   resultpreceding_framesfirst_frame_ptsry   s    ``                @r   _read_from_streamr      s    QM..2IA2MMM

5 4:la&:J6J&KLLMMu%%TYzQ9I5I'JKKLLJZ[[[FMO{g (2	  	3I--..))C#$$A	5q99AyI5q99} !

d 2KkAoq))K < +7;;{ed6RRRR:    			 L
$%5Y%5%D%D%D%DEE 	 	KD% %F59yJ&&  \O%C%C A%L	 '
 :    `________F
6{{Q<!++F0J0J CBBBvBBB  1$$!"233OMM!VO4555Ms%   6F F$#F$*AG0 0HHaframesaudio_frames	ref_startref_endc                     |d         j         |d         j         }}| j        d         }||z
  dz   |z  }d}|}	||k     rt          ||z
  |z            }||k    rt          ||z
  |z            }	| d d ||	f         S )Nr   r   )rv   rN   r   )
r   r   r   r   startendtotal_aframesstep_per_aframes_idxe_idxs
             r   _align_audio_framesr      s     a$l2&6&:3EM!$MU{Q-7OEEyY&/9::
W}}Ws]o566111eEk>""r   rv   THWC	start_ptsend_ptsoutput_formatc           
         t           j                                        s2t           j                                        st	          t
                     |                                }|dvrt          d| d          ddlm	}  |            dk    rNt          j                            |           st          d|            t          j        | |||          \  }}}nt!                       |t#          d	          }||k     rt          d
| d|           i }g }	g }
t          j        }	 t'          j        | d          5 }|j        j        r|j        j        d         j        }|j        j        rQt3          |||||j        j        d         ddi          }	|j        j        d         j        }|t#          |          |d<   |j        j        r@t3          |||||j        j        d         ddi          }
|j        j        d         j        |d<   ddd           n# 1 swxY w Y   n# t&          j        $ r Y nw xY wd |	D             }d |
D             }|r't          j        t=          j        |                    }n t          j         dt           j!                  }|rt=          j"        |d          }t          j        |          }|dk    ratG          tI          j%        |d|z  z                      }|t#          d	          k    r'tG          tI          j&        |d|z  z                      }tO          ||
||          }n t          j         dt           j(                  }|dk    r|)                    dddd          }|||fS )a  
    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)
    )r   TCHWz5output_format should be either 'THWC' or 'TCHW', got .r   get_video_backendpyavzFile not found: Nrm   z7end_pts should be larger than start_pts, got start_pts=z and end_pts=ignoremetadata_errorsro   	video_fpsrS   r$   c                 Z    g | ](}|                                                                 )S r   )to_rgb
to_ndarrayrw   rd   s     r   rz   zread_video.<locals>.<listcomp>S  s,    NNN1133NNNr   c                 6    g | ]}|                                 S r   )r   r   s     r   rz   zread_video.<locals>.<listcomp>T  s$    EEEu((**EEEr   )r   r   r   rp   r(   r   rl   )r   r   r   rp   r
   )*rA   rB   rC   rD   r   
read_videoupper
ValueErrortorchvisionr   ospathexistsRuntimeErrorr   _read_videor   rI   default_timebaser   rL   rR   rS   r   ro   r   average_rater-   r   rF   rJ   stackemptyrG   concatenater   r   r   r   r   float32permute)r   r   r   ri   r   r   vframesr   infovideo_framesr   audio_timebaser\   r   vframes_listaframes_lists                   r   r   r      s   B 9!!## (EI,@,@,B,B (J'''!''))M,,,aQ^aaabbb------f$$w~~h'' 	><(<<===!+!7)WV^!_!_$$?EllGYk)kkbikk   #4	8<<< H	$* J%.%6%<Q%?%IN$* =#4!! !)/2 !$ $L !* 1 7 : GI ,,1),<,<[)$* 	H#4!! !)/2 !$ $L )2(9(?(B(GD%5H H H H H H H H H H H H H H H8 z 	 	 	D	 ONNNNEEEEE 	Cobh|&<&<==GGk,ekBBBG 		?n\155Gog..G5  
9N8J+K L LMM	eEll**!$)Gq>7I,J"K"KLLG)'<GTTGGk&>>>G//!Q1--GT!!s7   +H& CHH& HH& !H"H& &H87H8c                 H    | j         d         j        j        }|dS d|v rdS dS )Nr   Fs   LavcT)rR   r   r   )r\   r   s     r   !_can_read_timestamps_from_packetsr   m  s5    !!$2<Iu)t5r   c                     t          |           r d |                     d          D             S d |                     d          D             S )Nc                 *    g | ]}|j         	|j         S r   ru   rw   xs     r   rz   z,_decode_video_timestamps.<locals>.<listcomp>y  s!    MMM!15;L;L;L;Lr   r   )ro   c                 *    g | ]}|j         	|j         S r   ru   r   s     r   rz   z,_decode_video_timestamps.<locals>.<listcomp>{  s!    NNN!AE<M<M<M<Mr   )r   demuxr   )r\   s    r   _decode_video_timestampsr   v  sZ    (33 OMMyQ77MMMMNNy//a/88NNNNr   c                 :  	 t           j                                        s2t           j                                        st	          t
                     ddlm}  |            dk    rt          j	        | |          S t                       d}g }	 t          j        | d          5 }|j        j        rj|j        j        d         }|j        		 t!          |          }n,# t          j        $ r t%          j        d|             Y nw xY wt)          |j                  }ddd           n# 1 swxY w Y   n># t          j        $ r,}d|  d	| }t%          j        |t,                     Y d}~nd}~ww xY w|                                 |d
k    r	fd|D             }||fS )ap  
    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: rl   c                     g | ]}|z  S r   r   )rw   r   video_time_bases     r   rz   z)read_video_timestamps.<locals>.<listcomp>  s    000qq?"000r   )rA   rB   rC   rD   r   read_video_timestampsr   r   r   _read_video_timestampsr   r   rL   rR   ro   r   r   r   r   r   rI   r   RuntimeWarningsort)
r   ri   r   r   rv   r\   video_streamemsgr   s
            @r   r   r   ~  s   2 9!!## 3EI,@,@,B,B 31222------f$$08DDDI
C+WXx888 	=I & =(06q9"."8Q29==CCz Q Q QM"OX"O"OPPPPPQ!,";<<		= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= : + + +KhKKKKc>********+ HHJJJ50000C000	>s`   D3 $&D'CD'&DD'DD'D3 'D++D3 .D+/D3 3E."E))E.)r   N)r   NNNNN)r   Nrv   r   ru   ).r~   r   r   r   r   	fractionsr   typingr   r   r   r   r   r	   rH   rJ   rA   utilsr    r   r   logging	set_levelERRORhasattrro   rd   r[   ImportErrorr   boolr   r|   r}   strTensorrI   rE   r   r   r   r   r   r   r   r   r   r   <module>r      sO   				  				 				        : : : : : : : : : : : : : : : :      ' ' ' ' ' '      IIIJ)***728>,k:: 	
[
 
    		
 
BBB   
)t ) ) ) )
   !(,*.!%!%.2]" ]"]"]" 
]" 	]"
 d38n%]" %,']" ]" #]" DcN+]" 
]" ]" ]" ]"@M'MM M 	M
 M c8E#uS#XS	*I$JKKLM 

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