video_compress_kit_ios 0.0.3
video_compress_kit_ios: ^0.0.3 copied to clipboard
iOS implementation of the video_compress_kit plugin using AVFoundation & VideoToolbox.
video_compress_kit_ios #
The iOS implementation of video_compress_kit.
Usage #
You should not depend on this package directly. Add the app-facing package instead:
dependencies:
video_compress_kit: ^0.0.1
The iOS implementation is automatically endorsed and registered.
How It Works #
Video Compression Pipeline #
Uses Apple's AVAssetReader / AVAssetWriter pipeline backed by the VideoToolbox hardware encoder on all modern iOS devices. No third-party native libraries — uses only system frameworks, adding ~0 KB to your app binary.
Input MP4
→ AVAssetReader (demux + decode via VideoToolbox)
→ AVAssetWriter (re-encode at target resolution + bitrate)
├─ H.264 Profile: Baseline / Main / High
│ (AVVideoProfileLevelKey)
├─ Bitrate: AVVideoAverageBitRateKey (VBR/CBR)
│ or AVVideoQualityKey (CQ mode)
├─ Color: BT.709 full-range pixel format
│ (kCVPixelFormatType_420YpCbCr8BiPlanarFullRange)
└─ Faststart: shouldOptimizeForNetworkUse = true
→ Audio passthrough via AVAssetReaderTrackOutput
→ Output MP4
Image Compression Pipeline #
Uses Apple's UIImage for decode/scale and ImageIO (CGImageDestination)
for format-specific encoding. WebP support via UTType.webP (iOS 14+).
Input image (JPEG / PNG / HEIC / etc.)
→ UIImage(contentsOfFile:)
→ Scale to maxWidth / maxHeight
(UIGraphicsBeginImageContextWithOptions, proportional)
→ Encode to output format:
├─ JPEG: UIImage.jpegData(compressionQuality:)
├─ PNG: UIImage.pngData()
└─ WebP: CGImageDestination + UTType.webP (iOS 14+)
→ Copy EXIF metadata (if keepExif = true)
(CGImageSource → CGImageDestinationAddImageFromSource)
→ Output file
Per-Session Architecture #
Each compressVideo() call creates a VideoCompressor instance stored in a
[String: VideoCompressor] dictionary protected by NSLock. This enables:
- Concurrent compressions — multiple videos processed in parallel
- Per-session progress — each compressor reports progress tagged with its session ID
- Per-session cancellation — cancel a specific session without affecting others
- Cancel all — iterate the dictionary and cancel every active session
┌────────────────────────────────┐
│ VideoCompressKitIosPlugin │
│ │
│ compressors: [String: VC] │
│ compressorsLock: NSLock │
│ ┌───────────┬───────────┐ │
│ │ "job-1" │ "job-2" │ │
│ │ Compressor│ Compressor│ │
│ └───────────┴───────────┘ │
│ │
│ cancelCompression("job-1") │
│ → compressors["job-1"].cancel│
│ │
│ cancelCompression(nil) │
│ → cancel ALL │
└────────────────────────────────┘
Video Feature Support Matrix #
| Feature | Support | Notes |
|---|---|---|
| H.264 encoding | ✅ | Hardware-accelerated via VideoToolbox |
| Resolution scaling | ✅ | Aspect-ratio preserved |
| Custom bitrate | ✅ | Overrides preset default |
| Custom frame rate | ✅ | |
| Audio passthrough | ✅ | Copies audio track without re-encoding |
| H.264 Baseline profile | ✅ | AVVideoProfileLevelH264BaselineAutoLevel |
| H.264 Main profile | ✅ | AVVideoProfileLevelH264MainAutoLevel |
| H.264 High profile | ✅ | AVVideoProfileLevelH264HighAutoLevel |
| VBR bitrate mode | ✅ | AVVideoAverageBitRateKey (default) |
| CBR bitrate mode | ✅ | AVVideoAverageBitRateKey |
| CQ bitrate mode | ✅ | AVVideoQualityKey (0.0–1.0). Uses quality-based encoding instead of bitrate-based |
| BT.601 color standard | ✅ | Standard pixel format |
| BT.709 color standard | ✅ | kCVPixelFormatType_420YpCbCr8BiPlanarFullRange |
| Faststart (moov atom) | ✅ | shouldOptimizeForNetworkUse = true |
| Per-session progress | ✅ | Event channel with {sessionId, progress} |
| Per-session cancel | ✅ | Dictionary with NSLock keyed by sessionId |
| Delete original file | ✅ |
Image Feature Support Matrix #
| Feature | Support | Notes |
|---|---|---|
| JPEG output | ✅ | UIImage.jpegData(compressionQuality:) |
| PNG output | ✅ | UIImage.pngData() (lossless, quality ignored) |
| WebP output | ✅* | CGImageDestination + UTType.webP. Requires iOS 14+ |
| Quality control | ✅ | 1–100 for JPEG/WebP (mapped to 0.0–1.0 internally) |
| Resize (maxWidth / maxHeight) | ✅ | Proportional scaling via UIGraphicsBeginImageContextWithOptions |
| EXIF metadata copy | ✅ | ImageIO — CGImageSource → CGImageDestinationAddImageFromSource with original properties |
WebP on iOS: WebP encoding requires iOS 14.0 or later. On iOS 13.x, requesting
ImageFormat.webpwill return an error. The decoder supports WebP on iOS 14+ viaUTType.webP.
Requirements #
| Requirement | Value |
|---|---|
| Min iOS | 13.0 |
| Swift | 5.0+ |
| Frameworks | AVFoundation, CoreMedia, VideoToolbox, ImageIO, UIKit |
Native APIs Used #
| API | Purpose | Min iOS |
|---|---|---|
AVAssetReader |
Demux + decode video/audio tracks | 4.1 |
AVAssetWriter |
Encode and mux output MP4 | 4.1 |
AVAssetReaderTrackOutput |
Read video/audio samples | 4.1 |
AVAssetWriterInput |
Write encoded samples | 4.1 |
AVVideoCompressionPropertiesKey |
Configure H.264 profile, bitrate, quality | 4.0 |
shouldOptimizeForNetworkUse |
Faststart (moov atom at front) | 4.1 |
UIImage |
Decode/scale images | 2.0 |
CGImageDestination |
Encode images to JPEG/PNG/WebP | 4.0 |
CGImageSource |
Read image metadata (EXIF) | 4.0 |
UTType.webP |
WebP format identifier | 14.0 |
AVURLAsset |
Load video metadata | 4.0 |
AVAssetImageGenerator |
Extract thumbnail frames | 4.0 |
License #
MIT — see LICENSE.