You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS Swift:如何缩小AVAssetExportSession导出的视频体积?

如何缩小iOS导出视频的体积?

Hey there! Let's fix that bloated video size issue—20MB for 15 seconds of 720p footage is way too large, especially compared to Snapchat's 2MB output. I’ve got several practical solutions tailored to your setup:


1. 精准压低视频比特率(核心优化)

Snapchat’s tiny file sizes come from using just-enough bitrates without sacrificing too much visual quality. Your current 2.5Mbps (2500000) setting is still high for social media use. Try dropping it to 1Mbps (1000000) or even 800kbps first—for 15 seconds of 720p 30fps video, that would land you around 1.8MB, plus ~0.2MB for audio, totaling right around 2MB (matching Snapchat’s size).

2. 修复NextLevelSessionExporter的尺寸&音频问题

If you want to stick with NextLevelSessionExporter, here’s how to fix the issues you ran into:

  • Size distortion: Replace AVVideoScalingModeResizeAspectFill with AVVideoScalingModeResizeAspectFit to avoid stretching your video. Also double-check that your mixComposition’s native resolution matches the 1280x720 you’re exporting to—unnecessary scaling can cause weird sizing and extra file size.
  • Missing audio: First confirm your mixComposition actually includes an audio track (make sure your custom camera captured audio and added it to the composition). Then simplify your audio configuration temporarily to rule out misconfigured parameters:
    exporter.audioOutputConfiguration = [
        AVFormatIDKey: kAudioFormatMPEG4AAC,
        AVEncoderBitRateKey: NSNumber(integerLiteral: 128000)
    ]
    
    If audio works with this minimal setup, you can add back other parameters like channel count and sample rate one by one.

3. 用原生AVAssetExportSession实现压缩

You don’t need a third-party library to get small files—tweak the native exporter’s settings directly:

let exporter = AVAssetExportSession(asset: mixComposition, presetName: AVAssetExportPreset1280x720)
exporter?.outputURL = outputPath
exporter?.outputFileType = AVFileType.mp4
exporter?.shouldOptimizeForNetworkUse = true
exporter?.videoComposition = mainCompositionInst

// Add custom video compression settings
let videoCompression: [String: Any] = [
    AVVideoAverageBitRateKey: 1000000, // Start here, adjust based on quality tests
    AVVideoProfileLevelKey: AVVideoProfileLevelH264BaselineAutoLevel,
    AVVideoMaxKeyFrameIntervalKey: 60 // Reduce redundant frames
]

let videoSettings: [String: Any] = [
    AVVideoCodecKey: AVVideoCodecType.h264,
    AVVideoWidthKey: 1280,
    AVVideoHeightKey: 720,
    AVVideoCompressionPropertiesKey: videoCompression
]
exporter?.videoSettings = videoSettings

// Add audio compression settings
let audioSettings: [String: Any] = [
    AVFormatIDKey: kAudioFormatMPEG4AAC,
    AVEncoderBitRateKey: 128000,
    AVNumberOfChannelsKey: 2,
    AVSampleRateKey: 44100
]
exporter?.audioSettings = audioSettings

// Start export
exporter?.exportAsynchronously(completionHandler: {
    // Handle completion/failure
})

4. 额外优化技巧

  • Lower the frame rate: If your custom camera is capturing at 60fps, drop it to 30fps. This cuts the file size in half with barely noticeable visual difference for social media.
  • Optimize capture settings: Don’t just compress post-capture—set your camera’s capture session to use lower bitrates upfront. This reduces the raw file size before you even start exporting.
  • Trim unused tracks: Make sure your mixComposition only includes the video and audio tracks you need—remove any unused or duplicate tracks that might be adding bloat.

Remember: Bitrate is a balance between quality and size. Test with different values (800kbps, 1000kbps, 1200kbps) to find the sweet spot where your video still looks good but stays small.

内容的提问来源于stack exchange,提问作者thelearner

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:17:58