From Remote URL
VideoEditor SDK supports loading video files from a remote URL, this can be a resource hosted by a video hosting provider such as Vimeo or Wistia or your own servers.
Download the video file#
Although you can pass a URL directly to the editor, we strongly recommend that you manage downloading the remote resource yourself. This allows you more control over where and how the download task is being executed and avoid potential problems around UI locking.
Hence we create a download task and move the downloaded file to a temporary directory to obtain a local URL. We save this URL in the instance variable localURL
to be able to reference it outside of the current task.
In between, we are performing a bit of housekeeping ensuring that no files exists at the temporary location.
Initialize the editor#
When the download is finished, we hand off to the main queue to initialize a Video
from the localURL
and pass it as videoAsset
to the VideoEditViewController
to instantiate the video editor.
Setting the current class the delegate of the VideoEditViewController
allows us to implement export, cancellation and error handling there.
Handling user interaction#
While the download task is executing we disable user interaction. In production you might might want to indicate download progress here.
When the download finished, we can reenable user interaction and dismiss the progress indicator.
import UIKitimport VideoEditorSDKclass OpenVideoFromRemoteURLSwift: Example, VideoEditViewControllerDelegate {// Save a reference to the downloaded file to remove it when doneprivate var localURL: URL?override func invokeExample() {// Although the editor supports opening a remote URL, we highly recommend// that you manage the download of the remote resource yourself, since this// gives you more control over the whole process. After successfully downloading// the file you should pass a local URL to the downloaded video to the editor.// This example demonstrates how to achieve this.guard let remoteURL = URL(string: "https://img.ly/static/example-assets/Skater.mp4") else {fatalError("Unable to parse URL.")}let downloadTask = URLSession.shared.downloadTask(with: remoteURL) { downloadURL, _, error inif let error = error {fatalError("There was an error downloading the file: \(error)")}if let downloadURL = downloadURL {// File was downloaded successfully. Saving it in the temporary directory.let temporaryDirectoryURL = FileManager.default.temporaryDirectorylet localURL = temporaryDirectoryURL.appendingPathComponent(remoteURL.lastPathComponent)if FileManager.default.fileExists(atPath: localURL.path) {// Remove the file at the destination if it already existstry? FileManager.default.removeItem(at: localURL)}try? FileManager.default.moveItem(at: downloadURL, to: localURL)self.localURL = localURL// Dispatch to the main queue for any UI workDispatchQueue.main.async {// Create a `Video` from the local file URL.let video = Video(url: localURL)// 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// Reenable user interaction. In production you would want to dismiss a progress indicator for example.self.presentingViewController?.view.isUserInteractionEnabled = true// Present the video editor.videoEditViewController.modalPresentationStyle = .fullScreenself.presentingViewController?.present(videoEditViewController, animated: true, completion: nil)}}}// Disable user interaction while the download is active. In production you would want to show a progress indicator for example.presentingViewController?.view.isUserInteractionEnabled = false// Start the file downloaddownloadTask.resume()}// 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// and removing the downloaded file.presentingViewController?.dismiss(animated: true, completion: {if let localURL = self.localURL {try? FileManager.default.removeItem(at: localURL)}})}func videoEditViewControllerDidFail(_ videoEditViewController: VideoEditViewController, error: VideoEditorError) {// There was an error generating the video.print(error.localizedDescription)// Dismissing the editor and removing the downloaded file.presentingViewController?.dismiss(animated: true, completion: {if let localURL = self.localURL {try? FileManager.default.removeItem(at: localURL)}})}func videoEditViewControllerDidCancel(_ videoEditViewController: VideoEditViewController) {// The user tapped the cancel button. Dismissing the editor and removing the downloaded file.presentingViewController?.dismiss(animated: true, completion: {if let localURL = self.localURL {try? FileManager.default.removeItem(at: localURL)}})}}