Blog TorchCodec 1.0: The Media Layer of PyTorch By Nicolas Hug, Scott Schneider, Daniel Flores, Molly Xu, Ahmad SharifDecember 2, 2026No Comments /row

Featured projects

TorchCodec 1.0 is here!  TorchCodec started out as a video decoding library with a simple goal: efficiently turning encoded videos into RGB video frame tensors. Since then, it has grown to decode and encode all media, including audio and images. As we described in our previous post, TorchCodec is now where all of PyTorch’s media decoding and encoding lives. Our APIs have been stable for a while, and 1.0 makes it official!

TorchCodec has been used in production for over two years, and it is a key part of many multimodal projects. Hugging Face libraries like datasets, transformers and LeRobot rely on TorchCodec to decode audio and video, from dataset samples to model inputs. vLLM uses TorchCodec to decode the video, audio and images sent to multimodal models, on CPU or GPU. SGLang uses it as its preferred video and audio decoder, including GPU video decoding.

In this post, we’ll go over what TorchCodec does today. Then we’ll dive into the new low-level APIs available in 1.0, which unlock a whole set of new features and performance knobs, and show how a coding agent can auto-tune those knobs for your own workload.

What TorchCodec does: media decoding and encoding

TorchCodec is the media layer behind multimodal models, for both training and inference. It decodes videos, audio and images into PyTorch tensors. Those tensors are ready to be used with TorchVision or TorchAudio transforms, or fed directly to models. It can also endcode tensors back into media, like the outputs of generative models.

Video. VideoDecoder reads videos from a file, from bytes, or from any file-like object, so you can stream videos from the cloud, only fetching the parts you need instead of downloading the whole file first. You can index frames like a Python list (decoder[10:20]), or retrieve them by timestamp. Decoding runs on CPU, or on NVIDIA GPUs by passing device="cuda". GPU decoding uses NVDEC, a dedicated hardware decoder that is separate from the CUDA cores, so it can run alongside your model’s own GPU work. Samplers retrieve random or regular clips, the way video models are typically trained, and HDR videos can be decoded with high bit-depth output.

Audio. AudioDecoder decodes audio from any format FFmpeg supports, including the audio track of video files. It can resample and change the number of channels as it decodes. There’s also a dedicated, faster WavDecoder for WAV files.

VideoDecoder and AudioDecoder aren’t the only way to decode video and audio: when you need more control or more performance, the new low-level APIs let you build your own decoding pipelines. More on those below.

Images. decode_image detects the format and decodes JPEG, PNG, WebP, GIF, AVIF and HEIC. JPEGs can be decoded on GPU with nvJPEG. On data-center GPUs like the A100 and H100, this can use a dedicated hardware JPEG decoder which, like NVDEC, runs alongside the CUDA cores. get_image_metadata gives you an image’s format, dimensions and more without decoding it, so you can filter or route images before paying for a full decode.

Encoding. The Encoder turns video frame and audio sample tensors into encoded media files like MP4s, with video and audio in the same container. Frames and samples can be added incrementally, as your model generates them, and written to a file or to any file-like object to directly upload them on the cloud. JpegEncoder and PngEncoder turn image tensors into JPEG and PNG files. Video and JPEG encoding also run on GPU.

Installation. pip install torchcodec works on Linux (x86 and aarch64), macOS and Windows, and the Linux wheels come with CUDA support out of the box. TorchCodec uses the FFmpeg you already have on your system, and supports every major version from 4 to 9. TorchCodec is also ABI stable, so a given TorchCodec version keeps working with future PyTorch versions.

The new low-level APIs: build and auto-tune your decoding pipelines

The biggest addition of the last few releases is a new set of low-level decoding APIs for video and audio. They give you control over every step of the decoding process, which enables new features and performance knobs that weren’t available before: multi-threaded and multi-process pipelines, decoding packets that don’t come from a container, direct access to raw YUV data (SDR and HDR), custom color conversion kernels, and more. And all these knobs are a perfect fit for auto-tuning with a coding agent!

