2121import tempfile
2222from pathlib import Path
2323import subprocess
24+ from torchcodec .decoders import VideoDecoder
2425
2526temp_dir = tempfile .mkdtemp ()
2627short_video_path = Path (temp_dir ) / "short_video.mp4"
4849 f"{ long_video_path } "
4950]
5051subprocess .run (ffmpeg_command )
51- from torchcodec . decoders import VideoDecoder
52+
5253test_decoder = VideoDecoder (short_video_path )
5354print (f"Short video duration: { test_decoder .metadata .duration_seconds } seconds" )
5455print (f"Long video duration: { VideoDecoder (long_video_path ).metadata .duration_seconds / 60 } minutes" )
8182# %%
8283# Define benchmarking function
8384
84- from torchcodec import samplers
85- from torchcodec .decoders ._video_decoder import VideoDecoder
8685import torch
8786
8887
@@ -107,7 +106,7 @@ def bench(f, file_like=False, average_over=50, warmup=2, **f_kwargs):
107106 print (f"{ med = :.2f} ms +- { std :.2f} " )
108107
109108# %%
110- # Compare performance of initializing VideoDecoder with custom_frame_mappings vs seek_modes
109+ # Compare performance of initializing VideoDecoder with custom_frame_mappings vs exact seek_mode
111110
112111
113112for video_path , json_path in ((short_video_path , short_json_path ), (long_video_path , long_json_path )):
@@ -125,58 +124,57 @@ def bench(f, file_like=False, average_over=50, warmup=2, **f_kwargs):
125124 bench (VideoDecoder , source = video_path , stream_index = stream_index , seek_mode = "exact" )
126125
127126# %%
128- # Decode entire videos with custom_frame_mappings vs seek_modes
129-
130- from torchcodec .decoders ._video_decoder import VideoDecoder
127+ # Decode frames from multiple videos with custom_frame_mappings vs exact seek_mode
131128
132129
133- def decode_frames (video_path , seek_mode = "exact" , custom_frame_mappings = None ):
134- decoder = VideoDecoder (
135- source = video_path ,
136- seek_mode = seek_mode ,
137- custom_frame_mappings = custom_frame_mappings
138- )
139- decoder .get_frames_in_range (start = 0 , stop = 100 )
130+ def decode_frames_from_n_videos (video_path , seek_mode = "exact" , custom_frame_mappings = None , num_videos = 10 ):
131+ for _ in range (num_videos ):
132+ decoder = VideoDecoder (
133+ source = video_path ,
134+ seek_mode = seek_mode ,
135+ custom_frame_mappings = custom_frame_mappings
136+ )
137+ decoder .get_frames_in_range (start = 0 , stop = 10 )
140138
141139
142140for video_path , json_path in ((short_video_path , short_json_path ), (long_video_path , long_json_path )):
143141 print (f"Running benchmarks on { Path (video_path ).name } " )
144142 print ("Decoding frames with custom_frame_mappings JSON str from file:" )
145143 with open (json_path , "r" ) as f :
146- bench (decode_frames , video_path = video_path , custom_frame_mappings = (f .read ()))
147-
148- print ("Creating a VideoDecoder object with custom_frame_mappings from filelike:" )
149- with open (json_path , "r" ) as f :
150- bench (decode_frames , file_like = True , video_path = video_path , custom_frame_mappings = f )
144+ bench (decode_frames_from_n_videos , video_path = video_path , custom_frame_mappings = (f .read ()))
151145
152146 # Compare against seek_modes
153147 print ("Decoding frames with seek_mode='exact':" )
154- bench (decode_frames , video_path = video_path , seek_mode = "exact" )
148+ bench (decode_frames_from_n_videos , video_path = video_path , seek_mode = "exact" )
155149
156150# %%
157- # Compare performance of sampling clips with custom_frame_mappings vs seek_modes
151+ # Compare performance of sampling clips from multiple videos with custom_frame_mappings vs exact seek_mode
158152
159153
160- def sample_clips (video_path , seek_mode = "exact" , custom_frame_mappings = None ):
161- return samplers .clips_at_random_indices (
162- decoder = VideoDecoder (
163- source = video_path ,
164- seek_mode = seek_mode ,
165- custom_frame_mappings = custom_frame_mappings
166- ),
167- num_clips = 5 ,
168- num_frames_per_clip = 2 ,
169- )
154+ from torchcodec import samplers
155+
156+
157+ def sample_clips_from_n_videos (video_path , seek_mode = "exact" , custom_frame_mappings = None , num_videos = 10 ):
158+ for _ in range (num_videos ):
159+ return samplers .clips_at_random_indices (
160+ decoder = VideoDecoder (
161+ source = video_path ,
162+ seek_mode = seek_mode ,
163+ custom_frame_mappings = custom_frame_mappings
164+ ),
165+ num_clips = 5 ,
166+ num_frames_per_clip = 2 ,
167+ )
170168
171169
172170for video_path , json_path in ((short_video_path , short_json_path ), (long_video_path , long_json_path )):
173171 print (f"Running benchmarks on { Path (video_path ).name } " )
174172 print ("Sampling clips with custom_frame_mappings:" )
175173 with open (json_path , "r" ) as f :
176174 mappings = f .read ()
177- bench (sample_clips , file_like = False , video_path = video_path , custom_frame_mappings = mappings )
175+ bench (sample_clips_from_n_videos , file_like = False , video_path = video_path , custom_frame_mappings = mappings )
178176
179177 print ("Sampling clips with seek_mode='exact':" )
180- bench (sample_clips , video_path = video_path , seek_mode = "exact" )
178+ bench (sample_clips_from_n_videos , video_path = video_path , seek_mode = "exact" )
181179
182180# %%
0 commit comments