import UIKit
import VideoEditorSDK
class VideoAddFiltersFromRemoteURLSwift: Example, VideoEditViewControllerDelegate {
private var downloadLocations = [String: URL]()
override func invokeExample() {
let video = Video(url: Bundle.main.url(forResource: "Skater", withExtension: "mp4")!)
let filterFilename = "custom_lut_invert.png"
let thumbnailFilename = "custom_filter_category.jpg"
let assetFilenames = [filterFilename, thumbnailFilename]
assetFilenames.forEach { filename in
guard let remoteURL = URL(string: "https://img.ly/static/example-assets/\(filename)") else {
fatalError("Unable to parse URL.")
}
let downloadTask = URLSession.shared.downloadTask(with: remoteURL) { downloadURL, _, error in
if let error = error {
fatalError("There was an error downloading the file: \(error)")
}
guard let downloadURL = downloadURL else {
return
}
let temporaryDirectoryURL = FileManager.default.temporaryDirectory
let localURL = temporaryDirectoryURL.appendingPathComponent(filename)
if FileManager.default.fileExists(atPath: localURL.path) {
try? FileManager.default.removeItem(at: localURL)
}
try? FileManager.default.moveItem(at: downloadURL, to: localURL)
DispatchQueue.main.async {
self.downloadLocations[filename] = localURL
if self.downloadLocations.count == assetFilenames.count {
let filterURL = self.downloadLocations[filterFilename]
let customLUTFilter = LUTEffect(identifier: "custom_lut_filter", lutURL: filterURL!, displayName: "Invert", horizontalTileCount: 5, verticalTileCount: 5)
let customDuoToneFilter = DuoToneEffect(identifier: "custom_duotone_filter", lightColor: .yellow, darkColor: .blue, displayName: "Custom")
let configuration = Configuration { builder in
let assetCatalog = AssetCatalog.defaultItems
assetCatalog.effects.append(contentsOf: [customLUTFilter, customDuoToneFilter])
builder.assetCatalog = assetCatalog
let thumbnailURL = self.downloadLocations[thumbnailFilename]
let thumbnailData = try? Data(contentsOf: thumbnailURL!)
let thumbnail = UIImage(data: thumbnailData!)
let customFilterGroup = Group(identifier: "custom_filter_category", displayName: "Custom", thumbnail: thumbnail, memberIdentifiers: ["custom_lut_filter"])
builder.configureFilterToolController { options in
options.filterGroups.append(customFilterGroup)
}
}
let videoEditViewController = VideoEditViewController(videoAsset: video, configuration: configuration)
videoEditViewController.delegate = self
self.presentingViewController?.view.isUserInteractionEnabled = true
videoEditViewController.modalPresentationStyle = .fullScreen
self.presentingViewController?.present(videoEditViewController, animated: true, completion: nil)
}
}
}
presentingViewController?.view.isUserInteractionEnabled = false
downloadTask.resume()
}
}
private func removeResources() {
for location in downloadLocations.values {
try? FileManager.default.removeItem(at: location)
}
downloadLocations.removeAll()
}
func videoEditViewControllerShouldStart(_ videoEditViewController: VideoEditViewController, task: VideoEditorTask) -> Bool {
true
}
func videoEditViewControllerDidFinish(_ videoEditViewController: VideoEditViewController, result: VideoEditorResult) {
presentingViewController?.dismiss(animated: true, completion: {
self.removeResources()
})
}
func videoEditViewControllerDidFail(_ videoEditViewController: VideoEditViewController, error: VideoEditorError) {
print(error.localizedDescription)
presentingViewController?.dismiss(animated: true, completion: {
self.removeResources()
})
}
func videoEditViewControllerDidCancel(_ videoEditViewController: VideoEditViewController) {
presentingViewController?.dismiss(animated: true, completion: {
self.removeResources()
})
}
}