Under the hood, decoding happens in three separate stages: demuxing reads compressed packets out of the container (e.g. an MP4 file), decoding decompresses packets into raw frames (often in YUV color space), and conversion turns those raw frames into RGB tensors, or resamples audio. The high-level decoders like VideoDecoder and AudioDecoder run these three stages for you, one after the other, on the calling thread. That’s a good default and that’s enough for a lot of users. But some users need more control, so the low-level APIs expose the three decoding stages, each with their own object:

Stage:     Demuxer  ->  VideoPacketDecoder  ->  ColorConverter
Produces:  Packets      RawFrames               RGB Frames

Stage:     Demuxer  ->  AudioPacketDecoder  ->  AudioConverter
Produces:  Packets      RawAudioSamples         AudioSamples

Here’s what a video pipeline looks like:

from torchcodec.decoders import ColorConverter, Demuxer, VideoPacketDecoder

demuxer = Demuxer("video.mp4", streams="video")
video_stream = demuxer.streams[0]
decoder = VideoPacketDecoder(video_stream.codec_parameters, device="cuda")
converter = ColorConverter(device="cuda")

# Each of these three stages can run on its own thread:
for packet in demuxer:                        # demuxing, on CPU
    for raw_frame in decoder.decode(packet):  # decoding, on GPU
        frame = converter.convert(raw_frame)  # color conversion, on GPU

This is more verbose than VideoDecoder("video.mp4", device="cuda"), but you now have fine-grained control over where and how each stage happens. That unlocks a lot of things the high-level decoders can’t do:

  • Multi-threaded and multi-process pipelines. Every stage releases the GIL, so you can demux, decode and convert on separate threads, and they truly run in parallel. On CPU, overlapping decoding with color conversion is often noticeably faster than running them one after the other. On CUDA you might prefer demuxing separately instead, since color-conversion is comparatively very cheap.
  • Raw YUV data. You can stop after decoding and access the frames’ YUV planes, with no conversion and no copy. 10-bit HDR content comes out as uint16 with every bit intact. This allows you to write your own color conversion kernel, e.g. fusing the color-conversion with the resizing step, or to train your model directly in YUV space! Audio samples are treated similarly: RawAudioSamples are the codec’s own samples, in the codec’s own sample format (e.g. uint8), before they are normalized to float values in [0, 1].
  • Multi-stream decoding. One Demuxer can follow several streams at once, e.g. video and audio, so you can decode both in a single pass over the file.
  • Decoding without a demuxer. A VideoPacketDecoder only needs codec parameters and packets, and you can build both yourself. That means you can decode packets that were demuxed ahead of time and stored in a database, received over the network, or parsed by your own container reader.
  • Live streams. The Demuxer can read from sources that have no duration and no end, like pipes or live streams.
  • Key frames. A scan gives you the exact positions of key frames, which are the cheapest frames you can decode. Use those when you just need a few sampled frames for training!

Auto-tuning your pipeline with an agent

All the knobs above are knobs that a coding agent can auto-tune for you. On the plots below, we had an agent write a video decoding pipeline from scratch and hill-climb its throughput: with the low-level APIs, it ended up 1.4-1.9x faster than a simple baseline, and up to 1.36x faster than an equally tuned VideoDecoder.

The best knobs will be different on your setup, so we encourage you to hill-climb your own! Read more about our experiments and the knobs the agent found in our hill-climbing tutorial. For the rest, the low-level tutorials are the best place to get started. If you try them, please tell us what you think.

Thank you

TorchCodec wouldn’t be where it is without our users and contributors: thank you for the bug reports, the feature requests, the pull requests, and for trusting TorchCodec with your data pipelines!

To get started, check out our documentation, and if you’re still using the decoding or encoding APIs from TorchVision or TorchAudio, refer to our migration guide. And if something you need is missing, let us know on GitHub!

/row /row