|
| 1 | +# cap-media-encoders |
| 2 | + |
| 3 | +A comprehensive collection of media encoders for various output formats, designed to work seamlessly with the Cap media processing pipeline. The crate provides unified interfaces for encoding captured audio and video data into different formats using multiple underlying media frameworks. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +This crate serves as a unified interface for encoding captured media into various output formats, with FFmpeg providing cross-platform support and AVFoundation offering optimized macOS-specific encoding. All encoders support real-time frame queuing and proper finalization for streaming applications. |
| 8 | + |
| 9 | +## Available Encoders |
| 10 | + |
| 11 | +### FFmpeg-based Encoders |
| 12 | + |
| 13 | +**Video Encoders:** |
| 14 | +- `H264Encoder` - H.264/AVC video encoding with configurable quality presets and bitrate control |
| 15 | +- `MP4File` - Complete MP4 container handling H.264 video + audio stream muxing |
| 16 | + |
| 17 | +**Audio Encoders:** |
| 18 | +- `AACEncoder` - AAC audio encoding at 320kbps with automatic resampling and format conversion |
| 19 | +- `OpusEncoder` - Opus audio encoding at 128kbps optimized for voice and music |
| 20 | +- `OggFile` - Ogg container wrapper specifically designed for Opus audio streams |
| 21 | + |
| 22 | +### AVFoundation-based Encoders (macOS only) |
| 23 | + |
| 24 | +**Video + Audio:** |
| 25 | +- `MP4AVAssetWriterEncoder` - Native macOS encoder using AVAssetWriter for hardware-accelerated MP4 encoding |
| 26 | + - Hardware acceleration support |
| 27 | + - Pause/resume functionality for recording sessions |
| 28 | + - Real-time encoding optimizations |
| 29 | + |
| 30 | +### Standalone Encoders |
| 31 | + |
| 32 | +**Animation:** |
| 33 | +- `GifEncoderWrapper` - GIF animation encoder with advanced features: |
| 34 | + - Floyd-Steinberg dithering for quality color reduction |
| 35 | + - Custom 256-color palette with grayscale fallback |
| 36 | + - Configurable frame delay and infinite loop support |
| 37 | + |
| 38 | +## Common Interface |
| 39 | + |
| 40 | +### AudioEncoder Trait |
| 41 | + |
| 42 | +All audio encoders implement the `AudioEncoder` trait providing: |
| 43 | +- `queue_frame()` - Queue audio frames for encoding |
| 44 | +- `finish()` - Finalize encoding and flush remaining data |
| 45 | +- `boxed()` - Convert to boxed trait object for dynamic dispatch |
| 46 | + |
| 47 | +## Key Features |
| 48 | + |
| 49 | +### Format Handling |
| 50 | +- **Automatic conversion**: Seamless pixel format and sample format conversion between input and encoder requirements |
| 51 | +- **Resampling**: Built-in audio resampling for sample rate and format mismatches |
| 52 | +- **Validation**: Input validation with detailed error reporting |
| 53 | + |
| 54 | +### Performance Optimizations |
| 55 | +- **Hardware acceleration**: AVFoundation encoder leverages macOS VideoToolbox |
| 56 | +- **Threading**: Multi-threaded encoding support where available |
| 57 | +- **Real-time processing**: Optimized for live capture and streaming scenarios |
| 58 | + |
| 59 | +### Quality Control |
| 60 | +- **Configurable presets**: H.264 encoding supports Slow, Medium, and Ultrafast presets |
| 61 | +- **Bitrate control**: Intelligent bitrate calculation based on resolution and frame rate |
| 62 | +- **Format-specific optimizations**: Each encoder tuned for its target format characteristics |
| 63 | + |
| 64 | +## Dependencies |
| 65 | + |
| 66 | +- `cap-media-info` - Media stream information structures |
| 67 | +- `ffmpeg` - Cross-platform media processing (video and audio encoding) |
| 68 | +- `gif` - GIF image format encoding |
| 69 | +- `cidre` (macOS only) - AVFoundation bindings for native encoding |
| 70 | + |
| 71 | +## Integration |
| 72 | + |
| 73 | +The crate integrates with: |
| 74 | +- **cap-media-info** for media stream configuration and format definitions |
| 75 | +- **FFmpeg ecosystem** for broad codec and container support |
| 76 | +- **macOS AVFoundation** for optimized native encoding on Apple platforms |
| 77 | +- **Raw media pipelines** for direct frame processing |
| 78 | + |
| 79 | +## Error Handling |
| 80 | + |
| 81 | +Comprehensive error types for each encoder: |
| 82 | +- `H264EncoderError` - Video encoding failures |
| 83 | +- `AACEncoderError` / `OpusEncoderError` - Audio encoding issues |
| 84 | +- `GifEncodingError` - Animation encoding problems |
| 85 | +- `InitError` - Encoder initialization failures |
| 86 | + |
| 87 | +Each error type provides detailed context about failures including codec availability, format compatibility, and resource constraints. |
| 88 | + |
| 89 | +## Use Cases |
| 90 | + |
| 91 | +This crate is designed for applications requiring: |
| 92 | +- **Screen recording** with multiple output formats |
| 93 | +- **Live streaming** with real-time encoding |
| 94 | +- **Video conferencing** with adaptive quality |
| 95 | +- **Media conversion** between different formats |
| 96 | +- **Animation creation** from frame sequences |
| 97 | + |
| 98 | +The modular design allows applications to choose the most appropriate encoder for their specific requirements, whether prioritizing quality, performance, or compatibility. |
0 commit comments