
    QЦi=C                     L   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JrJrJr  S SKrS SKJrJrJrJr  SSKJr  \" S5      r\R0                  4S\S	\S
\S\S\4
S jjrSS\R6                  S\S\S\S\R6                  4
S jjr " S S5      rS\S\4S jr " S S5      rg)    NFraction)	AnyCallablecastDictListOptionalTupleTypeVarUnion)_probe_video_from_file_read_video_from_file
read_videoread_video_timestamps   )tqdmTptstimebase_fromtimebase_to
round_funcreturnc                 6    [        U S5      U-  U-  nU" U5      $ )zconvert pts between different time bases
Args:
    pts: presentation timestamp, float
    timebase_from: original timebase. Fraction
    timebase_to: new timebase. Fraction
    round_func: rounding function.
r   r   )r   r   r   r   new_ptss        _/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/torchvision/datasets/video_utils.pypts_convertr      s%     sA.<Gg    tensorsizestepdilationc                 &   U R                  5       S:w  a  [        SU R                  5        35      eU R                  S5      nU R                  5       nX$-  X4-  4nXSUS-
  -  S-   -
  U-  S-   U4nUS   S:  a  SU4n[        R
                  " XU5      $ )z
similar to tensor.unfold, but with the dilation
and specialized for 1d tensors

Returns all consecutive windows of `size` elements, with
`step` between windows. The distance between each element
in a window is given by `dilation`.
r   z*tensor should have 1 dimension instead of r   )dim
ValueErrorstridenumeltorch
as_strided)r   r    r!   r"   o_strider'   
new_stridenew_sizes           r   unfoldr-      s     zz|qEfjjl^TUU}}QHLLNE/8#67JdQh/!34=A4HH{Qt9Fj99r   c                   f    \ rS rSrSrS\\   SS4S jrS\4S jr	S\S\
\\   \\   4   4S	 jrS
rg)_VideoTimestampsDataset/   z
Dataset used to parallelize the reading of the timestamps
of a list of videos, given their paths in the filesystem.

Used in VideoClips and defined at top level, so it can be
pickled when forking.
video_pathsr   Nc                     Xl         g Nr1   )selfr1   s     r   __init__ _VideoTimestampsDataset.__init__8   s    &r   c                 ,    [        U R                  5      $ r3   lenr1   r5   s    r   __len___VideoTimestampsDataset.__len__;       4##$$r   idxc                 2    [        U R                  U   5      $ r3   )r   r1   )r5   r?   s     r   __getitem__#_VideoTimestampsDataset.__getitem__>   s    $T%5%5c%:;;r   r4   )__name__
__module____qualname____firstlineno____doc__r	   strr6   intr<   r   r
   floatrA   __static_attributes__ r   r   r/   r/   /   sO    'DI '$ '% %<s <uT#Y-G'H <r   r/   xc                     U $ )z@
