Skip to content

Commit ef2e047

Browse files
committed
Undo some stuff; make us 3.10+
1 parent 38e989e commit ef2e047

23 files changed

+55
-82
lines changed

examples/decoding/approximate_mode.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,25 +42,21 @@
4242

4343
temp_dir = tempfile.mkdtemp()
4444
short_video_path = Path(temp_dir) / "short_video.mp4"
45-
with open(short_video_path, "wb") as f:
45+
with open(short_video_path, 'wb') as f:
4646
for chunk in response.iter_content():
4747
f.write(chunk)
4848

4949
long_video_path = Path(temp_dir) / "long_video.mp4"
5050
ffmpeg_command = [
5151
"ffmpeg",
52-
"-stream_loop",
53-
"99", # repeat video 100 times
54-
"-i",
55-
f"{short_video_path}",
56-
"-c",
57-
"copy",
52+
"-stream_loop", "99", # repeat video 100 times
53+
"-i", f"{short_video_path}",
54+
"-c", "copy",
5855
f"{long_video_path}",
5956
]
6057
subprocess.run(ffmpeg_command, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
6158

6259
from torchcodec.decoders import VideoDecoder
63-
6460
print(f"Short video duration: {VideoDecoder(short_video_path).metadata.duration_seconds} seconds")
6561
print(f"Long video duration: {VideoDecoder(long_video_path).metadata.duration_seconds / 60} minutes")
6662

@@ -120,7 +116,10 @@ def bench(f, average_over=50, warmup=2, **f_kwargs):
120116

121117
def sample_clips(seek_mode):
122118
return samplers.clips_at_random_indices(
123-
decoder=VideoDecoder(source=long_video_path, seek_mode=seek_mode),
119+
decoder=VideoDecoder(
120+
source=long_video_path,
121+
seek_mode=seek_mode
122+
),
124123
num_clips=5,
125124
num_frames_per_clip=2,
126125
)
@@ -155,8 +154,7 @@ def sample_clips(seek_mode):
155154
torch.testing.assert_close(
156155
exact_decoder.get_frame_at(i).data,
157156
approx_decoder.get_frame_at(i).data,
158-
atol=0,
159-
rtol=0,
157+
atol=0, rtol=0,
160158
)
161159
print("Frame seeking is the same for this video!")
162160

examples/decoding/basic_cuda_example.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,9 @@ def plot_cpu_and_cuda_frames(cpu_frames: torch.Tensor, cuda_frames: torch.Tensor
154154
# differences because CUDA math is not bit-exact with respect to CPU math.
155155
#
156156
frames_equal = torch.equal(cpu_frames.to("cuda"), cuda_frames)
157-
mean_abs_diff = torch.mean(torch.abs(cpu_frames.float().to("cuda") - cuda_frames.float()))
157+
mean_abs_diff = torch.mean(
158+
torch.abs(cpu_frames.float().to("cuda") - cuda_frames.float())
159+
)
158160
max_abs_diff = torch.max(torch.abs(cpu_frames.to("cuda").float() - cuda_frames.float()))
159161
print(f"{frames_equal=}")
160162
print(f"{mean_abs_diff=}")

examples/decoding/basic_example.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
:class:`~torchcodec.decoders.VideoDecoder` class.
1414
"""
1515

16-
from __future__ import annotations
1716

1817
# %%
1918
# First, a bit of boilerplate: we'll download a video from the web, and define a

examples/decoding/custom_frame_mappings.py

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,25 +41,21 @@
4141

4242
temp_dir = tempfile.mkdtemp()
4343
short_video_path = Path(temp_dir) / "short_video.mp4"
44-
with open(short_video_path, "wb") as f:
44+
with open(short_video_path, 'wb') as f:
4545
for chunk in response.iter_content():
4646
f.write(chunk)
4747

4848
long_video_path = Path(temp_dir) / "long_video.mp4"
4949
ffmpeg_command = [
5050
"ffmpeg",
51-
"-stream_loop",
52-
"50", # repeat video 50 times to get a ~12 min video
53-
"-i",
54-
f"{short_video_path}",
55-
"-c",
56-
"copy",
51+
"-stream_loop", "50", # repeat video 50 times to get a ~12 min video
52+
"-i", f"{short_video_path}",
53+
"-c", "copy",
5754
f"{long_video_path}",
5855
]
5956
subprocess.run(ffmpeg_command, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
6057

6158
from torchcodec.decoders import VideoDecoder
62-
6359
print(f"Short video duration: {VideoDecoder(short_video_path).metadata.duration_seconds} seconds")
6460
print(f"Long video duration: {VideoDecoder(long_video_path).metadata.duration_seconds / 60} minutes")
6561

@@ -88,15 +84,12 @@
8884
def generate_frame_mappings(video_path, output_json_path, stream_index):
8985
ffprobe_cmd = [
9086
"ffprobe",
91-
"-i",
92-
f"{video_path}",
93-
"-select_streams",
94-
f"{stream_index}",
87+
"-i", f"{video_path}",
88+
"-select_streams", f"{stream_index}",
9589
"-show_frames",
9690
"-show_entries",
9791
"frame=pts,duration,key_frame",
98-
"-of",
99-
"json",
92+
"-of", "json",
10093
]
10194
print(f"Running ffprobe:\n{' '.join(ffprobe_cmd)}\n")
10295
ffprobe_result = subprocess.run(ffprobe_cmd, check=True, capture_output=True, text=True)
@@ -173,7 +166,11 @@ def bench(f, file_like=False, average_over=50, warmup=2, **f_kwargs):
173166

174167

175168
def decode_frames(video_path, seek_mode="exact", custom_frame_mappings=None):
176-
decoder = VideoDecoder(source=video_path, seek_mode=seek_mode, custom_frame_mappings=custom_frame_mappings)
169+
decoder = VideoDecoder(
170+
source=video_path,
171+
seek_mode=seek_mode,
172+
custom_frame_mappings=custom_frame_mappings
173+
)
177174
decoder.get_frames_in_range(start=0, stop=10)
178175

179176

@@ -207,8 +204,7 @@ def decode_frames(video_path, seek_mode="exact", custom_frame_mappings=None):
207204
torch.testing.assert_close(
208205
exact_decoder.get_frame_at(i).data,
209206
custom_frame_mappings_decoder.get_frame_at(i).data,
210-
atol=0,
211-
rtol=0,
207+
atol=0, rtol=0,
212208
)
213209
print("Frame seeking is the same for this video!")
214210

examples/decoding/file_like.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ def stream_while_decode():
158158
# session; we need to indicate that we need to trust the environment
159159
# settings for proxy configuration. Depending on your environment, you may
160160
# not need this setting.
161-
with fsspec.open(nasa_url, client_kwargs={"trust_env": True}) as file_like:
161+
with fsspec.open(nasa_url, client_kwargs={'trust_env': True}) as file_like:
162162
decoder = VideoDecoder(file_like, seek_mode="approximate")
163163
return decoder[0]
164164

@@ -226,20 +226,18 @@ def seek(self, offset: int, whence: int) -> int:
226226
file_op_counter = FileOpCounter(open(nasa_video_path, "rb"))
227227
counter_decoder = VideoDecoder(file_op_counter, seek_mode="approximate")
228228

229-
print(
230-
"Decoder initialization required " f"{file_op_counter.num_reads} reads and " f"{file_op_counter.num_seeks} seeks."
231-
)
229+
print("Decoder initialization required "
230+
f"{file_op_counter.num_reads} reads and "
231+
f"{file_op_counter.num_seeks} seeks.")
232232

233233
init_reads = file_op_counter.num_reads
234234
init_seeks = file_op_counter.num_seeks
235235

236236
first_frame = counter_decoder[0]
237237

238-
print(
239-
"Decoding the first frame required "
240-
f"{file_op_counter.num_reads - init_reads} additional reads and "
241-
f"{file_op_counter.num_seeks - init_seeks} additional seeks."
242-
)
238+
print("Decoding the first frame required "
239+
f"{file_op_counter.num_reads - init_reads} additional reads and "
240+
f"{file_op_counter.num_seeks - init_seeks} additional seeks.")
243241

244242
# %%
245243
# While we defined a simple class primarily for demonstration, it's actually
@@ -301,6 +299,5 @@ def decode_from_existing_open_file_object():
301299
# %%
302300
# Finally, let's clean up the local resources we created.
303301
import shutil
304-
305302
shutil.rmtree(temp_dir)
306303
# %%

examples/decoding/parallel_decoding.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
pool 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

128123
short_duration = timedelta(seconds=VideoDecoder(short_video_path).metadata.duration_seconds)
129124
long_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")
133126
print(f"Long video duration: {int(long_duration.total_seconds() // 60)}m{int(long_duration.total_seconds() % 60):02d}s")
134127
print(f"Video resolution: {metadata.width}x{metadata.height}")
135128
print(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
# %%
262267
import shutil
263-
264268
shutil.rmtree(temp_dir)

examples/decoding/sampling.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
input to video models.
1515
"""
1616

17-
from __future__ import annotations
1817

1918
# %%
2019
# First, a bit of boilerplate: we'll download a video from the web, and define a

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "torchcodec"
33
description = "A video decoder for PyTorch"
44
readme = "README.md"
5-
requires-python = ">=3.8"
5+
requires-python = ">=3.10"
66
license-files = ["LICENSE"]
77
authors = [
88
{ name = "PyTorch Team", email = "[email protected]" },
@@ -32,7 +32,7 @@ dev = [
3232
first_party_detection = false
3333

3434
[tool.black]
35-
target-version = ["py38"]
35+
target-version = ["py310"]
3636

3737
[tool.ufmt]
3838

src/torchcodec/_core/_metadata.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
# This source code is licensed under the BSD-style license found in the
55
# LICENSE file in the root directory of this source tree.
66

7-
from __future__ import annotations
87

98
import dataclasses
109
import json

src/torchcodec/_core/ops.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
# This source code is licensed under the BSD-style license found in the
55
# LICENSE file in the root directory of this source tree.
66

7-
from __future__ import annotations
87

98
import io
109
import json

0 commit comments

Comments
 (0)