2
2
3
3
# TorchCodec
4
4
5
- TorchCodec is a Python library for decoding videos into PyTorch tensors. It aims
6
- to be fast, easy to use, and well integrated into the PyTorch ecosystem. If you
7
- want to use PyTorch to train ML models on videos, TorchCodec is how you turn
8
- those videos into data.
5
+ TorchCodec is a Python library for decoding videos into PyTorch tensors, on CPU
6
+ and CUDA GPU. It aims to be fast, easy to use, and well integrated into the
7
+ PyTorch ecosystem. If you want to use PyTorch to train ML models on videos,
8
+ TorchCodec is how you turn those videos into data.
9
9
10
10
We achieve these capabilities through:
11
11
@@ -19,21 +19,24 @@ We achieve these capabilities through:
19
19
or used directly to train models.
20
20
21
21
> [ !NOTE]
22
- > ⚠️ TorchCodec is still in early development stage and some APIs may be updated
23
- > in future versions without a deprecation cycle , depending on user feedback.
22
+ > ⚠️ TorchCodec is still in development stage and some APIs may be updated
23
+ > in future versions, depending on user feedback.
24
24
> If you have any suggestions or issues, please let us know by
25
25
> [ opening an issue] ( https://github.com/pytorch/torchcodec/issues/new/choose ) !
26
26
27
27
## Using TorchCodec
28
28
29
- Here's a condensed summary of what you can do with TorchCodec. For a more
30
- detailed example , [ check out our
29
+ Here's a condensed summary of what you can do with TorchCodec. For more detailed
30
+ examples , [ check out our
31
31
documentation] ( https://pytorch.org/torchcodec/stable/generated_examples/ ) !
32
32
33
+ #### Decoding
34
+
33
35
``` python
34
36
from torchcodec.decoders import VideoDecoder
35
37
36
- decoder = VideoDecoder(" path/to/video.mp4" )
38
+ device = " cpu" # or e.g. "cuda" !
39
+ decoder = VideoDecoder(" path/to/video.mp4" , device = device)
37
40
38
41
decoder.metadata
39
42
# VideoStreamMetadata:
@@ -44,39 +47,47 @@ decoder.metadata
44
47
# average_fps: 25.0
45
48
# ... (truncated output)
46
49
47
- len (decoder) # == decoder.metadata.num_frames!
48
- # 250
49
- decoder.metadata.average_fps # Note: instantaneous fps can be higher or lower
50
- # 25.0
51
-
52
50
# Simple Indexing API
53
51
decoder[0 ] # uint8 tensor of shape [C, H, W]
54
52
decoder[0 : - 1 : 20 ] # uint8 stacked tensor of shape [N, C, H, W]
55
53
54
+ # Indexing, with PTS and duration info:
55
+ decoder.get_frames_at(indices = [2 , 100 ])
56
+ # FrameBatch:
57
+ # data (shape): torch.Size([2, 3, 270, 480])
58
+ # pts_seconds: tensor([0.0667, 3.3367], dtype=torch.float64)
59
+ # duration_seconds: tensor([0.0334, 0.0334], dtype=torch.float64)
56
60
57
- # Iterate over frames:
58
- for frame in decoder:
59
- pass
61
+ # Time-based indexing with PTS and duration info
62
+ decoder.get_frames_played_at(seconds = [0.5 , 10.4 ])
63
+ # FrameBatch:
64
+ # data (shape): torch.Size([2, 3, 270, 480])
65
+ # pts_seconds: tensor([ 0.4671, 10.3770], dtype=torch.float64)
66
+ # duration_seconds: tensor([0.0334, 0.0334], dtype=torch.float64)
67
+ ```
60
68
61
- # Indexing, with PTS and duration info
62
- decoder.get_frame_at(len (decoder) - 1 )
63
- # Frame:
64
- # data (shape): torch.Size([3, 400, 640])
65
- # pts_seconds: 9.960000038146973
66
- # duration_seconds: 0.03999999910593033
69
+ #### Clip sampling
67
70
68
- decoder.get_frames_in_range(start = 10 , stop = 30 , step = 5 )
69
- # FrameBatch:
70
- # data (shape): torch.Size([4, 3, 400, 640])
71
- # pts_seconds: tensor([0.4000, 0.6000, 0.8000, 1.0000])
72
- # duration_seconds: tensor([0.0400, 0.0400, 0.0400, 0.0400])
71
+ ``` python
73
72
74
- # Time-based indexing with PTS and duration info
75
- decoder.get_frame_played_at(pts_seconds = 2 )
76
- # Frame:
77
- # data (shape): torch.Size([3, 400, 640])
78
- # pts_seconds: 2.0
79
- # duration_seconds: 0.03999999910593033
73
+ from torchcodec.samplers import clips_at_regular_timestamps
74
+
75
+ clips_at_regular_timestamps(
76
+ decoder,
77
+ seconds_between_clip_starts = 1.5 ,
78
+ num_frames_per_clip = 4 ,
79
+ seconds_between_frames = 0.1
80
+ )
81
+ # FrameBatch:
82
+ # data (shape): torch.Size([9, 4, 3, 270, 480])
83
+ # pts_seconds: tensor([[ 0.0000, 0.0667, 0.1668, 0.2669],
84
+ # [ 1.4681, 1.5682, 1.6683, 1.7684],
85
+ # [ 2.9696, 3.0697, 3.1698, 3.2699],
86
+ # ... (truncated), dtype=torch.float64)
87
+ # duration_seconds: tensor([[0.0334, 0.0334, 0.0334, 0.0334],
88
+ # [0.0334, 0.0334, 0.0334, 0.0334],
89
+ # [0.0334, 0.0334, 0.0334, 0.0334],
90
+ # ... (truncated), dtype=torch.float64)
80
91
```
81
92
82
93
You can use the following snippet to generate a video with FFmpeg and tryout
@@ -142,7 +153,7 @@ format you want. Refer to Nvidia's GPU support matrix for more details
142
153
[ official instructions] ( https://pytorch.org/get-started/locally/ ) .
143
154
144
155
3 . Install or compile FFmpeg with NVDEC support.
145
- TorchCodec with CUDA should work with FFmpeg versions in [ 4 , 7] .
156
+ TorchCodec with CUDA should work with FFmpeg versions in [ 5 , 7] .
146
157
147
158
If FFmpeg is not already installed, or you need a more recent version, an
148
159
easy way to install it is to use ` conda ` :
@@ -172,16 +183,17 @@ format you want. Refer to Nvidia's GPU support matrix for more details
172
183
ffmpeg -hwaccel cuda -hwaccel_output_format cuda -i test/resources/nasa_13013.mp4 -f null -
173
184
```
174
185
175
- 4 . Install TorchCodec by passing in an ` --index-url ` parameter that corresponds to your CUDA
176
- Toolkit version, example:
186
+ 4 . Install TorchCodec by passing in an ` --index-url ` parameter that corresponds
187
+ to your CUDA Toolkit version, example:
177
188
178
189
``` bash
179
- # This corresponds to CUDA Toolkit version 12.4 and nightly Pytorch.
180
- pip install torchcodec --index-url=https://download.pytorch.org/whl/nightly/cu124
190
+ # This corresponds to CUDA Toolkit version 12.4. It should be the same one
191
+ # you used when you installed PyTorch (If you installed PyTorch with pip).
192
+ pip install torchcodec --index-url=https://download.pytorch.org/whl/cu124
181
193
```
182
194
183
- Note that without passing in the ` --index-url ` parameter, ` pip ` installs TorchCodec
184
- binaries from PyPi which are CPU-only and do not have CUDA support .
195
+ Note that without passing in the ` --index-url ` parameter, ` pip ` installs
196
+ the CPU-only version of TorchCodec .
185
197
186
198
## Benchmark Results
187
199
0 commit comments