Search code examples
swiftavfoundationavmutablecomposition

Split AVMutableComposition and export as multiple smaller compositions


I building an app that requires the following functionality:

  • Combine videos into a single AVMutableComposition & present that single video composition to the user.
  • Export video composition in multiple chunks. For example, if the entire composition is 60 seconds long export it as 3 videos of 20 seconds.

What would be the most efficient way to split an AVMutableComposition and export it as multiple smaller compositions?

I've come up with the below solutions however none of them seem to be particularly efficient:

  • Create x compositions of the desired length.
  • Create x number of copies of the entire composition and use removeTimeRange() to trim each copy into smaller compositions.
  • Export single composition and use the file to create x shorter AVMutableCompositions.

Before going ahead and implementing one of the above solutions, I want to confirm that there's no straightforward method of splitting AVMutableCompositions that I'm missing.


Solution

  • The most efficient way is to not split the composition at all. Instead use

    • the timeRange property of AVAssetExportSession, or

    • startSessionAtSourceTime and endSessionAtSourceTime when writing with AVAssetWriter