High / prohibitive memory usage for AVAssetExportSession

I am making an app that can two two videos, and then stitch them together on the screen (one video on top half and the other on bottom half).

This is achieved with AVMutableComposition, and then I am using AVAssetExportSession to export a mp4 file out:

 guard let export = AVAssetExportSession(asset: composition, presetName: AVAssetExportPreset1920x1080) else {
    return
  }

export.exportAsynchronously {
....

When the two input videos are around 1GB each, starting the export session immediately increases memory usage by ~2GB, as if it moves the input files into memory immediately (my guess), and at some point my app is killed for using too much memory.

Is there a way to avoid this upfront memory usage and/or avoid getting killed?

Hello @hamzaaftab, thank you for your post. If you need more control over how an AVAsset is exported, I would recommend using AVAssetWriter. On a separate note, I noticed your code uses exportAsynchronously(completionHandler:), which has been marked as deprecated. If you use export(to:as:isolation:) instead, how does it affect your app's memory usage?

High / prohibitive memory usage for AVAssetExportSession
 
 
Q