⬆️ ⬇️

Combine multiple videos in iOS using AVMutableVideoComposition

image Hello, Habr!

In one of the projects, I needed to solve the problem of video merging, in particular, the user could pause the video, and then continue recording (the number of iterations was unknown). Therefore, it was necessary to find a way to solve this problem with available means. Of course, it came to my mind two options, either to write everything at once into one file, or write to different ones, and glue them after the session. I decided to stop on the second, and what came of it, read under the cut.



For video recording, I used AVCamCaptureManager , based on the AVCam application, kindly located on all of our favorite sites. Well, after pressing the stop button began the most interesting.



Stage 1. Preparation.

At this stage, do the following:











Stage 2. Search for files, create assets, generate instructions and transform video tracks if necessary.











')

Stage 3. Setting the MainInstruction parameters and forming AVMutableVideoComposition











Stage 4. Saving the received video through AVAssetExportSession

The difference between the Highest and Medium presets is very significant. Therefore, if you plan to distribute video on social networks, I strongly recommend that you use Medium.





Possible statuses for export:

What we have here?
  switch (exporter.status) { case AVAssetExportSessionStatusCompleted: NSLog(@"Completed exporting!"); break; case AVAssetExportSessionStatusFailed: NSLog(@"Failed:%@", exporter.error.description); break; case AVAssetExportSessionStatusCancelled: NSLog(@"Canceled:%@", exporter.error); break; case AVAssetExportSessionStatusExporting: NSLog(@"Exporting!"); break; case AVAssetExportSessionStatusWaiting: NSLog(@"Waiting"); break; default: break; } 




Conclusion


In the article, I tried to focus attention on combining the video, and I think this post will help many habrovchan developers working on iOS, faced with such a task. In the future, I plan to talk in more detail about CMTime , and why it is not just "time." By the way, there is a lot of useful information on the topic on the site . And the method itself can be found in the repository.

Source: https://habr.com/ru/post/173599/



All Articles