21
21
import tempfile
22
22
from pathlib import Path
23
23
import subprocess
24
+ from torchcodec .decoders import VideoDecoder
24
25
25
26
temp_dir = tempfile .mkdtemp ()
26
27
short_video_path = Path (temp_dir ) / "short_video.mp4"
48
49
f"{ long_video_path } "
49
50
]
50
51
subprocess .run (ffmpeg_command )
51
- from torchcodec . decoders import VideoDecoder
52
+
52
53
test_decoder = VideoDecoder (short_video_path )
53
54
print (f"Short video duration: { test_decoder .metadata .duration_seconds } seconds" )
54
55
print (f"Long video duration: { VideoDecoder (long_video_path ).metadata .duration_seconds / 60 } minutes" )
81
82
# %%
82
83
# Define benchmarking function
83
84
84
- from torchcodec import samplers
85
- from torchcodec .decoders ._video_decoder import VideoDecoder
86
85
import torch
87
86
88
87
@@ -107,7 +106,7 @@ def bench(f, file_like=False, average_over=50, warmup=2, **f_kwargs):
107
106
print (f"{ med = :.2f} ms +- { std :.2f} " )
108
107
109
108
# %%
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
111
110
112
111
113
112
for 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):
125
124
bench (VideoDecoder , source = video_path , stream_index = stream_index , seek_mode = "exact" )
126
125
127
126
# %%
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
131
128
132
129
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 )
140
138
141
139
142
140
for video_path , json_path in ((short_video_path , short_json_path ), (long_video_path , long_json_path )):
143
141
print (f"Running benchmarks on { Path (video_path ).name } " )
144
142
print ("Decoding frames with custom_frame_mappings JSON str from file:" )
145
143
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 ()))
151
145
152
146
# Compare against seek_modes
153
147
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" )
155
149
156
150
# %%
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
158
152
159
153
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
+ )
170
168
171
169
172
170
for video_path , json_path in ((short_video_path , short_json_path ), (long_video_path , long_json_path )):
173
171
print (f"Running benchmarks on { Path (video_path ).name } " )
174
172
print ("Sampling clips with custom_frame_mappings:" )
175
173
with open (json_path , "r" ) as f :
176
174
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 )
178
176
179
177
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" )
181
179
182
180
# %%
0 commit comments