2424pool manager.
2525"""
2626
27- from __future__ import annotations
2827
2928# %%
3029# Let's first define some utility functions for benchmarking and data
@@ -97,21 +96,17 @@ def generate_long_video(temp_dir: str):
9796 raise RuntimeError (f"Failed to download video. { response .status_code = } ." )
9897
9998 short_video_path = Path (temp_dir ) / "short_video.mp4"
100- with open (short_video_path , "wb" ) as f :
99+ with open (short_video_path , 'wb' ) as f :
101100 for chunk in response .iter_content ():
102101 f .write (chunk )
103102
104103 # Create a longer video by repeating the short one 50 times
105104 long_video_path = Path (temp_dir ) / "long_video.mp4"
106105 ffmpeg_command = [
107- "ffmpeg" ,
108- "-y" ,
109- "-stream_loop" ,
110- "49" , # repeat video 50 times
111- "-i" ,
112- str (short_video_path ),
113- "-c" ,
114- "copy" ,
106+ "ffmpeg" , "-y" ,
107+ "-stream_loop" , "49" , # repeat video 50 times
108+ "-i" , str (short_video_path ),
109+ "-c" , "copy" ,
115110 str (long_video_path ),
116111 ]
117112 subprocess .run (ffmpeg_command , check = True , stdout = subprocess .PIPE , stderr = subprocess .PIPE )
@@ -127,9 +122,7 @@ def generate_long_video(temp_dir: str):
127122
128123short_duration = timedelta (seconds = VideoDecoder (short_video_path ).metadata .duration_seconds )
129124long_duration = timedelta (seconds = metadata .duration_seconds )
130- print (
131- f"Original video duration: { int (short_duration .total_seconds () // 60 )} m{ int (short_duration .total_seconds () % 60 ):02d} s"
132- )
125+ print (f"Original video duration: { int (short_duration .total_seconds () // 60 )} m{ int (short_duration .total_seconds () % 60 ):02d} s" )
133126print (f"Long video duration: { int (long_duration .total_seconds () // 60 )} m{ int (long_duration .total_seconds () % 60 ):02d} s" )
134127print (f"Video resolution: { metadata .width } x{ metadata .height } " )
135128print (f"Average FPS: { metadata .average_fps :.1f} " )
@@ -182,7 +175,11 @@ def decode_sequentially(indices: list[int], video_path=long_video_path):
182175# threads within FFmpeg itself to accelerate decoding operations.
183176
184177
185- def decode_with_ffmpeg_parallelism (indices : list [int ], num_threads : int , video_path = long_video_path ):
178+ def decode_with_ffmpeg_parallelism (
179+ indices : list [int ],
180+ num_threads : int ,
181+ video_path = long_video_path ,
182+ ):
186183 """Decode frames using FFmpeg's internal threading."""
187184 decoder = VideoDecoder (video_path , num_ffmpeg_threads = num_threads , seek_mode = "approximate" )
188185 return decoder .get_frames_at (indices )
@@ -203,7 +200,11 @@ def decode_with_ffmpeg_parallelism(indices: list[int], num_threads: int, video_p
203200# Process-based parallelism distributes work across multiple Python processes.
204201
205202
206- def decode_with_multiprocessing (indices : list [int ], num_processes : int , video_path = long_video_path ):
203+ def decode_with_multiprocessing (
204+ indices : list [int ],
205+ num_processes : int ,
206+ video_path = long_video_path ,
207+ ):
207208 """Decode frames using multiple processes with joblib."""
208209 chunks = split_indices (indices , num_chunks = num_processes )
209210
@@ -229,7 +230,11 @@ def decode_with_multiprocessing(indices: list[int], num_processes: int, video_pa
229230# TorchCodec releases the GIL, so this can be very effective.
230231
231232
232- def decode_with_multithreading (indices : list [int ], num_threads : int , video_path = long_video_path ):
233+ def decode_with_multithreading (
234+ indices : list [int ],
235+ num_threads : int ,
236+ video_path = long_video_path
237+ ):
233238 """Decode frames using multiple threads with joblib."""
234239 chunks = split_indices (indices , num_chunks = num_threads )
235240
@@ -260,5 +265,4 @@ def decode_with_multithreading(indices: list[int], num_threads: int, video_path=
260265
261266# %%
262267import shutil
263-
264268shutil .rmtree (temp_dir )
0 commit comments