Skip to content

Commit ccb8fa9

Browse files
committed
fix: prevent bitrate override during smart compression and implement file inflation fallback for iOS
1 parent 522a504 commit ccb8fa9

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

android/src/main/java/com/mediatoolkit/VideoProcessor.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,10 @@ internal object VideoProcessor {
542542
}
543543
}
544544

545-
if (bitrate > 0) {
545+
// Explicit bitrate override — only when NOT in smart compress mode.
546+
// When targetSizeInMB is set, the budget calculation already determines
547+
// the optimal bitrate; overriding it would break the size constraint.
548+
if (bitrate > 0 && targetSizeInMB <= 0) {
546549
computedBitrate = bitrate
547550
}
548551

ios/VideoProcessor.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -902,6 +902,22 @@ class VideoProcessor: NSObject {
902902
NSLog("[MediaToolkit] Smart Compress Result: target=%.1fMB, actual=%.2fMB (%d%%)", targetMB, actualMB, pct)
903903
}
904904
}
905+
906+
// Fallback: If encoder inflates the file beyond original size,
907+
// revert to original to prevent making it worse. (Matches Android behavior)
908+
if !muteAudio,
909+
let sourceURL = (asset as? AVURLAsset)?.url, sourceURL.isFileURL,
910+
let origAttr = try? FileManager.default.attributesOfItem(atPath: sourceURL.path),
911+
let origSize = origAttr[.size] as? Int64, origSize > 0,
912+
let outAttr = try? FileManager.default.attributesOfItem(atPath: out),
913+
let outSize = outAttr[.size] as? Int64, outSize > origSize {
914+
let origMB = Double(origSize) / (1024.0 * 1024.0)
915+
let outMB = Double(outSize) / (1024.0 * 1024.0)
916+
NSLog("[MediaToolkit] Encoder inflated file from %.1fMB to %.1fMB. Reverting to original.", origMB, outMB)
917+
try? FileManager.default.removeItem(at: outURL)
918+
try? FileManager.default.copyItem(at: sourceURL, to: outURL)
919+
}
920+
905921
let durationMs = asset.duration.seconds * 1000
906922
completion(videoResult(path: out, asset: asset, trimmed: durationMs), nil)
907923
default:

0 commit comments

Comments
 (0)