Expand description
§videotoolbox
Safe, zero-runtime-dependency Rust bindings for Apple’s VideoToolbox framework — hardware-accelerated H.264, HEVC, and ProRes codecs on macOS.
Status: experimental. Encoder is functional; decoder, pixel transfer, and multi-pass support are planned.
§Features
- Hardware-accelerated encoding — H.264, HEVC, and
ProRes422/4444 - Direct
IOSurfaceinput — encode zero-copy from screencapturekit / camera output viaapple-cf::iosurface - Builder pattern — fluent configuration of bitrate, frame rate, keyframe interval, real-time mode
- Pure C bindings — no Swift bridge, no
bindgen, no procedural macros - Single dependency — only
apple-cffor shared types
§Why not bindgen?
The full VideoToolbox C surface is ~200 symbols, but the useful set for an encoder is closer to 15. Hand-writing those declarations gives us:
- No build-time dependency on
clang - Type-safe Rust enums for codec types (instead of raw
u32four-character codes) - Builder APIs that map ergonomically to VT’s
CFDictionaryproperty bag
§Requirements
- macOS 13.0+
- Apple Silicon or Intel Mac with hardware video encoder
§Quick start
use videotoolbox::prelude::*;
use apple_cf::iosurface::{IOSurface, IOSurfaceLockOptions};
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Allocate a 1920×1080 BGRA IOSurface.
let surface = IOSurface::create(1920, 1080, u32::from_be_bytes(*b"BGRA"), 4)
.ok_or("failed to allocate")?;
// Build a real-time H.264 encoder.
let encoder = CompressionSession::builder(1920, 1080, Codec::H264)
.with_real_time(true)
.with_average_bit_rate(8_000_000)
.with_expected_frame_rate(60.0)
.with_max_keyframe_interval(120)
.build()?;
// Encode one frame and inspect the resulting CMSampleBuffer.
let encoded = encoder.encode(&surface, (0, 60))?;
println!("Got {} bytes of H.264", encoded.data.len());
if let Some(sb) = encoded.cm_sample_buffer() {
// Hand `sb` straight to avassetwriter::Writer::append_sample for
// zero-copy muxing — no raw pointer hand-off needed.
let _ = sb.is_valid();
}
Ok(())
}§Composes with the rest of the doom-fish stack
screencapturekit-rs ──► IOSurface ──► videotoolbox-rs ──► H.264 bytes
↓
avassetwriter-rs (future)
↓
.mp4 file§Roadmap
-
VTCompressionSession(encoder) -
VTDecompressionSession(decoder) -
VTPixelTransferSession(pixel format / colour space conversion) -
VTPixelRotationSession -
VTMultiPassStorage+VTFrameSilo(two-pass encoding) -
VTHDRPerFrameMetadataGenerationSession(Dolby Vision metadata) -
VTFrameProcessorcapability queries (super-resolution / optical flow detection) -
VTFrameProcessorpipeline (super-resolution + motion blur + temporal noise + frame-rate conversion + optical flow + 2 low-latency variants) -
VTMotionEstimationSession -
VTRAWProcessingSession(with parameter introspection) -
VTProfessionalVideoWorkflowdecoder/encoder registration -
VTCreateCGImageFromCVPixelBuffer - HEVC profile-level helpers
-
Async encode API via
VTCompressionSessionEncodeFrameWithOutputHandler
§License
Licensed under either of Apache-2.0 or MIT at your option.
§API Documentation
Safe, zero-runtime-dependency Rust bindings for Apple’s VideoToolbox framework — hardware-accelerated H.264, HEVC, and ProRes codecs on macOS.
Unlike the rest of the doom-fish suite, VideoToolbox is a pure C framework,
so this crate does not ship a Swift bridge — all bindings are direct
extern "C" declarations against the system framework.
§Quick start
use videotoolbox::prelude::*;
use apple_cf::iosurface::IOSurface;
let surface = IOSurface::create(1920, 1080, u32::from_be_bytes(*b"BGRA"), 4)
.ok_or("failed to allocate IOSurface")?;
let encoder = CompressionSession::builder(1920, 1080, Codec::H264)
.with_real_time(true)
.with_average_bit_rate(8_000_000)
.with_expected_frame_rate(60.0)
.build()?;
let encoded = encoder.encode(&surface, (0, 60))?;
println!("Got {} bytes of H.264", encoded.data.len());Re-exports§
pub use error::VTError;pub use encoder_list::available_video_encoders;pub use encoder_list::VideoEncoder;pub use hdr_metadata::HdrMetadataSession;pub use multipass::FrameSilo;pub use multipass::MultiPassStorage;pub use session::Codec;pub use transfer::PixelRotationSession;pub use transfer::PixelTransferSession;pub use transfer::Rotation;pub use utilities::create_cg_image_from_pixel_buffer;pub use utilities::register_professional_workflow_decoders;pub use utilities::register_professional_workflow_encoders;pub use frame_processor::frame_processor_capabilities;frame_processorpub use frame_processor::super_resolution_supported_scale_factors;frame_processorpub use frame_processor::FrameProcessor;frame_processorpub use frame_processor::FrameProcessorCapabilities;frame_processorpub use motion_estimation::MotionEstimationSession;frame_processorpub use raw_processing::RawProcessingParameter;frame_processorpub use raw_processing::RawProcessingSession;frame_processorpub use compression::CompressionSession;compressionpub use compression::CompressionSessionBuilder;compressionpub use compression::EncodedFrame;compression
Modules§
- compression
compression CompressionSession— hardware H.264/HEVC/ProRes encoder.- decompression
DecompressionSession— hardware H.264/HEVC/ProRes decoder.- encoder_
list available_video_encoders— enumerate Apple’s installed video encoders. WrapsVTCopyVideoEncoderList.- error
- Errors produced by
VideoToolboxAPIs. - ffi
- Raw
extern "C"declarations against<VideoToolbox/VideoToolbox.h>,<CoreMedia/CoreMedia.h>,<CoreVideo/CVPixelBuffer.h>, and<CoreFoundation/CoreFoundation.h>. - frame_
processor frame_processor VTFrameProcessorcapability queries —isSupported+ supported scale factors via the newframe_processorSwift bridge.- hdr_
metadata VTHDRPerFrameMetadataGenerationSession— generate Dolby Vision per-frame HDR metadata (macOS 15+).- motion_
estimation frame_processor VTMotionEstimationSession— between-frame motion-vector estimation backed by the Apple Neural Engine (macOS 26+).- multipass
VTFrameSilo+VTMultiPassStorage— multi-pass video encoding storage.- prelude
- Common imports for users of this crate.
- raw_
processing frame_processor VTRAWProcessingSession—ProResRAW /CinemaDNGdecoder with per-frame parameter controls (macOS 15+).- session
- Video codecs supported by
VideoToolbox. - transfer
PixelTransferSessionandPixelRotationSession— Apple’s zero-copy pixel-format conversion / scaling / rotation engines.- utilities
VTUtilities+VTProfessionalVideoWorkflowhelpers.