Skip to content

fix(ios): honor explicit bitrate in compressVideo - #4

Merged
thangdevalone merged 1 commit into
mainfrom
fix/ios-bitrate-parameter
Sep 15, 2026
Merged

thangdevalone merged 1 commit into
mainfrom
fix/ios-bitrate-parameter

Conversation

@thangdevalone

Copy link
Copy Markdown
Owner

Summary

Fixes #3 - the bitrate parameter in compressVideo was declared but completely unused on iOS.

Root Cause

iOS used AVAssetExportSession for all compression paths, which only supports Apple's built-in quality presets (Low/Medium/High/1080p/720p etc.) and does not accept custom bitrate values. The bitrate parameter was accepted in the function signature but never referenced in the function body.

Solution

Added a new AVAssetWriter + AVAssetReader pipeline (compressWithBitrate) that activates when:

  • bitrate > 0 AND
    • targetSizeInMB is not set (<= 0)
      This mirrors how Android handles explicit bitrate via Transformer's VideoEncoderSettings.setBitrate().

Key implementation details:

  • H.264 encoding with AVVideoAverageBitRateKey for precise bitrate control
    • Audio passthrough (no re-encode) unless muteAudio: true
    • maxWidth support via CIImage GPU-accelerated resize when scaling is needed
    • Rotation preserved via preferredTransform on the writer input
    • Anti-inflation fallback - reverts to original file if encoder produces larger output
    • Existing paths unchanged - quality preset and targetSizeInMB smart compress are not affected

Behavior matrix:

Parameters Before After
{ bitrate: 4000000 } Ignored, uses medium preset Encodes at 4Mbps
{ quality: "low" } Low preset Unchanged
{ targetSizeInMB: 5 } Smart compress Unchanged
{ bitrate: 4000000, muteAudio: true } Bitrate ignored 4Mbps + no audio

Testing

Needs macOS build + device testing with the test cases above.

The bitrate parameter was declared but never used in VideoProcessor.swift
compressVideo(). AVAssetExportSession does not support custom bitrate,
so this adds an AVAssetWriter/AVAssetReader pipeline that activates when
bitrate > 0 (and targetSizeInMB is not set).

- Encodes H.264 with AVVideoAverageBitRateKey for precise bitrate control
- Audio passthrough (no re-encode) unless muteAudio is true
- Supports maxWidth scaling via CIImage GPU-accelerated resize
- Preserves rotation metadata via preferredTransform
- Includes anti-inflation fallback (reverts if output > original size)
- Existing quality preset and targetSizeInMB paths are unchanged

Fixes #3
@thangdevalone
thangdevalone merged commit acc1042 into main Sep 15, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[ios] bitrate is ignored in compressVideo

1 participant