PyTorch's TorchCodec 1.0 Unlocks 1.9x Faster Video Decoding for AI Pipelines
PyTorch's media library hits stable, exposing low-level demux/decode/convert stages that a coding agent can auto-tune up to 1.9x faster than baseline.
Takeaways
- TorchCodec 1.0 ships stable video, audio, and image decode/encode APIs for PyTorch tensors, CPU and GPU.
- New low-level APIs split pipelines into Demuxer, PacketDecoder, ColorConverter stages, each GIL-free.
- Coding-agent auto-tuning hit 1.4-1.9x speedups over baseline, up to 1.36x over tuned VideoDecoder.
- Already used in production by Hugging Face datasets, transformers, LeRobot, vLLM, and SGLang.
- Decode raw H.264/HEVC without a container, from cameras, databases, or network sources.
- Single py3-none wheel per platform, CUDA 13.2/13.4 support, requires torch 2.11+ and Python 3.11+.
TorchCodec 1.0 establishes stable APIs for decoding and encoding video, audio, and images as PyTorch tensors. The release also introduces low-level primitives for controlling demuxing, decoding, and format conversion independently, giving developers more options for parallelism, custom kernels, and hardware-specific tuning.
Production use predates the 1.0 release by more than two years. Hugging Face libraries including datasets, transformers, and LeRobot use TorchCodec to decode dataset samples and model inputs. vLLM handles video, audio, and image inputs with it on CPUs and GPUs, while SGLang uses it as its preferred video and audio decoder. Version 1.0 formalizes the API contract for an established dependency in the multimodal stack.
Start with the high-level API
Multimodal training and inference systems use TorchCodec as a bridge between compressed media and tensors. Its high-level interfaces cover common loading, sampling, and encoding workflows:
| Capability | API | Behavior |
|---|---|---|
| Video decoding | VideoDecoder |
Reads files, byte buffers, and file-like objects. It supports frame indexing such as decoder[10:20], timestamp-based access, and remote reads that fetch required file segments. |
| GPU video decoding | device="cuda" |
Uses NVIDIA NVDEC hardware, which operates separately from CUDA cores and can overlap with model computation. |
| Audio decoding | AudioDecoder |
Decodes formats supported by FFmpeg, including audio tracks stored inside video containers. |
| Image decoding | decode_image |
Detects and decodes JPEG, PNG, WebP, GIF, AVIF, and HEIC. NVIDIA GPUs can decode JPEG files through nvJPEG. |
| Media encoding | Encoder |
Writes video-frame and audio-sample tensors into media files such as MP4, including containers with synchronized video and audio. |
Compatibility at a glance
Installation uses pip install torchcodec. TorchCodec links against the system FFmpeg installation and supports major FFmpeg versions 4 through 9.
| Python | 3.11 or newer |
|---|---|
| PyTorch | 2.11 or newer |
| Platforms | Linux on x86 and AArch64, macOS, and Windows |
| Linux GPU wheels | CUDA support included, with CUDA 13.2 and 13.4 wheels matching PyTorch 2.15 |
Pull the pipeline apart
The major addition in 1.0 is a low-level API that exposes the three stages normally managed by VideoDecoder. Demuxing extracts compressed packets from a container such as MP4. Decoding expands those packets into raw frames, commonly represented in YUV color space. Conversion then produces RGB tensors or resampled audio.
The high-level decoder runs all three stages on the calling thread. The new Demuxer, VideoPacketDecoder, and ColorConverter objects let applications schedule and connect them separately:
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")
for packet in demuxer: # demux on CPU
for raw_frame in decoder.decode(packet): # decode on GPU
frame = converter.convert(raw_frame) # convert on GPU
Control at each stage
- Parallel execution: Each stage releases Python’s Global Interpreter Lock, so applications can demux, decode, and convert on separate threads. CPU workloads can overlap decoding with color conversion instead of processing the stages sequentially.
- Raw YUV access: Applications can stop after decoding and access the brightness and color planes without conversion or copying. Ten-bit HDR frames use
uint16tensors that preserve the source precision. This supports custom color kernels, fused conversion-and-resize operations, and models trained directly on YUV data. - Packet-level decoding:
VideoPacketDecoderneeds codec parameters and compressed packets. Those inputs can come from a database, a network stream, or a custom container parser. - Containerless input: Raw H.264 and HEVC streams from sources such as cameras can be decoded without an MP4 or another media container.
- Keyframe scanning: The scanner reports exact keyframe positions. Applications that sample a small number of frames can seek to independently decodable frames and reduce unnecessary work.
Tuning follows the hardware
TorchCodec’s team tested the low-level API by asking a coding agent to build a video-decoding pipeline and iteratively retain changes that increased throughput. Depending on the hardware configuration, the resulting pipeline ran 1.4 to 1.9 times faster than a simple baseline and up to 1.36 times faster than an equivalently tuned VideoDecoder.
Results differed across the 8-CPU, 96-CPU, and A100-plus-8-CPU systems because each configuration shifts the bottleneck among input, CPU processing, GPU decoding, conversion, and data transfer. Teams evaluating the low-level API should benchmark representative files, sampling patterns, thread counts, queue depths, and target hardware. The published figures describe those tested workloads rather than a guaranteed speedup.
Migration details with consequences
The release notes include several changes that affect dataset inspection, packaging, image handling, and error behavior:
get_image_metadata()reads image metadata without performing a full decode, allowing pipelines to filter or route samples before spending compute on pixel data.- Platform wheels now use the
py3-nonetag, so one wheel supports every compatible Python version on that platform, including free-threaded Python builds. - EXIF orientation handling now matches Pillow’s
ImageOps.exif_transpose(). - NVDEC sequential decoding now returns the requested frame instead of occasionally returning a later one. The release also fixes several audio sample-boundary errors.
- Exceptions raised by a file-like object’s
read(),write(), orseek()method now surface asRuntimeError. This backward-incompatible change may require updates to exception handling.
Projects still using TorchVision or TorchAudio for media loading now have an official migration path through TorchCodec. Before upgrading, maintainers should verify the Python, PyTorch, FFmpeg, and CUDA requirements, then test file-like object failures and output parity on representative media.
Teams limited by media loading can begin with VideoDecoder and profile the full input path. Workloads that remain constrained by demuxing, decoding, conversion, or frame sampling can adopt the low-level objects selectively and tune them against the deployment hardware.