DEV Community

Enes
Enes

Posted on

Building Shrink Video: Hardware-Accelerated 4K Video Compression on iOS with VideoToolbox

The Challenge: Mobile 4K Video Bloat

Modern iPhones shoot stunning 4K 60fps video, but sending clips via messaging apps is a constant headache:

  • WhatsApp: 16MB file limit (expanded to 64MB under specific document attachments).
  • Discord: 25MB standard upload limit.
  • Email: 20MB-25MB hard attachment ceilings.

Most third-party video compressor apps on iOS suffer from two critical architectural flaws:

  1. They upload user videos to remote cloud servers (severe privacy invasion and slow upload latency).
  2. They use crude "Low / Medium / High" presets that crush bitrate and render pixelated artifacts.

To solve this, we engineered Shrink Video: Compressor entirely on-device using Apple's low-level VideoToolbox framework and AVFoundation.


Technical Architecture

1. Zero-Cloud Hardware Re-Encoding

By leveraging Apple Silicon's dedicated media engine via VTCompressionSession and AVAssetWriter:

  • Videos never leave the iPhone RAM.
  • Compression runs 100% offline in Airplane Mode.
  • Native hardware HEVC (H.265) achieves up to 80% file size reduction while retaining crisp edge detail.

2. Deterministic Bitrate Target Slider

Instead of guessing quality presets, users drag a target size slider (e.g., exactly 15.5 MB for WhatsApp). The app calculates the exact video bitrate target:

let audioBitrate: Double = 128_000 // 128 kbps AAC
let targetSizeBytes: Double = targetMB * 1024 * 1024
let totalSeconds = CMTimeGetSeconds(asset.duration)

let availableVideoBytes = targetSizeBytes - ((audioBitrate / 8) * totalSeconds)
let targetVideoBitrate = max(500_000, (availableVideoBytes * 8) / totalSeconds)
Enter fullscreen mode Exit fullscreen mode

3. Smart Downscaling Pipeline

Downscaling from 4K (3840x2160) to 1080p or 720p runs through Metal-accelerated bilinear filtering to preserve high-frequency texture information.


Results & App Store Availability

Have you implemented custom VideoToolbox pipelines in Swift? Let's discuss transcoding trade-offs in the comments!

Top comments (0)