Search code examples
swiftaudiomergeoverlayavmutablecomposition

Merging audio tracks in a single track using AVMutableComposition


I'm dealing with an app that should be able to mix multiple video and audio clips in a single movie file (.mp4).

At the momento the resulting movie has one video track, that is the concatenation of all imported video clips, and two audio tracks, the one originating from the imported video clips and the other originating from the imported audio clips.

What I'm trying to do is to merge those two audio tracks because I would like to have only one audio track in the resulting movie file. While we can use the instruction layer technique and merging multiple video tracks together I was not able to find something similar for audio.

I read about audio mixing object but I think it only mixes audio from both tracks. Instead I would like to have only One.

Obviously I tried to add video's audio and simple audios to the same track, but the resulting video remains black, that means to me that something has gone wrong with the asset building process. Naturally inserting different audios at the same time range is not a good think :-)

Any suggestions?


Solution

  • OK, finally I should have found the solution. Really using the AVMutableAudioMix resulting movie file has only one audio track instead of two.

    EDIT Answering to Justin comment, here is the trick:

            let audioMix = AVMutableAudioMix()
            let vip = AVMutableAudioMixInputParameters(track: self.videoAudioTrack!)
            vip.trackID = self.videoAudioTrack!.trackID
            vip.setVolume(self.videoAudioMixerVolume, at: .zero)
            let aip = AVMutableAudioMixInputParameters(track: self.audioTrack!)
            aip.trackID = self.audioTrack!.trackID
            aip.setVolume(self.audioMixerVolume, at: .zero)
            audioMix.inputParameters = [vip, aip]
            easset.audioMix = audioMix
    

    Where videoAudioTrack is the audio track for the video clip, wherease audioTrack is another simple audio track. easset is the AVAssetExporterSession object.