Skip to main content
Language:

Configure Trim

VideoEditor SDK supports configuring the TrimToolController to enforce a minimum and maximum duration of the trimmed video. For a detailed explanation of how to configure different editor views, refer to this guide.

Min / Max Duration#

By default, no minimum or maximum video durations are enfored. In cases where you need to restrict the user from shortening the video below a specific minimum duration or importing a video that is too long, you can use the maximumDuration and minimumDuration options. The unit is seconds, so in this example we only allow videos that are at least two seconds and at most five seconds long.

Behavior and user alerts for exceptions#

If enabled, the trim length will initially be set to the maximumDuration. If this value is not set, or the video length is shorter than the maximumDuration, it will be set to the full length of the video. The user interface will not allow a trim length that is shorter than minimumDuration or longer than maximumDuration, so users will never be able to generate a video that lies outside these defined limits. However, there are two cases where this behavior can only be achieved with an alert:

  1. If a minimumDuration has been set for the trim tool and the length of the input video is shorter than that, we will present an alert as soon as the editor is opened stating that the video is too short. After confirming that alert, VideoEditViewControllerDelegate.videoEditViewControllerDidFailToGenerateVideo(_:) will be called which should dismiss the editor. The wording of that alert can be changed using the localization system or the behavior can be completely customized with the TrimToolControllerOptions.videoTooShortConfirmationClosure.
  2. If a minimumDuration has been set for the composition tool and the length of the video composition does not yet overstep that length, tapping on the export button will present an alert that notifies the user that they will have to add additional videos to the composition to export the video. After confirming the alert, the composition tool will automatically open. The wording of that alert can be changed using the localization system or the behavior can be completely customized with the CompositionToolControllerOptions.compositionTooShortConfirmationClosure. If you do not allow your users to add additional videos to the composition when CompositionToolControllerOptions.videoClipLibraryMode is set to none, the behavior will be identical as if the trim tool would have been used above.
File:
import UIKit
import VideoEditorSDK
class TrimConfigurationSwift: Example, VideoEditViewControllerDelegate {
override func invokeExample() {
// Create a `Video` from a URL to a video in the app bundle.
let video = Video(url: Bundle.main.url(forResource: "Skater", withExtension: "mp4")!)
// Create a `Configuration` object.
let configuration = Configuration { builder in
// Configure the `TrimToolController` that lets the user
// trim the video. The duration limits of these configuration options are
// also respected by the `CompositionToolController`.
builder.configureTrimToolController { options in
// By default the editor does not have a maximum duration.
// For this example the duration is set, e.g. for a social
// media application where the posts are not allowed to be
// longer than 5 seconds.
options.maximumDuration = 5.0
// By default the editor does not limit the maximum video duration.
// For this example the duration is set, e.g. for a social
// media application where the posts are not allowed to be
// shorter than 2 seconds.
options.minimumDuration = 2.0
}
}
// Create and present the video editor. Make this class the delegate of it to handle export and cancelation.
let videoEditViewController = VideoEditViewController(videoAsset: video, configuration: configuration)
videoEditViewController.delegate = self
videoEditViewController.modalPresentationStyle = .fullScreen
presentingViewController?.present(videoEditViewController, animated: true, completion: nil)
}
// MARK: - VideoEditViewControllerDelegate
func 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)
}
}