From Multiple Videos
This example demonstrates how to load multiple video files into VideoEditor SDK as a single video.
Load and combine video files#
Obtain the URLs for the videos you want to load from either the local file system or the app bundle. In our example we are loading videos from the app bundle.
Then create VideoSegment
objects from these URLs and pass them to a Video
constructor as segments
parameter in order to instantiate a video object.
Trim video segments#
The duration of individual VideoSegment
s can be trimmed when creating the input Video
which will then initialize the editor with a video composition with corresponding pretrimmed video clips. The user can still change this start and end time in the clip trim tool.
Initialize the editor#
This combined Video
object we now pass as videoAsset
to the VideoEditViewController
to instantiate the video editor.
Setting the current class as the delegate of the VideoEditViewController
allows us to implement export, cancellation and error handling there.
import AVFoundationimport UIKitimport VideoEditorSDKclass OpenVideoFromMultipleVideoClipsSwift: Example, VideoEditViewControllerDelegate {override func invokeExample() {// Get the URLs for video files on the local file system. This could be any file within the app bundle// or also a file within the documents or temporary folders for example. This example will use// files from the app bundle.let firstURL = Bundle.main.url(forResource: "Skater", withExtension: "mp4")!let secondURL = Bundle.main.url(forResource: "test_video", withExtension: "mp4")!// Create `VideoSegment`s from above `URL`s.let firstAsset = VideoSegment(url: firstURL)let secondAsset = VideoSegment(url: secondURL, startTime: 1.5, endTime: 5.0)// Create a `Video` from above assets.let video = Video(segments: [firstAsset, secondAsset])// Create a video editor and pass it the video. Make this class the delegate of it to handle export and cancelation.let videoEditViewController = VideoEditViewController(videoAsset: video)videoEditViewController.delegate = self// Present the video editor.videoEditViewController.modalPresentationStyle = .fullScreenpresentingViewController?.present(videoEditViewController, animated: true, completion: nil)}// MARK: - VideoEditViewControllerDelegatefunc videoEditViewControllerShouldStart(_ videoEditViewController: VideoEditViewController, task: VideoEditorTask) -> Bool {// Implementing this method is optional. You can perform additional validation and interrupt the process by returning `false`.true}func videoEditViewControllerDidFinish(_ videoEditViewController: VideoEditViewController, result: VideoEditorResult) {// The user exported a new video successfully and the newly generated video is located at `result.output.url`. Dismissing the editor.presentingViewController?.dismiss(animated: true, completion: nil)}func videoEditViewControllerDidFail(_ videoEditViewController: VideoEditViewController, error: VideoEditorError) {// There was an error generating the video.print(error.localizedDescription)// Dismissing the editor.presentingViewController?.dismiss(animated: true, completion: nil)}func videoEditViewControllerDidCancel(_ videoEditViewController: VideoEditViewController) {// The user tapped on the cancel button within the editor. Dismissing the editor.presentingViewController?.dismiss(animated: true, completion: nil)}}