1818 _add_video_stream ,
1919 create_from_file ,
2020 get_frames_at_indices ,
21+ get_frames_by_pts ,
2122 get_json_metadata ,
2223 get_next_frame ,
2324 scan_all_streams_to_update_metadata ,
@@ -37,47 +38,51 @@ def get_frames_from_video(self, video_file, pts_list):
3738 pass
3839
3940
40- class DecordNonBatchDecoderAccurateSeek (AbstractDecoder ):
41+ class DecordAccurate (AbstractDecoder ):
4142 def __init__ (self ):
4243 import decord # noqa: F401
4344
4445 self .decord = decord
45-
46- self ._print_each_iteration_time = False
46+ self .decord .bridge .set_bridge ("torch" )
4747
4848 def get_frames_from_video (self , video_file , pts_list ):
49- self .decord .bridge .set_bridge ("torch" )
5049 decord_vr = self .decord .VideoReader (video_file , ctx = self .decord .cpu ())
5150 frames = []
52- times = []
5351 fps = decord_vr .get_avg_fps ()
5452 for pts in pts_list :
55- start = timeit .default_timer ()
5653 decord_vr .seek_accurate (int (pts * fps ))
5754 frame = decord_vr .next ()
58- end = timeit .default_timer ()
59- times .append (round (end - start , 3 ))
6055 frames .append (frame )
61- if self ._print_each_iteration_time :
62- print ("decord times=" , times , sum (times ))
6356 return frames
6457
6558 def get_consecutive_frames_from_video (self , video_file , numFramesToDecode ):
66- self .decord .bridge .set_bridge ("torch" )
6759 decord_vr = self .decord .VideoReader (video_file , ctx = self .decord .cpu ())
6860 frames = []
69- times = []
7061 for _ in range (numFramesToDecode ):
71- start = timeit .default_timer ()
7262 frame = decord_vr .next ()
73- end = timeit .default_timer ()
74- times .append (round (end - start , 3 ))
7563 frames .append (frame )
76- if self ._print_each_iteration_time :
77- print ("decord times=" , times , sum (times ))
7864 return frames
7965
8066
67+ class DecordAccurateBatch (AbstractDecoder ):
68+ def __init__ (self ):
69+ import decord # noqa: F401
70+
71+ self .decord = decord
72+ self .decord .bridge .set_bridge ("torch" )
73+
74+ def get_frames_from_video (self , video_file , pts_list ):
75+ decord_vr = self .decord .VideoReader (video_file , ctx = self .decord .cpu ())
76+ average_fps = decord_vr .get_avg_fps ()
77+ indices_list = [int (pts * average_fps ) for pts in pts_list ]
78+ return decord_vr .get_batch (indices_list )
79+
80+ def get_consecutive_frames_from_video (self , video_file , numFramesToDecode ):
81+ decord_vr = self .decord .VideoReader (video_file , ctx = self .decord .cpu ())
82+ indices_list = list (range (numFramesToDecode ))
83+ return decord_vr .get_batch (indices_list )
84+
85+
8186class TorchVision (AbstractDecoder ):
8287 def __init__ (self , backend ):
8388 self ._backend = backend
@@ -87,47 +92,63 @@ def __init__(self, backend):
8792 self .torchvision = torchvision
8893
8994 def get_frames_from_video (self , video_file , pts_list ):
90- start = timeit .default_timer ()
9195 self .torchvision .set_video_backend (self ._backend )
9296 reader = self .torchvision .io .VideoReader (video_file , "video" )
93- create_done = timeit .default_timer ()
9497 frames = []
9598 for pts in pts_list :
9699 reader .seek (pts )
97100 frame = next (reader )
98101 frames .append (frame ["data" ].permute (1 , 2 , 0 ))
99- frames_done = timeit .default_timer ()
100- if self ._print_each_iteration_time :
101- create_duration = 1000 * round (create_done - start , 3 )
102- frames_duration = 1000 * round (frames_done - create_done , 3 )
103- total_duration = 1000 * round (frames_done - start , 3 )
104- print (f"TV: { create_duration = } { frames_duration = } { total_duration = } " )
105102 return frames
106103
107104 def get_consecutive_frames_from_video (self , video_file , numFramesToDecode ):
108- start = timeit .default_timer ()
109105 self .torchvision .set_video_backend (self ._backend )
110106 reader = self .torchvision .io .VideoReader (video_file , "video" )
111- create_done = timeit .default_timer ()
112107 frames = []
113108 for _ in range (numFramesToDecode ):
114109 frame = next (reader )
115110 frames .append (frame ["data" ].permute (1 , 2 , 0 ))
116- frames_done = timeit .default_timer ()
117-
118- if self ._print_each_iteration_time :
119- create_duration = 1000 * round (create_done - start , 3 )
120- frames_duration = 1000 * round (frames_done - create_done , 3 )
121- total_duration = 1000 * round (frames_done - start , 3 )
122- print (
123- f"TV: consecutive: { create_duration = } { frames_duration = } { total_duration = } { frames [0 ].shape = } "
124- )
125111 return frames
126112
127113
128114class TorchCodecCore (AbstractDecoder ):
129115 def __init__ (self , num_threads = None , color_conversion_library = None , device = "cpu" ):
130- self ._print_each_iteration_time = False
116+ self ._num_threads = int (num_threads ) if num_threads else None
117+ self ._color_conversion_library = color_conversion_library
118+ self ._device = device
119+
120+ def get_frames_from_video (self , video_file , pts_list ):
121+ decoder = create_from_file (video_file )
122+ scan_all_streams_to_update_metadata (decoder )
123+ _add_video_stream (
124+ decoder ,
125+ num_threads = self ._num_threads ,
126+ color_conversion_library = self ._color_conversion_library ,
127+ )
128+ metadata = json .loads (get_json_metadata (decoder ))
129+ best_video_stream = metadata ["bestVideoStreamIndex" ]
130+ frames , * _ = get_frames_by_pts (
131+ decoder , stream_index = best_video_stream , timestamps = pts_list
132+ )
133+ return frames
134+
135+ def get_consecutive_frames_from_video (self , video_file , numFramesToDecode ):
136+ decoder = create_from_file (video_file )
137+ _add_video_stream (
138+ decoder ,
139+ num_threads = self ._num_threads ,
140+ color_conversion_library = self ._color_conversion_library ,
141+ )
142+
143+ frames = []
144+ for _ in range (numFramesToDecode ):
145+ frame = get_next_frame (decoder )
146+ frames .append (frame )
147+
148+ return frames
149+
150+ class TorchCodecCoreNonBatch (AbstractDecoder ):
151+ def __init__ (self , num_threads = None , color_conversion_library = None , device = "cpu" ):
131152 self ._num_threads = int (num_threads ) if num_threads else None
132153 self ._color_conversion_library = color_conversion_library
133154 self ._device = device
@@ -140,49 +161,28 @@ def get_frames_from_video(self, video_file, pts_list):
140161 color_conversion_library = self ._color_conversion_library ,
141162 device = self ._device ,
142163 )
164+
143165 frames = []
144- times = []
145166 for pts in pts_list :
146- start = timeit .default_timer ()
147167 seek_to_pts (decoder , pts )
148168 frame = get_next_frame (decoder )
149- end = timeit .default_timer ()
150- times .append (round (end - start , 3 ))
151169 frames .append (frame )
152170
153- if self ._print_each_iteration_time :
154- print ("torchcodec times=" , times , sum (times ))
155171 return frames
156172
157173 def get_consecutive_frames_from_video (self , video_file , numFramesToDecode ):
158- create_time = timeit .default_timer ()
159174 decoder = create_from_file (video_file )
160- add_stream_time = timeit .default_timer ()
161175 _add_video_stream (
162176 decoder ,
163177 num_threads = self ._num_threads ,
164178 color_conversion_library = self ._color_conversion_library ,
165179 )
180+
166181 frames = []
167- times = []
168- frames_time = timeit .default_timer ()
169182 for _ in range (numFramesToDecode ):
170- start = timeit .default_timer ()
171183 frame = get_next_frame (decoder )
172- end = timeit .default_timer ()
173- times .append (round (end - start , 3 ))
174184 frames .append (frame )
175185
176- if self ._print_each_iteration_time :
177- done_time = timeit .default_timer ()
178- create_duration = 1000 * round (add_stream_time - create_time , 3 )
179- add_stream_duration = 1000 * round (frames_time - add_stream_time , 3 )
180- frames_duration = 1000 * round (done_time - frames_time , 3 )
181- total_duration = 1000 * round (done_time - create_time , 3 )
182- print (
183- f"{ numFramesToDecode = } { create_duration = } { add_stream_duration = } { frames_duration = } { total_duration = } { frames [0 ][0 ].shape = } "
184- )
185- print ("torchcodec times=" , times , sum (times ))
186186 return frames
187187
188188
@@ -201,12 +201,9 @@ def get_frames_from_video(self, video_file, pts_list):
201201 color_conversion_library = self ._color_conversion_library ,
202202 )
203203 metadata = json .loads (get_json_metadata (decoder ))
204- average_fps = metadata ["averageFps" ]
205204 best_video_stream = metadata ["bestVideoStreamIndex" ]
206- indices_list = [int (pts * average_fps ) for pts in pts_list ]
207- frames = []
208- frames , * _ = get_frames_at_indices (
209- decoder , stream_index = best_video_stream , frame_indices = indices_list
205+ frames , * _ = get_frames_by_pts (
206+ decoder , stream_index = best_video_stream , timestamps = pts_list
210207 )
211208 return frames
212209
@@ -220,7 +217,6 @@ def get_consecutive_frames_from_video(self, video_file, numFramesToDecode):
220217 )
221218 metadata = json .loads (get_json_metadata (decoder ))
222219 best_video_stream = metadata ["bestVideoStreamIndex" ]
223- frames = []
224220 indices_list = list (range (numFramesToDecode ))
225221 frames , * _ = get_frames_at_indices (
226222 decoder , stream_index = best_video_stream , frame_indices = indices_list
0 commit comments