ff-decode
Safe, high-level video and audio decoding — no unsafe code required.
This crate provides frame-by-frame video/audio decoding, efficient seeking, and thumbnail generation. All APIs are safe — FFmpeg internals are fully encapsulated so you never need to write unsafe code.
Features
- Video Decoding: Frame-by-frame decoding with Iterator pattern
- Audio Decoding: Sample-level audio extraction
- Seeking: Fast keyframe and exact seeking without file re-open
- Thumbnails: Efficient thumbnail generation for timelines
- Hardware Acceleration: Optional NVDEC, QSV, AMF, VideoToolbox, VAAPI support
- Frame Pooling: Memory reuse for reduced allocation overhead
Quick Start
Add to your Cargo.toml:
[]
= "0.1"
= "0.1"
Usage Examples
Video Decoding
use ;
use PixelFormat;
use Duration;
Audio Decoding
use AudioDecoder;
use SampleFormat;
Hardware Acceleration
use ;
Seeking
use ;
use Duration;
Thumbnail Generation
use VideoDecoder;
use Duration;
Frame Pooling
use ;
use Arc;
Performance
Benchmarking
Run performance benchmarks with:
# Run all benchmarks
# Run specific benchmark group
# Quick benchmarks (faster, less precise)
Benchmark Results
Typical performance on modern hardware (actual results may vary):
| Operation | Time | Notes |
|---|---|---|
| Keyframe seek | ~12ms | Fast seeking to nearest keyframe |
| Exact seek | ~20ms | Frame-accurate seeking |
| Single frame decode | ~0.3ms | H.264 1080p |
| Thumbnail (320x180) | ~12ms | Including seek and scale |
| Sequential decode (100 frames) | ~33ms | ~0.33ms per frame |
Seek Performance
One of the key features of ff-decode is efficient seeking without file re-opening. The benchmark seek_repeated/scrubbing_5_positions demonstrates this - 5 seeks complete in ~100ms (20ms per seek), which would be much slower if the file was being re-opened each time.
Platform Support
| Platform | Status | Hardware Acceleration |
|---|---|---|
| Windows | ✅ Tested | NVDEC, QSV, AMF |
| macOS | ✅ Tested | VideoToolbox |
| Linux | ✅ Tested | VAAPI, NVDEC, QSV |
Error Handling
All operations return Result<T, DecodeError>:
use ;
Error types include:
DecodeError::FileNotFound- File doesn't existDecodeError::NoVideoStream- No video stream in fileDecodeError::NoAudioStream- No audio stream in fileDecodeError::UnsupportedCodec- Codec not supportedDecodeError::DecodingFailed- Decoding errorDecodeError::SeekFailed- Seek operation failedDecodeError::HwAccelUnavailable- Hardware acceleration unavailableDecodeError::EndOfStream- End of stream reached
Module Structure
video- Video decoder for extracting video framesaudio- Audio decoder for extracting audio frameserror- Error types for decoding operationspool- Frame pool trait for memory reuse
Architecture
This crate is part of the ff-* suite of crates that provide a safe Rust wrapper around FFmpeg:
- ff-sys - Low-level FFI bindings (internal)
- ff-format - Common types (VideoFrame, AudioFrame, etc.)
- ff-probe - Metadata extraction
- ff-decode - Decoding (this crate)
- ff-encode - Encoding
- ff-filter - Filters and effects
Performance Characteristics
Typical performance on modern hardware (see benchmarks with cargo bench -p ff-decode):
- Frame decode: ~0.3ms for 1080p H.264 (software decoding)
- Seek (keyframe): ~12ms per seek (no file re-open)
- Seek (exact): ~20ms per seek (includes frame skipping)
- Thumbnail generation: ~12ms per thumbnail (320x180)
- Scrubbing (5 positions): ~100ms total (~20ms per position)
Hardware acceleration can significantly improve decode performance.
License
Licensed under either of:
- MIT license
- Apache License, Version 2.0
at your option.