Search code examples
swiftavfoundationavassetavassetexportsession

AVAssetExportSession AudioMix ignored if I'm using AVAssetExportPresetPassthrough


I'm trying to only modify the video audio via AVAssetExportSession AudioMix property.

I'm not trimming or doing any other thing except for sound modification. Therefore, I tried using AVAssetExportPresetPassthrough and apply the relevant AudioMix, the AVAssetExportSession works, but the sound remains the same. If I'm changing the preset to AVAssetExportPresetHighestQuality it's much slower but then the audio change works.

Code:

let exportSession = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetPassthrough)
exportSession.audioMix = audioMix // ignored

let exportSession = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetHighestQuality)
exportSession.audioMix = audioMix // works

Any idea why? Thanks in advance!


Solution

  • By modifying the audioMix, you are not passing through. And there's no single-pass way to say "pass through the video track and decode/apply audioMix then re-encode in as AVAssetExportPresetHighestQuality", so you could try a two step method:

    Export the audio track by itself [create an AVMutableComposition containing only the that track], using AVAssetExportPresetHighestQuality:

    let exportSession = AVAssetExportSession(asset: audioOnlyMutableComposition, presetName: AVAssetExportPresetHighestQuality)
    exportSession.audioMix = audioMix
    

    Then create a new AVMutableComposition containing the original video track and your newly created mixed audio track & pass the composition through an AVAssetExportPresetPassthrough AVAssetExportSession.