@@ -6,7 +6,7 @@ TorchCodec is a Python library for decoding video and audio data into PyTorch
66tensors, on CPU and CUDA GPU. It also supports video and audio encoding on CPU!
77It aims to be fast, easy to use, and well integrated
88into the PyTorch ecosystem. If you want to use PyTorch to train ML models on
9- videos and audio, TorchCodec is how you turn these into data.
9+ videos and audio, or run inference, TorchCodec is how you turn these into data.
1010
1111We achieve these capabilities through:
1212
@@ -19,13 +19,13 @@ We achieve these capabilities through:
1919* Returning data as PyTorch tensors, ready to be fed into PyTorch transforms
2020 or used directly to train models.
2121
22- ## Using TorchCodec
22+ ## Usage Examples
2323
24- Here's a condensed summary of what you can do with TorchCodec. For more detailed
25- examples, [ check out our
24+ Below are some examples of what you can do with TorchCodec. For more detailed
25+ examples and more use-cases , [ check out our
2626documentation] ( https://meta-pytorch.org/torchcodec/stable/generated_examples/ ) !
2727
28- #### Decoding
28+ #### Video Decoding
2929
3030``` python
3131from torchcodec.decoders import VideoDecoder
@@ -61,56 +61,40 @@ decoder.get_frames_played_at(seconds=[0.5, 10.4])
6161# duration_seconds: tensor([0.0334, 0.0334], dtype=torch.float64)
6262```
6363
64- #### Clip sampling
64+ You can use the following snippet to generate a video with FFmpeg and try out
65+ the ` VideoDecoder ` :
6566
66- ``` python
67-
68- from torchcodec.samplers import clips_at_regular_timestamps
69-
70- clips_at_regular_timestamps(
71- decoder,
72- seconds_between_clip_starts = 1.5 ,
73- num_frames_per_clip = 4 ,
74- seconds_between_frames = 0.1
75- )
76- # FrameBatch:
77- # data (shape): torch.Size([9, 4, 3, 270, 480])
78- # pts_seconds: tensor([[ 0.0000, 0.0667, 0.1668, 0.2669],
79- # [ 1.4681, 1.5682, 1.6683, 1.7684],
80- # [ 2.9696, 3.0697, 3.1698, 3.2699],
81- # ... (truncated), dtype=torch.float64)
82- # duration_seconds: tensor([[0.0334, 0.0334, 0.0334, 0.0334],
83- # [0.0334, 0.0334, 0.0334, 0.0334],
84- # [0.0334, 0.0334, 0.0334, 0.0334],
85- # ... (truncated), dtype=torch.float64)
67+ ``` bash
68+ ffmpeg -f lavfi -i testsrc2=size=640x400:duration=10:rate=25 /tmp/output_video.mp4
8669```
8770
88- You can use the following snippet to generate a video with FFmpeg and tryout
89- TorchCodec:
71+ #### Encoding
9072
91- ``` bash
92- fontfile=/usr/share/fonts/dejavu-sans-mono-fonts/DejaVuSansMono-Bold.ttf
93- output_video_file=/tmp/output_video.mp4
73+ ``` python
74+ from torchcodec.encoders import Encoder
9475
95- ffmpeg -f lavfi -i \
96- color=size=640x400:duration=10:rate=25:color=blue \
97- -vf " drawtext=fontfile=${fontfile} :fontsize=30:fontcolor=white:x=(w-text_w)/2:y=(h-text_h)/2:text='Frame %{frame_num}'" \
98- ${output_video_file}
76+ encoder = Encoder()
77+ video_stream = encoder.add_video(
78+ height = height, width = width, frame_rate = frame_rate,
79+ )
80+ audio_stream = encoder.add_audio(
81+ sample_rate = sample_rate, num_channels = num_channels,
82+ )
83+ with encoder.open_file(" output.mp4" ):
84+ video_stream.add_frames(frames_batch_0)
85+ audio_stream.add_samples(samples_batch_0)
86+ video_stream.add_frames(frames_batch_1)
87+ audio_stream.add_samples(samples_batch_1)
88+ # ...
9989```
10090
10191## Installing TorchCodec
102- ### Installing CPU-only TorchCodec
10392
104- 1 . Install the latest stable version of PyTorch following the
105- [ official instructions] ( https://pytorch.org/get-started/locally/ ) . For other
106- versions, refer to the table below for compatibility between versions of
107- ` torch ` and ` torchcodec ` .
108-
109- 2 . Install FFmpeg, if it's not already installed. TorchCodec supports
110- all major FFmpeg versions in [ 4, 8] .
111- Linux distributions usually come with FFmpeg pre-installed. You'll need
112- FFmpeg that comes with separate shared libraries. This is especially relevant
113- for Windows users: these are usually called the "shared" releases.
93+ 1 . Install FFmpeg, if it's not already installed. TorchCodec supports all major
94+ FFmpeg versions in [ 4, 8] . Linux distributions usually come with FFmpeg
95+ pre-installed. You'll need FFmpeg that comes with separate shared libraries.
96+ This is especially relevant for Windows users: these are usually called the
97+ "shared" releases.
11498
11599 If FFmpeg is not already installed, or you need a more recent version, an
116100 easy way to install it is to use ` conda ` :
@@ -121,18 +105,74 @@ ffmpeg -f lavfi -i \
121105 conda install " ffmpeg" -c conda-forge
122106 ```
123107
124- 3 . Install TorchCodec:
108+ 2 . Install PyTorch and TorchCodec:
125109
126110 ``` bash
127- pip install torchcodec --index-url=https://download.pytorch.org/whl/cpu
111+ pip install torch torchcodec
128112 ```
129113
114+ That's it! On Linux x86 and aarch64, this will install CUDA-enabled wheels by
115+ default (matching the default behavior of ` pip install torch ` ). These wheels
116+ should * still* work even if you do not have a GPU on your machine. On macOS
117+ and Windows this will install CPU-only wheels. CPU wheels are available for
118+ Linux (x86_64 and aarch64), macOS, and Windows.
119+
120+ For other versions of PyTorch, refer to the compatibility table below.
121+
122+ ### CUDA support
123+
124+ CUDA-enabled wheels are installed by default on Linux. For Windows, you'll need
125+ to pass ` --index-url ` as described below.
126+
127+ Make sure you have a GPU with NVDEC hardware that can decode the format you
128+ want. Refer to Nvidia's GPU support matrix
129+ [ here] ( https://developer.nvidia.com/video-encode-and-decode-gpu-support-matrix-new ) .
130+
131+ You will need the ` libnpp ` and ` libnvrtc ` CUDA libraries, which are usually
132+ part of the CUDA Toolkit.
133+
134+ To select a specific CUDA Toolkit version, use ` --index-url ` . Make sure to
135+ install the corresponding PyTorch version as well (refer to the
136+ [ official instructions] ( https://pytorch.org/get-started/locally/ ) ):
137+
138+ ``` bash
139+ # This corresponds to CUDA Toolkit version 13.0.
140+ pip install torch torchcodec --index-url=https://download.pytorch.org/whl/cu130
141+ ```
142+
143+ Make sure your FFmpeg has NVDEC support:
144+
145+ ``` bash
146+ ffmpeg -decoders | grep -i nvidia
147+ # This should show a line like this:
148+ # V..... h264_cuvid Nvidia CUVID H264 decoder (codec h264)
149+ ```
150+
151+ To check that FFmpeg libraries work with NVDEC correctly you can decode a
152+ generated test video:
153+
154+ ``` bash
155+ ffmpeg -hwaccel cuda -hwaccel_output_format cuda -f lavfi -i testsrc2=duration=1 -f null -
156+ ```
157+
158+ ### CPU-only installation
159+
160+ To install CPU-only wheels explicitly (e.g. on Linux where CUDA wheels are the
161+ default):
162+
163+ ``` bash
164+ pip install torchcodec --index-url=https://download.pytorch.org/whl/cpu
165+ ```
166+
167+ ### Compatibility
168+
130169The following table indicates the compatibility between versions of
131170` torchcodec ` , ` torch ` and Python.
132171
133172| ` torchcodec ` | ` torch ` | Python |
134173| ------------------ | ------------------ | ------------------- |
135174| ` main ` / ` nightly ` | ` main ` / ` nightly ` | ` >=3.10 ` , ` <=3.14 ` |
175+ | ` 0.13 ` | ` >=2.11 ` | ` >=3.10 ` , ` <=3.14 ` |
136176| ` 0.12 ` | ` >=2.11 ` | ` >=3.10 ` , ` <=3.14 ` |
137177| ` 0.11 ` | ` 2.11 ` | ` >=3.10 ` , ` <=3.14 ` |
138178| ` 0.10 ` | ` 2.10 ` | ` >=3.10 ` , ` <=3.14 ` |
@@ -155,86 +195,6 @@ The following table indicates the compatibility between versions of
155195
156196</details >
157197
158- ### Installing CUDA-enabled TorchCodec
159-
160- First, make sure you have a GPU that has NVDEC hardware that can decode the
161- format you want. Refer to Nvidia's GPU support matrix for more details
162- [ here] ( https://developer.nvidia.com/video-encode-and-decode-gpu-support-matrix-new ) .
163-
164- 1 . Install FFmpeg with NVDEC support.
165- TorchCodec with CUDA should work with FFmpeg versions in [ 4, 8] .
166-
167- If FFmpeg is not already installed, or you need a more recent version, an
168- easy way to install it is to use ` conda ` :
169-
170- ``` bash
171- conda install " ffmpeg"
172- # or
173- conda install " ffmpeg" -c conda-forge
174- ```
175-
176- After installing FFmpeg make sure it has NVDEC support when you list the supported
177- decoders:
178-
179- ``` bash
180- ffmpeg -decoders | grep -i nvidia
181- # This should show a line like this:
182- # V..... h264_cuvid Nvidia CUVID H264 decoder (codec h264)
183- ```
184-
185- To check that FFmpeg libraries work with NVDEC correctly you can decode a sample video:
186-
187- ``` bash
188- ffmpeg -hwaccel cuda -hwaccel_output_format cuda -i test/resources/nasa_13013.mp4 -f null -
189- ```
190-
191- #### Linux
192-
193- 2 . Install Pytorch corresponding to your CUDA Toolkit using the
194- [ official instructions] ( https://pytorch.org/get-started/locally/ ) . You'll
195- need the ` libnpp ` and ` libnvrtc ` CUDA libraries, which are usually part of
196- the CUDA Toolkit.
197-
198- 3 . Install TorchCodec
199-
200- On Linux, ` pip install torchcodec ` defaults to a CUDA wheel,
201- matching the default behavior of ` pip install torch ` .
202-
203- ``` bash
204- pip install torchcodec
205- ```
206- Use ` --index-url ` to select a different CUDA Toolkit version:
207-
208- ``` bash
209- # This corresponds to CUDA Toolkit version 13.0. It should be the same one
210- # you used when you installed PyTorch (If you installed PyTorch with pip).
211- pip install torchcodec --index-url=https://download.pytorch.org/whl/cu130
212- ```
213-
214- #### Windows
215-
216- 2 . On Windows (experimental support), you'll need to rely on ` conda ` to install
217- both pytorch and TorchCodec:
218-
219- ``` bash
220- conda install -c conda-forge " torchcodec=*=*cuda*"
221- ```
222-
223- ## Benchmark Results
224-
225- The following was generated by running [ our benchmark script] ( ./benchmarks/decoders/generate_readme_data.py ) on a lightly loaded 22-core machine with an Nvidia A100 with
226- 5 [ NVDEC decoders] ( https://docs.nvidia.com/video-technologies/video-codec-sdk/12.1/nvdec-application-note/index.html# ) .
227-
228- ![ benchmark_results] ( ./benchmarks/decoders/benchmark_readme_chart.png )
229-
230- The top row is a [ Mandelbrot] ( https://ffmpeg.org/ffmpeg-filters.html#mandelbrot ) video
231- generated from FFmpeg that has a resolution of 1280x720 at 60 fps and is 120 seconds long.
232- The bottom row is [ promotional video from NASA] ( https://download.pytorch.org/torchaudio/tutorial-assets/stream-api/NASAs_Most_Scientifically_Complex_Space_Observatory_Requires_Precision-MP4_small.mp4 )
233- that has a resolution of 960x540 at 29.7 fps and is 206 seconds long. Both videos were
234- encoded with libx264 and yuv420p pixel format. All decoders, except for TorchVision, used FFmpeg 6.1.2. TorchVision used FFmpeg 4.2.2.
235-
236- For TorchCodec, the "approx" label means that it was using [ approximate mode] ( https://meta-pytorch.org/torchcodec/stable/generated_examples/decoding/approximate_mode.html )
237- for seeking.
238198
239199## Contributing
240200
0 commit comments