o
    #i
                     @   sp   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 ej	d 	d	ddZ
	d	deejee f fddZdS )
    N)print0torchc           	      C   s   t || }t|| }|d | d }|dur|}n| | d dk r%d}n
td| | d }tj|t | d || |dt}|S )a$  sample frames proportional to the length of the frames in one second
    e.g., 1s video has 30 frames, when 'fps'=3, we sample frames with spacing of 30/3=10
    return the frame indices

    Parameters
    ----------
    total_frames : length of the video
    video_fps : original fps of the video
    sample_num_frames : number of frames to sample
    sample_fps : the fps to sample frames
    start_index : the starting frame index. If it is not None, it will be used as the starting frame index  

    Returns
    -------
    frame indices
       Nr   )startstopnum)minroundrandomrandintnplinspaceastypeint)	total_frames	video_fpssample_num_frames
sample_fpsstart_indexintervalZframes_ranger   
frame_idxs r   5/data/cameron/vidgen/VidTok/vidtok/data/video_read.pysample_frames_with_fps   s   
r   returnc           	   
   C   s   t j| dd}t|}| }t|||||d}||}| d }|dddd}|jd |ks8t||krNt	d| d	|jd  d
t| d|  d	 ||fS )au  read frames from video path using decord

    Parameters
    ----------
    video_path : path to video
    sample_num_frames : number of frames to sample
    sample_fps : the fps to sample frames
    start_index : the starting frame index. If it is not None, it will be used as the starting frame index  

    Returns
    -------
    frames (tensor 0~1), frame indices
    r   )num_threads)r   r   r   r   r         r      z\[bold yellow]\[vidtok.data.video_read][read_frames_with_decord][/bold yellow] Warning: need z frames, but got z	 frames, z frame indices, video_path=.)
decordVideoReaderlenget_avg_fpsr   	get_batchfloatpermuteshaper   )	
video_pathr   r   r   video_readerr   r   r   framesr   r   r   read_frames_with_decord5   s,   


r+   )N)osr
   r    numpyr   r   vidtok.modules.utilr   bridge
set_bridger   tupleTensorlistr   r+   r   r   r   r   <module>   s    
-