-
Notifications
You must be signed in to change notification settings - Fork 16
/
VideoEditorModule.swift
263 lines (218 loc) · 9.42 KB
/
VideoEditorModule.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import Foundation
import BanubaVideoEditorSDK
import BanubaAudioBrowserSDK
import VideoEditor
import VEExportSDK
import Flutter
protocol VideoEditor {
func initVideoEditor(token: String?, flutterResult: @escaping FlutterResult)
func openVideoEditorDefault(fromViewController controller: FlutterViewController, flutterResult: @escaping FlutterResult)
func openVideoEditorPIP(fromViewController controller: FlutterViewController, videoURL: URL, flutterResult: @escaping FlutterResult)
func openVideoEditorTrimmer(fromViewController controller: FlutterViewController, videoURL: URL, flutterResult: @escaping FlutterResult)
}
class VideoEditorModule: VideoEditor {
private var videoEditorSDK: BanubaVideoEditor?
private var flutterResult: FlutterResult?
// Use “true” if you want users could restore the last video editing session.
private let restoreLastVideoEditingSession: Bool = false
func initVideoEditor(
token: String?,
flutterResult: @escaping FlutterResult
) {
guard videoEditorSDK == nil else {
flutterResult(nil)
return
}
var config = VideoEditorConfig()
config.featureConfiguration.supportsTrimRecordedVideo = true
// Make customization here
videoEditorSDK = BanubaVideoEditor(
token: token ?? "",
configuration: config,
externalViewControllerFactory: self.getAppDelegate().provideCustomViewFactory()
)
if videoEditorSDK == nil {
flutterResult(FlutterError(code: AppDelegate.errEditorNotInitialized, message: "", details: nil))
return
}
videoEditorSDK?.delegate = self
flutterResult(nil)
}
func openVideoEditorDefault(
fromViewController controller: FlutterViewController,
flutterResult: @escaping FlutterResult
) {
self.flutterResult = flutterResult
let config = VideoEditorLaunchConfig(
entryPoint: .camera,
hostController: controller,
animated: true
)
checkLicenseAndStartVideoEditor(with: config, flutterResult: flutterResult)
}
func openVideoEditorPIP(
fromViewController controller: FlutterViewController,
videoURL: URL,
flutterResult: @escaping FlutterResult
) {
self.flutterResult = flutterResult
let pipLaunchConfig = VideoEditorLaunchConfig(
entryPoint: .pip,
hostController: controller,
pipVideoItem: videoURL,
musicTrack: nil,
animated: true
)
checkLicenseAndStartVideoEditor(with: pipLaunchConfig, flutterResult: flutterResult)
}
func openVideoEditorTrimmer(
fromViewController controller: FlutterViewController,
videoURL: URL,
flutterResult: @escaping FlutterResult
) {
self.flutterResult = flutterResult
let trimmerLaunchConfig = VideoEditorLaunchConfig(
entryPoint: .trimmer,
hostController: controller,
videoItems: [videoURL],
musicTrack: nil,
animated: true
)
checkLicenseAndStartVideoEditor(with: trimmerLaunchConfig, flutterResult: flutterResult)
}
func checkLicenseAndStartVideoEditor(with config: VideoEditorLaunchConfig, flutterResult: @escaping FlutterResult) {
if videoEditorSDK == nil {
flutterResult(FlutterError(code: AppDelegate.errEditorNotInitialized, message: "", details: nil))
return
}
// Checking the license might take around 1 sec in the worst case.
// Please optimize use if this method in your application for the best user experience
videoEditorSDK?.getLicenseState(completion: { [weak self] isValid in
guard let self else { return }
if isValid {
print("✅ The license is active")
DispatchQueue.main.async {
self.videoEditorSDK?.presentVideoEditor(
withLaunchConfiguration: config,
completion: nil
)
}
} else {
if self.restoreLastVideoEditingSession == false {
self.videoEditorSDK?.clearSessionData()
}
self.videoEditorSDK = nil
print("❌ Use of SDK is restricted: the license is revoked or expired")
flutterResult(FlutterError(code: "ERR_SDK_LICENSE_REVOKED", message: "", details: nil))
}
})
}
private func getAppDelegate() -> AppDelegate {
return UIApplication.shared.delegate as! AppDelegate
}
}
// MARK: - Export flow
extension VideoEditorModule {
func exportVideo() {
let progressView = createProgressViewController()
progressView.cancelHandler = { [weak self] in
self?.videoEditorSDK?.stopExport()
}
getTopViewController()?.present(progressView, animated: true)
let manager = FileManager.default
// File name
let firstFileURL = manager.temporaryDirectory.appendingPathComponent("banuba_demo_ve.mov")
if manager.fileExists(atPath: firstFileURL.path) {
try? manager.removeItem(at: firstFileURL)
}
// Video configuration
let exportVideoConfigurations: [ExportVideoConfiguration] = [
ExportVideoConfiguration(
fileURL: firstFileURL,
quality: .auto,
useHEVCCodecIfPossible: true,
watermarkConfiguration: nil
)
]
// Set up export
let exportConfiguration = ExportConfiguration(
videoConfigurations: exportVideoConfigurations,
isCoverEnabled: true,
gifSettings: nil
)
videoEditorSDK?.export(
using: exportConfiguration,
exportProgress: { [weak progressView] progress in progressView?.updateProgressView(with: Float(progress)) }
) { [weak self] (error, coverImage) in
// Export Callback
DispatchQueue.main.async {
progressView.dismiss(animated: true) {
// if export cancelled just hide progress view
if let error, error as NSError == exportCancelledError {
return
}
self?.completeExport(videoUrl: firstFileURL, error: error, coverImage: coverImage?.coverImage)
}
}
}
}
private func completeExport(videoUrl: URL, error: Error?, coverImage: UIImage?) {
videoEditorSDK?.dismissVideoEditor(animated: true) {
let success = error == nil
if success {
let exportedVideoFilePath = videoUrl.path
print("Video exported successfully = \(exportedVideoFilePath))")
let coverImageData = coverImage?.pngData()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH-mm-ss.SSS"
let coverImageURL = FileManager.default.temporaryDirectory.appendingPathComponent("export_preview-\(dateFormatter.string(from: Date())).png")
try? coverImageData?.write(to: coverImageURL)
let data = [
AppDelegate.argExportedVideoFile: exportedVideoFilePath,
AppDelegate.argExportedVideoCoverPreviewPath: coverImageURL.path
]
self.flutterResult?(data)
} else {
print("Error while exporting video = \(String(describing: error))")
self.flutterResult?(FlutterError(code: "ERR_MISSING_EXPORT_RESULT", message: "", details: nil))
}
// Remove strong reference to video editor sdk instance
if self.restoreLastVideoEditingSession == false {
self.videoEditorSDK?.clearSessionData()
}
self.videoEditorSDK = nil
}
}
func getTopViewController() -> UIViewController? {
let keyWindow = UIApplication
.shared
.connectedScenes
.flatMap { ($0 as? UIWindowScene)?.windows ?? [] }
.last { $0.isKeyWindow }
var topController = keyWindow?.rootViewController
while let newTopController = topController?.presentedViewController {
topController = newTopController
}
return topController
}
func createProgressViewController() -> ProgressViewController {
let progressViewController = ProgressViewController.makeViewController()
progressViewController.message = "Exporting"
return progressViewController
}
}
// MARK: - BanubaVideoEditorSDKDelegate
extension VideoEditorModule: BanubaVideoEditorDelegate {
func videoEditorDidCancel(_ videoEditor: BanubaVideoEditor) {
videoEditor.dismissVideoEditor(animated: true) {
// remove strong reference to video editor sdk instance
if self.restoreLastVideoEditingSession == false {
self.videoEditorSDK?.clearSessionData()
}
self.videoEditorSDK = nil
}
}
func videoEditorDone(_ videoEditor: BanubaVideoEditor) {
exportVideo()
}
}