I'm currently exporting a video in the following way:
let exporter = AVAssetExportSession.init(asset: mixComposition, presetName: AVAssetExportPreset1280x720)
exporter?.outputURL = outputPath
exporter?.outputFileType = AVFileType.mp4
exporter?.shouldOptimizeForNetworkUse = true
exporter?.videoComposition = mainCompositionInst
A 15s video consumes about 20MB in data. Comparing this to Snapchat's 2MB videos, this number seems totally unacceptable.
I already reduced the quality of the export- and capture session (1280x720).
The video is filmed on a custom camera. UIImagePickerController
is not used.
AVAssetExportSession is used with default settings.
Is there any way I can reduce the size of my videos? Thanks a lot!
EDIT 1: I tried to use this library: https://cocoapods.org/pods/NextLevelSessionExporter
Unfortunately, this creates sizing problems and removes my audio:
// Creating exporter
let exporter = NextLevelSessionExporter(withAsset: mixComposition)
exporter.outputURL = outputPath
exporter.outputFileType = AVFileType.mp4
exporter.videoComposition = mainCompositionInst
let compressionDict: [String: Any] = [
AVVideoAverageBitRateKey: NSNumber(integerLiteral: 2500000),
AVVideoProfileLevelKey: AVVideoProfileLevelH264BaselineAutoLevel as String,
]
exporter.videoOutputConfiguration = [
AVVideoCodecKey: AVVideoCodecType.h264,
AVVideoWidthKey: NSNumber(integerLiteral: 1280),
AVVideoHeightKey: NSNumber(integerLiteral: 720),
AVVideoScalingModeKey: AVVideoScalingModeResizeAspectFill,
AVVideoCompressionPropertiesKey: compressionDict
]
exporter.audioOutputConfiguration = [
AVFormatIDKey: kAudioFormatMPEG4AAC,
AVEncoderBitRateKey: NSNumber(integerLiteral: 128000),
AVNumberOfChannelsKey: NSNumber(integerLiteral: 2),
AVSampleRateKey: NSNumber(value: Float(44100))
]
To reduce file size try these properties for setting up HEVC
codec (use cocoa pod NextLevelSessionExporter
):
let compressionDict: [String: Any] = [
AVVideoAverageBitRateKey: NSNumber(integerLiteral: 2500000), //lower it if you wish
AVVideoProfileLevelKey: AVVideoProfileLevelH264BaselineAutoLevel as String,
]
exporter.videoOutputConfiguration = [
AVVideoCodecKey : AVVideoCodecType.hevc,
AVVideoWidthKey : NSNumber(integerLiteral: 1280),
AVVideoHeightKey: NSNumber(integerLiteral: 720),
AVVideoScalingModeKey: AVVideoScalingModeResizeAspectFill,
AVVideoCompressionPropertiesKey: compressionDict
]
You need to upgrade to macOS High Sierra
and iOS 11
in order to use HEVC
video codec. But if you can't use HEVC
for some reason, use regular H.264
with lower bitrate.
AVVideoCodecKey : AVVideoCodecType.h264:
Also, look at this SO post about video bitrate in iOS.