22
33# TorchCodec
44
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.
99
1010We achieve these capabilities through:
1111
@@ -19,21 +19,24 @@ We achieve these capabilities through:
1919 or used directly to train models.
2020
2121> [ !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.
2424> If you have any suggestions or issues, please let us know by
2525> [ opening an issue] ( https://github.com/pytorch/torchcodec/issues/new/choose ) !
2626
2727## Using TorchCodec
2828
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
3131documentation] ( https://pytorch.org/torchcodec/stable/generated_examples/ ) !
3232
33+ #### Decoding
34+
3335``` python
3436from torchcodec.decoders import VideoDecoder
3537
36- decoder = VideoDecoder(" path/to/video.mp4" )
38+ device = " cpu" # or e.g. "cuda" !
39+ decoder = VideoDecoder(" path/to/video.mp4" , device = device)
3740
3841decoder.metadata
3942# VideoStreamMetadata:
@@ -44,39 +47,47 @@ decoder.metadata
4447# average_fps: 25.0
4548# ... (truncated output)
4649
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-
5250# Simple Indexing API
5351decoder[0 ] # uint8 tensor of shape [C, H, W]
5452decoder[0 : - 1 : 20 ] # uint8 stacked tensor of shape [N, C, H, W]
5553
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)
5660
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+ ```
6068
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
6770
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
7372
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)
8091```
8192
8293You 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
142153 [ official instructions] ( https://pytorch.org/get-started/locally/ ) .
143154
1441553 . 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] .
146157
147158 If FFmpeg is not already installed, or you need a more recent version, an
148159 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
172183 ffmpeg -hwaccel cuda -hwaccel_output_format cuda -i test/resources/nasa_13013.mp4 -f null -
173184 ```
174185
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:
177188
178189 ``` 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
181193 ```
182194
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 .
185197
186198## Benchmark Results
187199
0 commit comments