Dummy collate function to be used with _VideoTimestampsDataset
rL   )rM   s    r   _collate_fnrO   B   s	     Hr   c                      \ rS rSrSr            S,S\\   S\S\S\\	   S\\
\\4      S	\S
\S\S\S\S\S\S\SS4S jjrS-S jrS\
\\4   SS4S jr\S\
\\4   4S j5       rS\\   SS 4S jr\ S.S\R(                  S\S\S\\	   S\\	   S\\R(                  \\\   \R(                  4   4   4S jj5       rS.S\S\S\\	   SS4S jjrS\4S jrS\4S  jrS\4S! jrS"\S\\\4   4S# jr\S\S$\	S%\	S\\\R(                  4   4S& j5       rS"\S\\R(                  \R(                  \
\\4   \4   4S' jrS\
\\4   4S( jr S)\
\\4   SS4S* jr!S+r"g)/
VideoClipsI   a  
Given a list of video files, computes all consecutive subvideos of size
`clip_length_in_frames`, where the distance between each subvideo in the
same video is defined by `frames_between_clips`.
If `frame_rate` is specified, it will also resample all the videos to have
the same frame rate, and the clips will refer to this frame rate.

Creating this instance the first time is time-consuming, as it needs to
decode all the videos in `video_paths`. It is recommended that you
cache the results after instantiation of the class.

Recreating the clips for different clip lengths is fast, and can be done
with the `compute_clips` method.

Args:
    video_paths (List[str]): paths to the video files
    clip_length_in_frames (int): size of a clip in number of frames
    frames_between_clips (int): step (in frames) between each clip
    frame_rate (float, optional): if specified, it will resample the video
        so that it has `frame_rate`, and then the clips will be defined
        on the resampled video
    num_workers (int): how many subprocesses to use for data loading.
        0 means that the data will be loaded in the main process. (default: 0)
    output_format (str): The format of the output video tensors. Can be either "THWC" (default) or "TCHW".
Nr1   clip_length_in_framesframes_between_clips
frame_rate_precomputed_metadatanum_workers_video_width_video_height_video_min_dimension_video_max_dimension_audio_samples_audio_channelsoutput_formatr   c                 :   Xl         X`l        Xpl        Xl        Xl        Xl        Xl        Xl        UR                  5       U l	        U R                  S;  a  [        SU S35      eUc  U R                  5         OU R                  U5        U R                  X#U5        g )N)THWCTCHWz5output_format should be either 'THWC' or 'TCHW', got .)r1   rW   rX   rY   rZ   r[   r\   r]   upperr^   r%   _compute_frame_pts_init_from_metadatacompute_clips)r5   r1   rS   rT   rU   rV   rW   rX   rY   rZ   r[   r\   r]   r^   s                 r   r6   VideoClips.__init__d   s    " '& )*$8!$8!,.*002%55TUbTccdeff (##%$$%:;0
Sr   c           
      0   / U l         / U l        SS KnUR                  R                  R                  [        U R                  5      SU R                  [        S9n[        [        U5      S9 nU H  nUR                  S5        [        [        U6 5      u  pVU Vs/ s H  oqR                  " XqR                   S9PM     nnU R                   R#                  U5        U R                  R#                  U5        M     S S S 5        g s  snf ! , (       d  f       g = f)Nr      )
batch_sizerW   
collate_fn)totalr   dtype)	video_pts	video_fpstorch.utils.datautilsdata
DataLoaderr/   r1   rW   rO   r   r:   updatelistzip	as_tensorlongextend)r5   r(   dlpbarbatch	batch_pts	batch_fpsr   s           r   rd   VideoClips._compute_frame_pts   s    &( 	 */++*:*:*E*E#D$4$45(("	 +F +
 B DA'+CK'8$	 PYYy__S

Cy	Y%%i0%%i0  !  Z ! s   ,.D#D=<DD
Dmetadatac                     US   U l         [        U R                   5      [        US   5      :X  d   eUS   U l        [        U R                   5      [        US   5      :X  d   eUS   U l        g )Nr1   ro   rp   )r1   r:   ro   rp   )r5   r   s     r   re   VideoClips._init_from_metadata   sm    #M24##$H[,A(BBBB!+.4##$H[,A(BBBB!+.r   c                 N    U R                   U R                  U R                  S.nU$ )Nr1   ro   rp   r   )r5   	_metadatas     r   r   VideoClips.metadata   s*      ++
	
 r   indicesc                    U Vs/ s H  o R                   U   PM     nnU Vs/ s H  o R                  U   PM     nnU Vs/ s H  o R                  U   PM     nnUUUS.n[        U 5      " UU R                  U R
                  U R                  UU R                  U R                  U R                  U R                  U R                  U R                  U R                  U R                  S9$ s  snf s  snf s  snf )Nr   )rS   rT   rU   rV   rW   rX   rY   rZ   r[   r\   r]   r^   )r1   ro   rp   type
num_framesr!   rU   rW   rX   rY   rZ   r[   r\   r]   r^   )r5   r   ir1   ro   rp   r   s          r   subsetVideoClips.subset   s    4;<Gq''*G<0781^^A&	80781^^A&	8&""

 Dz"&//!%"*((**,,!%!:!:!%!:!:.. 00,,
 	
 =88s   C*C/C4ro   r   r!   fpsc                    Uc  SnUc  Un[        U 5      U-  U-  n[        R                  [        [        R
                  " U5      5      X45      nX   n [        XU5      nUR                  5       (       d  [        R                  " S5        [        U[        5      (       a  U/[        U5      -  nXx4$ [        XaU5      nXx4$ )Nr   zThere aren't enough frames in the current video to get a clip for the given clip length and frames between clips. The video (and potentially others) will be skipped.)r:   rQ   _resample_video_idxrI   mathfloorr-   r'   warningswarn
isinstanceslice)	ro   r   r!   r   rU   total_frames_idxsclipsidxss	            r   compute_clips_for_video"VideoClips.compute_clips_for_video   s     ; CJ9~
2S8..s4::l3K/Lc^$	yd3{{}}MM\
 eU##7SZ'D { %T2D{r   c                    Xl         X l        X0l        / U l        / U l        [        U R                  U R                  5       HP  u  pEU R                  XAX%U5      u  pgU R                  R                  U5        U R                  R                  U5        MR     [        R                  " U R                   Vs/ s H  n[        U5      PM     sn5      n	U	R                  S5      R                  5       U l        gs  snf )aD  
Compute all consecutive sequences of clips from video_pts.
Always returns clips of size `num_frames`, meaning that the
last few frames in a video can potentially be dropped.

Args:
    num_frames (int): number of frames for the clip
    step (int): distance between two clips
    frame_rate (int, optional): The frame rate
r   N)r   r!   rU   r   resampling_idxsrw   ro   rp   r   appendr(   rx   r:   cumsumtolistcumulative_sizes)
r5   r   r!   rU   ro   r   r   r   vclip_lengthss
             r   rf   VideoClips.compute_clips   s     %	$
!!$..$..ANI66ydYcdKEJJe$  ''- B 

'C
1A
'CD , 3 3A 6 = = ? (Ds   3C4c                 "    U R                  5       $ r3   )	num_clipsr;   s    r   r<   VideoClips.__len__   s    ~~r   c                 ,    [        U R                  5      $ r3   r9   r;   s    r   
num_videosVideoClips.num_videos   r>   r   c                      U R                   S   $ )z:
Number of subclips that are available in the video list.
)r   r;   s    r   r   VideoClips.num_clips  s     $$R((r   r?   c                     [         R                  " U R                  U5      nUS:X  a  UnX#4$ XR                  US-
     -
  nX#4$ )z_
Converts a flattened representation of the indices into a video_idx, clip_idx
representation.
r   r   )bisectbisect_rightr   )r5   r?   	video_idxclip_idxs       r   get_clip_locationVideoClips.get_clip_location	  sU    
 ''(=(=sC	>H "" 229q=AAH""r   original_fpsnew_fpsc                    X-  nUR                  5       (       a  [        U5      n[        S S U5      $ [        R                  " U [        R
                  S9U-  nUR                  5       R                  [        R                  5      nU$ )Nrm   )	
is_integerrI   r   r(   arangefloat32r   toint64)r   r   r   r!   r   s        r   r   VideoClips._resample_video_idx  sg    %?? t9DtT**||Jemm<tCzz|u{{+r   c                    XR                  5       :  a   [        SU SU R                  5        S35      eU R                  U5      u  p#U R                  U   nU R                  U   U   nSSKJn  U" 5       nUS:X  a  U R                  S:w  a  [        S5      eU R                  S:w  a  [        S5      eU R                  S:w  a  [        S	5      eU R                  S:w  a  [        S
5      eU R                  S:w  a  [        S5      eUS:X  a7  US   R                  5       nUS   R                  5       n	[        XHU	5      u  pnGO[        U5      nUR                   nSn[#        [$        US   R                  5       5      n[#        [$        US   R                  5       5      nSu  nn['        SS5      n['        UR(                  R*                  UR(                  R,                  5      nUR.                  (       ax  ['        UR0                  R*                  UR0                  R,                  5      n[3        UUU[4        R6                  5      n[3        UUU[4        R8                  5      nUR:                  n[=        UU R                  U R                  U R                  U R                  UU4UU R                  U R>                  UU4US9u  pnSU0nUb  XS'   U R@                  bM  U RB                  U   U   n[E        U[F        RH                  5      (       a  UUS   -
  nU
U   n
U R@                  US'   [K        U
5      U RL                  :X  d    U
RN                   SU RL                   35       eU RP                  S:X  a  U
RS                  SSSS5      n
XX4$ )z
Gets a subclip from a list of videos.

Args:
    idx (int): index of the subclip. Must be between 0 and num_clips().

Returns:
    video (Tensor)
    audio (Tensor)
    info (Dict)
    video_idx (int): index of the video in `video_paths`
zIndex z out of range (z number of clips)r   )get_video_backendpyavz.pyav backend doesn't support _video_width != 0z/pyav backend doesn't support _video_height != 0z6pyav backend doesn't support _video_min_dimension != 0z6pyav backend doesn't support _video_max_dimension != 0z0pyav backend doesn't support _audio_samples != 0r   N)r   r   r   )
video_widthvideo_heightvideo_min_dimensionvideo_max_dimensionvideo_pts_rangevideo_timebaseaudio_samplesaudio_channelsaudio_pts_rangeaudio_timebaserp   	audio_fpsz x ra         )*r   
IndexErrorr   r1   r   torchvisionr   rX   r%   rY   rZ   r[   r\   itemr   r   rp   r   rI   r   r   	numeratordenominator	has_audior   r   r   r   ceilaudio_sample_rater   r]   rU   r   r   r(   Tensorr:   r   shaper^   permute)r5   r?   r   r   
video_pathclip_ptsr   backend	start_ptsend_ptsvideoaudioinfo_inforp   r   video_start_ptsvideo_end_ptsaudio_start_ptsaudio_end_ptsr   r   _resampling_idxs                           r   get_clipVideoClips.get_clip!  sM    ..""vcU/$..:J9KK\]^^"44S9	%%i0
::i(21#%f  A% !QRR!!Q& !RSS((A- !YZZ((A- !YZZ""a' !STTf ((*Irl'')G!+J7!KE$*:6EII"3(8(8(:;O hrl&7&7&9:M-2*O]%a^N%e&:&:&D&DeFZFZFfFfgN!)%*>*>*H*H%J^J^JjJj!k"-o~~_c_i_i"j +M>>[_[d[d e!33	3 --!//$($=$=$($=$=!0- @-"11#33!0- @-OE!  +D$$-[!??&!11)<XFN.%,,77!/.2C!C.)E $D5zT__,RS@Q.RR,'MM!Q1-ET,,r   c                    U R                    Vs/ s H  n[        U5      PM     nnU R                    Vs/ s H!  o3R                  [        R                  5      PM#     nnU(       a&  [        R
                  " U5      nUR                  5       nU R                  R                  5       nX%S'   XES'   US	 US	 US	 SUS'   U$ s  snf s  snf )Nvideo_pts_sizesro   r   r   r   r   _version)	ro   r:   r   r(   r   catnumpy__dict__copy)r5   r   r   rM   ro   ds         r   __getstate__VideoClips.__getstate__y  s    +/>>:>a3q6>:
 15?1TT%++&	?		),I ")I MM .
"+ gJ  ! *3 ;
 @s
   B=(Cr   c                    SU;  a  Xl         g [        R                  " US   [        R                  S9n[        R                  " X!S   SS9nUS	 X!S'   Xl         U R                  U R                  U R                  U R                  5        g )Nr   ro   rm   r   r   )r$   )	r   r(   rx   r   splitrf   r   r!   rU   )r5   r   ro   s      r   __setstate__VideoClips.__setstate__  ss    QMOOAkN%++F	KK	->+?QG	 "+4??DIItGr   )r   r]   r\   rY   r[   rZ   rX   r   r   rU   r   rW   r^   r   r!   rp   r1   ro   )ri   r   NNr   r   r   r   r   r   r   r`   )r   Nr3   )#rC   rD   rE   rF   rG   r	   rH   rI   r
   rJ   r   r   r6   rd   re   propertyr   r   staticmethodr(   r   r   r   r   r   rf   r<   r   r   r   r   r   r   r   rK   rL   r   r   rQ   rQ   I   s   : &($%&*:>$%$% ##T#Y#T  ##T "	#T
 UO#T  (S#X7#T #T #T #T "#T "#T #T #T #T 
#TJ14/DcN /t / $sCx.  
d3i 
L 
2 qu<<-08;BJ5/_ghm_n	u||U4;#<==	> 2@ @3 @HUO @_c @.   %C %)3 )
#S 
#U38_ 
# 	 	5 	5 	UZ[`bgbnbn[nUo 	 	V-C V-E%,,d3PS8nVY*Y$Z V-pd38n 8Hd38n H Hr   rQ   )r   ) r   r   r   	fractionsr   typingr   r   r   r   r	   r
   r   r   r   r(   torchvision.ior   r   r   r   rr   r   r   r   rI   r   r   r-   r/   rO   rQ   rL   r   r   <module>r      s        S S S  k k CL bfakak 	S 	 	 	V^ 	ps 	:5<< :s :# : :U\\ :(< <&1  ZH ZHr   