react-native-video-processing 1.6.0 → 1.7.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.travis.yml +1 -1
- package/CODE_OF_CONDUCT.md +46 -0
- package/ISSUE_TEMPLATE.md +21 -0
- package/README.md +97 -41
- package/_config.yml +1 -0
- package/android/build.gradle +26 -10
- package/android/react-native-video-processing.iml +8 -17
- package/android/src/main/java/com/shahenlibrary/Events/Events.java +2 -0
- package/android/src/main/java/com/shahenlibrary/RNVideoProcessingPackage.java +1 -2
- package/android/src/main/java/com/shahenlibrary/Trimmer/Trimmer.java +705 -112
- package/android/src/main/java/com/shahenlibrary/Trimmer/TrimmerManager.java +50 -7
- package/android/src/main/java/com/shahenlibrary/VideoPlayer/VideoPlayerView.java +80 -6
- package/android/src/main/java/com/shahenlibrary/VideoPlayer/VideoPlayerViewManager.java +35 -12
- package/android/src/main/java/com/shahenlibrary/interfaces/OnCompressVideoListener.java +8 -0
- package/android/src/main/java/com/shahenlibrary/utils/VideoEdit.java +34 -7
- package/android/src/main/jniLibs/arm64-v8a/libffmpeg.so +0 -0
- package/android/src/main/jniLibs/armeabi-v7a/libffmpeg.so +0 -0
- package/android/src/main/jniLibs/x86/libffmpeg.so +0 -0
- package/android/src/main/jniLibs/x86_64/libffmpeg.so +0 -0
- package/ios/RNVideoProcessing/RCTSwiftBridgeModule.h +29 -0
- package/ios/RNVideoProcessing/RNTrimmerView/ICGVideoTrimmer/ICGVideoTrimmerView.h +5 -0
- package/ios/RNVideoProcessing/RNTrimmerView/ICGVideoTrimmer/ICGVideoTrimmerView.m +48 -13
- package/ios/RNVideoProcessing/RNTrimmerView/RNTrimmerView.h +1 -0
- package/ios/RNVideoProcessing/RNTrimmerView/RNTrimmerView.swift +107 -36
- package/ios/RNVideoProcessing/RNTrimmerView/RNTrimmerViewBridge.m +4 -1
- package/ios/RNVideoProcessing/RNTrimmerView/RNTrimmerViewManager.swift +4 -1
- package/ios/RNVideoProcessing/RNVideoPlayer.swift +372 -291
- package/ios/RNVideoProcessing/RNVideoProcessingBridge.m +4 -1
- package/ios/RNVideoProcessing/RNVideoProcessingManager.swift +18 -4
- package/ios/RNVideoProcessing/RNVideoTrimmer/AVUtilities.swift +81 -0
- package/ios/RNVideoProcessing/RNVideoTrimmer/RNVideoTrimmer.swift +630 -207
- package/ios/RNVideoProcessing/RNVideoTrimmer/RNVideoTrimmerBridge.m +8 -3
- package/ios/RNVideoProcessing/RNVideoTrimmer/SDAVAssetExportSession.m +32 -32
- package/ios/RNVideoProcessing/VideoProcessingGPUFilters.swift +0 -1
- package/ios/RNVideoProcessing.xcodeproj/project.pbxproj +10 -3
- package/lib/ProcessingManager/ProcessingManager.android.js +81 -17
- package/lib/ProcessingManager/ProcessingManager.ios.js +72 -10
- package/lib/ProcessingManager/index.js +1 -1
- package/lib/ProcessingManager/test/ProcessingManager.test.js +24 -0
- package/lib/ProcessingManager/types.android.js +26 -0
- package/lib/ProcessingManager/types.ios.js +21 -3
- package/lib/Trimmer/Trimmer.android.js +4 -6
- package/lib/Trimmer/Trimmer.ios.js +15 -3
- package/lib/Trimmer/test/Trimmer.test.js +6 -3
- package/lib/Trimmer/test/__snapshots__/Trimmer.test.js.snap +4 -1
- package/lib/VideoPlayer/VideoPlayer.android.js +76 -8
- package/lib/VideoPlayer/VideoPlayer.ios.js +193 -171
- package/lib/VideoPlayer/test/__snapshots__/VideoPlayer.test.js.snap +4 -1
- package/lib/utils/index.js +1 -1
- package/lib/utils/test/trimmer.test.js +55 -0
- package/lib/utils/test/utils.test.js +1 -1
- package/lib/utils/trimmer.js +12 -0
- package/package.json +37 -26
- package/.npmignore +0 -60
- package/init/index.js +0 -7
- package/ios/GPUImage/.npmignore +0 -25
- package/yarn.lock +0 -6183
|
@@ -5,49 +5,256 @@
|
|
|
5
5
|
|
|
6
6
|
import Foundation
|
|
7
7
|
import AVFoundation
|
|
8
|
+
import UIKit
|
|
8
9
|
|
|
9
10
|
enum QUALITY_ENUM: String {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
11
|
+
case QUALITY_LOW = "low"
|
|
12
|
+
case QUALITY_MEDIUM = "medium"
|
|
13
|
+
case QUALITY_HIGHEST = "highest"
|
|
14
|
+
case QUALITY_640x480 = "640x480"
|
|
15
|
+
case QUALITY_960x540 = "960x540"
|
|
16
|
+
case QUALITY_1280x720 = "1280x720"
|
|
17
|
+
case QUALITY_1920x1080 = "1920x1080"
|
|
18
|
+
case QUALITY_3840x2160 = "3840x2160"
|
|
19
|
+
case QUALITY_PASS_THROUGH = "passthrough"
|
|
19
20
|
}
|
|
20
21
|
|
|
21
22
|
@objc(RNVideoTrimmer)
|
|
22
23
|
class RNVideoTrimmer: NSObject {
|
|
24
|
+
@objc class func requiresMainQueueSetup() -> Bool {
|
|
25
|
+
return true
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
@objc func getVideoOrientationFromAsset(asset : AVAsset) -> UIImage.Orientation {
|
|
29
|
+
let videoTrack: AVAssetTrack? = asset.tracks(withMediaType: .video)[0]
|
|
30
|
+
let size = videoTrack!.naturalSize
|
|
31
|
+
|
|
32
|
+
let txf: CGAffineTransform = videoTrack!.preferredTransform
|
|
33
|
+
|
|
34
|
+
if (size.width == txf.tx && size.height == txf.ty) {
|
|
35
|
+
return .left;
|
|
36
|
+
} else if (txf.tx == 0 && txf.ty == 0) {
|
|
37
|
+
return .right;
|
|
38
|
+
} else if (txf.tx == 0 && txf.ty == size.width) {
|
|
39
|
+
return .down;
|
|
40
|
+
} else {
|
|
41
|
+
return .up;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
23
44
|
|
|
24
|
-
|
|
45
|
+
@objc func crop(_ source: String, options: NSDictionary, callback: @escaping RCTResponseSenderBlock) {
|
|
25
46
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
let saveWithCurrentDate = options.object(forKey: "saveWithCurrentDate") as? Bool ?? false
|
|
47
|
+
let cropOffsetXInt = options.object(forKey: "cropOffsetX") as! Int
|
|
48
|
+
let cropOffsetYInt = options.object(forKey: "cropOffsetY") as! Int
|
|
49
|
+
let cropWidthInt = options.object(forKey: "cropWidth") as? Int
|
|
50
|
+
let cropHeightInt = options.object(forKey: "cropHeight") as? Int
|
|
31
51
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
52
|
+
if ( cropWidthInt == nil ) {
|
|
53
|
+
callback(["Invalid cropWidth", NSNull()])
|
|
54
|
+
return
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if ( cropHeightInt == nil ) {
|
|
58
|
+
callback(["Invalid cropHeight", NSNull()])
|
|
59
|
+
return
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
let cropOffsetX : CGFloat = CGFloat(cropOffsetXInt);
|
|
63
|
+
let cropOffsetY : CGFloat = CGFloat(cropOffsetYInt);
|
|
64
|
+
var cropWidth : CGFloat = CGFloat(cropWidthInt!);
|
|
65
|
+
var cropHeight : CGFloat = CGFloat(cropHeightInt!);
|
|
66
|
+
|
|
67
|
+
let quality = ((options.object(forKey: "quality") as? String) != nil) ? options.object(forKey: "quality") as! String : ""
|
|
68
|
+
|
|
69
|
+
let sourceURL = getSourceURL(source: source)
|
|
70
|
+
let asset = AVAsset(url: sourceURL as URL)
|
|
71
|
+
|
|
72
|
+
let fileName = ProcessInfo.processInfo.globallyUniqueString
|
|
73
|
+
let outputURL = "\(NSTemporaryDirectory())\(fileName).mp4"
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
let useQuality = getQualityForAsset(quality: quality, asset: asset)
|
|
77
|
+
|
|
78
|
+
print("RNVideoTrimmer passed quality: \(quality). useQuality: \(useQuality)")
|
|
79
|
+
|
|
80
|
+
guard
|
|
81
|
+
let exportSession = AVAssetExportSession(asset: asset, presetName: useQuality)
|
|
82
|
+
else {
|
|
83
|
+
callback(["Error creating AVAssetExportSession", NSNull()])
|
|
84
|
+
return
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
exportSession.outputURL = NSURL.fileURL(withPath: outputURL)
|
|
88
|
+
exportSession.outputFileType = .mp4
|
|
89
|
+
exportSession.shouldOptimizeForNetworkUse = true
|
|
90
|
+
|
|
91
|
+
let videoComposition = AVMutableVideoComposition(propertiesOf: asset)
|
|
92
|
+
let clipVideoTrack: AVAssetTrack! = asset.tracks(withMediaType: .video)[0]
|
|
93
|
+
let videoOrientation = self.getVideoOrientationFromAsset(asset: asset)
|
|
94
|
+
|
|
95
|
+
let videoWidth : CGFloat
|
|
96
|
+
let videoHeight : CGFloat
|
|
97
|
+
|
|
98
|
+
if ( videoOrientation == .up || videoOrientation == .down ) {
|
|
99
|
+
videoWidth = clipVideoTrack.naturalSize.height
|
|
100
|
+
videoHeight = clipVideoTrack.naturalSize.width
|
|
101
|
+
} else {
|
|
102
|
+
videoWidth = clipVideoTrack.naturalSize.width
|
|
103
|
+
videoHeight = clipVideoTrack.naturalSize.height
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
videoComposition.frameDuration = CMTimeMake(value: 1, timescale: 30)
|
|
107
|
+
|
|
108
|
+
while( cropWidth.truncatingRemainder(dividingBy: 2) > 0 && cropWidth < videoWidth ) {
|
|
109
|
+
cropWidth += 1.0
|
|
110
|
+
}
|
|
111
|
+
while( cropWidth.truncatingRemainder(dividingBy: 2) > 0 && cropWidth > 0.0 ) {
|
|
112
|
+
cropWidth -= 1.0
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
while( cropHeight.truncatingRemainder(dividingBy: 2) > 0 && cropHeight < videoHeight ) {
|
|
116
|
+
cropHeight += 1.0
|
|
117
|
+
}
|
|
118
|
+
while( cropHeight.truncatingRemainder(dividingBy: 2) > 0 && cropHeight > 0.0 ) {
|
|
119
|
+
cropHeight -= 1.0
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
videoComposition.renderSize = CGSize(width: cropWidth, height: cropHeight)
|
|
123
|
+
|
|
124
|
+
let instruction : AVMutableVideoCompositionInstruction = AVMutableVideoCompositionInstruction()
|
|
125
|
+
instruction.timeRange = CMTimeRange(start: .zero, end: asset.duration)
|
|
126
|
+
|
|
127
|
+
var t1 = CGAffineTransform.identity
|
|
128
|
+
var t2 = CGAffineTransform.identity
|
|
129
|
+
|
|
130
|
+
let transformer = AVMutableVideoCompositionLayerInstruction(assetTrack: clipVideoTrack)
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
switch videoOrientation {
|
|
134
|
+
case .up:
|
|
135
|
+
t1 = CGAffineTransform(translationX: clipVideoTrack.naturalSize.height - cropOffsetX, y: 0 - cropOffsetY );
|
|
136
|
+
t2 = t1.rotated(by: CGFloat.pi / 2 );
|
|
137
|
+
break;
|
|
138
|
+
case .left:
|
|
139
|
+
t1 = CGAffineTransform(translationX: clipVideoTrack.naturalSize.width - cropOffsetX, y: clipVideoTrack.naturalSize.height - cropOffsetY );
|
|
140
|
+
t2 = t1.rotated(by: CGFloat.pi );
|
|
141
|
+
break;
|
|
142
|
+
case .right:
|
|
143
|
+
t1 = CGAffineTransform(translationX: 0 - cropOffsetX, y: 0 - cropOffsetY );
|
|
144
|
+
t2 = t1.rotated(by: 0);
|
|
145
|
+
break;
|
|
146
|
+
case .down:
|
|
147
|
+
t1 = CGAffineTransform(translationX: 0 - cropOffsetX, y: clipVideoTrack.naturalSize.width - cropOffsetY ); // not fixed width is the real height in upside down
|
|
148
|
+
t2 = t1.rotated(by: -CGFloat.pi / 2 );
|
|
149
|
+
break;
|
|
150
|
+
default:
|
|
151
|
+
NSLog("no supported orientation has been found in this video");
|
|
152
|
+
break;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
let finalTransform: CGAffineTransform = t2
|
|
156
|
+
transformer.setTransform(finalTransform, at: .zero)
|
|
157
|
+
|
|
158
|
+
instruction.layerInstructions = [transformer]
|
|
159
|
+
videoComposition.instructions = [instruction]
|
|
160
|
+
|
|
161
|
+
exportSession.videoComposition = videoComposition
|
|
162
|
+
|
|
163
|
+
exportSession.exportAsynchronously {
|
|
164
|
+
switch exportSession.status {
|
|
165
|
+
case .completed:
|
|
166
|
+
callback( [NSNull(), outputURL] )
|
|
167
|
+
|
|
168
|
+
case .failed:
|
|
169
|
+
callback( ["Failed: \(exportSession.error)", NSNull()] )
|
|
170
|
+
|
|
171
|
+
case .cancelled:
|
|
172
|
+
callback( ["Cancelled: \(exportSession.error)", NSNull()] )
|
|
173
|
+
|
|
174
|
+
default: break
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
@objc func trim(_ source: String, options: NSDictionary, callback: @escaping RCTResponseSenderBlock) {
|
|
180
|
+
|
|
181
|
+
var sTime:Float?
|
|
182
|
+
var eTime:Float?
|
|
183
|
+
if let num = options.object(forKey: "startTime") as? NSNumber {
|
|
184
|
+
sTime = num.floatValue
|
|
185
|
+
}
|
|
186
|
+
if let num = options.object(forKey: "endTime") as? NSNumber {
|
|
187
|
+
eTime = num.floatValue
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
let quality = ((options.object(forKey: "quality") as? String) != nil) ? options.object(forKey: "quality") as! String : ""
|
|
191
|
+
let saveToCameraRoll = options.object(forKey: "saveToCameraRoll") as? Bool ?? false
|
|
192
|
+
let saveWithCurrentDate = options.object(forKey: "saveWithCurrentDate") as? Bool ?? false
|
|
193
|
+
|
|
194
|
+
let manager = FileManager.default
|
|
195
|
+
guard let documentDirectory = try? manager.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
|
|
196
|
+
else {
|
|
197
|
+
callback(["Error creating FileManager", NSNull()])
|
|
198
|
+
return
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
let sourceURL = getSourceURL(source: source)
|
|
202
|
+
let asset = AVAsset(url: sourceURL as URL)
|
|
203
|
+
|
|
204
|
+
asset.loadValuesAsynchronously(forKeys: [ "exportable", "tracks" ]) {
|
|
205
|
+
precondition(asset.statusOfValue(forKey: "exportable", error: nil) == .loaded)
|
|
206
|
+
precondition(asset.statusOfValue(forKey: "tracks", error: nil) == .loaded)
|
|
207
|
+
precondition(asset.isExportable)
|
|
38
208
|
|
|
39
|
-
let sourceURL = getSourceURL(source: source)
|
|
40
|
-
let asset = AVAsset(url: sourceURL as URL)
|
|
41
209
|
if eTime == nil {
|
|
42
210
|
eTime = Float(asset.duration.seconds)
|
|
43
211
|
}
|
|
44
212
|
if sTime == nil {
|
|
45
213
|
sTime = 0
|
|
46
214
|
}
|
|
215
|
+
|
|
216
|
+
let startTime = CMTime(seconds: Double(sTime!), preferredTimescale: 1000)
|
|
217
|
+
let endTime = CMTime(seconds: Double(eTime!), preferredTimescale: 1000)
|
|
218
|
+
let timeRange = CMTimeRange(start: startTime, end: endTime)
|
|
219
|
+
|
|
220
|
+
let composition = AVMutableComposition()
|
|
221
|
+
let track = composition.addMutableTrack(withMediaType: .video, preferredTrackID: Int32(kCMPersistentTrackID_Invalid))
|
|
222
|
+
let videoOrientation = self.getVideoOrientationFromAsset(asset: asset)
|
|
223
|
+
|
|
224
|
+
if ( videoOrientation == .up ) {
|
|
225
|
+
var transforms: CGAffineTransform?
|
|
226
|
+
transforms = track?.preferredTransform
|
|
227
|
+
transforms = CGAffineTransform(rotationAngle: 0)
|
|
228
|
+
transforms = transforms?.concatenating(CGAffineTransform(rotationAngle: CGFloat(90.0 * .pi / 180)))
|
|
229
|
+
track?.preferredTransform = transforms!
|
|
230
|
+
}
|
|
231
|
+
else if ( videoOrientation == .down ) {
|
|
232
|
+
var transforms: CGAffineTransform?
|
|
233
|
+
transforms = track?.preferredTransform
|
|
234
|
+
transforms = CGAffineTransform(rotationAngle: 0)
|
|
235
|
+
transforms = transforms?.concatenating(CGAffineTransform(rotationAngle: CGFloat(270.0 * .pi / 180)))
|
|
236
|
+
track?.preferredTransform = transforms!
|
|
237
|
+
}
|
|
238
|
+
else if ( videoOrientation == .left ) {
|
|
239
|
+
var transforms: CGAffineTransform?
|
|
240
|
+
transforms = track?.preferredTransform
|
|
241
|
+
transforms = CGAffineTransform(rotationAngle: 0)
|
|
242
|
+
transforms = transforms?.concatenating(CGAffineTransform(rotationAngle: CGFloat(180.0 * .pi / 180)))
|
|
243
|
+
track?.preferredTransform = transforms!
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
do {
|
|
247
|
+
try composition.insertTimeRange(timeRange, of: asset, at: CMTime.zero)
|
|
248
|
+
} catch {
|
|
249
|
+
callback(["Error inserting time range", NSNull()])
|
|
250
|
+
// Error handling code here
|
|
251
|
+
return
|
|
252
|
+
}
|
|
253
|
+
|
|
47
254
|
var outputURL = documentDirectory.appendingPathComponent("output")
|
|
48
255
|
do {
|
|
49
256
|
try manager.createDirectory(at: outputURL, withIntermediateDirectories: true, attributes: nil)
|
|
50
|
-
let name = randomString()
|
|
257
|
+
let name = self.randomString()
|
|
51
258
|
outputURL = outputURL.appendingPathComponent("\(name).mp4")
|
|
52
259
|
} catch {
|
|
53
260
|
callback([error.localizedDescription, NSNull()])
|
|
@@ -57,30 +264,29 @@ class RNVideoTrimmer: NSObject {
|
|
|
57
264
|
//Remove existing file
|
|
58
265
|
_ = try? manager.removeItem(at: outputURL)
|
|
59
266
|
|
|
60
|
-
let
|
|
267
|
+
let finalComposition = composition.copy() as! AVComposition
|
|
268
|
+
let useQuality = self.getQualityForAsset(quality: quality, asset: asset)
|
|
61
269
|
|
|
62
270
|
print("RNVideoTrimmer passed quality: \(quality). useQuality: \(useQuality)")
|
|
63
271
|
|
|
64
|
-
guard let exportSession = AVAssetExportSession(asset:
|
|
272
|
+
guard let exportSession = AVAssetExportSession(asset: finalComposition, presetName: useQuality)
|
|
65
273
|
else {
|
|
66
274
|
callback(["Error creating AVAssetExportSession", NSNull()])
|
|
67
275
|
return
|
|
68
276
|
}
|
|
69
277
|
exportSession.outputURL = NSURL.fileURL(withPath: outputURL.path)
|
|
70
|
-
exportSession.outputFileType =
|
|
278
|
+
exportSession.outputFileType = .mp4
|
|
71
279
|
exportSession.shouldOptimizeForNetworkUse = true
|
|
72
280
|
|
|
73
281
|
if saveToCameraRoll && saveWithCurrentDate {
|
|
74
282
|
let metaItem = AVMutableMetadataItem()
|
|
75
|
-
metaItem.key =
|
|
76
|
-
metaItem.keySpace =
|
|
77
|
-
metaItem.value = NSDate()
|
|
283
|
+
metaItem.key = AVMetadataKey.commonKeyCreationDate as (NSCopying & NSObjectProtocol)
|
|
284
|
+
metaItem.keySpace = .common
|
|
285
|
+
metaItem.value = NSDate()
|
|
78
286
|
exportSession.metadata = [metaItem]
|
|
79
287
|
}
|
|
80
288
|
|
|
81
|
-
|
|
82
|
-
let endTime = CMTime(seconds: Double(eTime!), preferredTimescale: 1000)
|
|
83
|
-
let timeRange = CMTimeRange(start: startTime, end: endTime)
|
|
289
|
+
|
|
84
290
|
|
|
85
291
|
exportSession.timeRange = timeRange
|
|
86
292
|
exportSession.exportAsynchronously{
|
|
@@ -100,223 +306,440 @@ class RNVideoTrimmer: NSObject {
|
|
|
100
306
|
default: break
|
|
101
307
|
}
|
|
102
308
|
}
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
@objc func boomerang(_ source: String, options: NSDictionary, callback: @escaping RCTResponseSenderBlock) {
|
|
313
|
+
|
|
314
|
+
let quality = ""
|
|
315
|
+
|
|
316
|
+
let manager = FileManager.default
|
|
317
|
+
guard let documentDirectory = try? manager.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
|
|
318
|
+
else {
|
|
319
|
+
callback(["Error creating FileManager", NSNull()])
|
|
320
|
+
return
|
|
103
321
|
}
|
|
104
322
|
|
|
105
|
-
|
|
323
|
+
let sourceURL = getSourceURL(source: source)
|
|
324
|
+
let firstAsset = AVAsset(url: sourceURL as URL)
|
|
325
|
+
|
|
326
|
+
let mixComposition = AVMutableComposition()
|
|
327
|
+
let track = mixComposition.addMutableTrack(withMediaType: .video, preferredTrackID: Int32(kCMPersistentTrackID_Invalid))
|
|
328
|
+
|
|
329
|
+
//fix issue where boomerang effect on main camera video is rotated 90 degrees
|
|
330
|
+
var transforms: CGAffineTransform?
|
|
331
|
+
transforms = track?.preferredTransform
|
|
332
|
+
transforms = CGAffineTransform(rotationAngle: 0)
|
|
333
|
+
//detect camera type
|
|
334
|
+
if(options.object(forKey: "cameraType") as? String == "back"){
|
|
335
|
+
transforms = transforms?.concatenating(CGAffineTransform(rotationAngle: CGFloat(90.0 * .pi / 180)))
|
|
336
|
+
transforms = transforms?.concatenating(CGAffineTransform(translationX: 640, y: 0))
|
|
337
|
+
track?.preferredTransform = transforms!
|
|
338
|
+
}
|
|
339
|
+
//end of fix
|
|
340
|
+
|
|
341
|
+
var outputURL = documentDirectory.appendingPathComponent("output")
|
|
342
|
+
var finalURL = documentDirectory.appendingPathComponent("output")
|
|
343
|
+
do {
|
|
344
|
+
try manager.createDirectory(at: outputURL, withIntermediateDirectories: true, attributes: nil)
|
|
345
|
+
try manager.createDirectory(at: finalURL, withIntermediateDirectories: true, attributes: nil)
|
|
346
|
+
let name = randomString()
|
|
347
|
+
outputURL = outputURL.appendingPathComponent("\(name).mp4")
|
|
348
|
+
finalURL = finalURL.appendingPathComponent("\(name)merged.mp4")
|
|
349
|
+
} catch {
|
|
350
|
+
callback([error.localizedDescription, NSNull()])
|
|
351
|
+
print(error)
|
|
352
|
+
}
|
|
106
353
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
let saveToCameraRoll = options.object(forKey: "saveToCameraRoll") as? Bool ?? false
|
|
111
|
-
let minimumBitrate = options.object(forKey: "minimumBitrate") as? Float
|
|
112
|
-
let saveWithCurrentDate = options.object(forKey: "saveWithCurrentDate") as? Bool ?? false
|
|
354
|
+
//Remove existing file
|
|
355
|
+
_ = try? manager.removeItem(at: outputURL)
|
|
356
|
+
_ = try? manager.removeItem(at: finalURL)
|
|
113
357
|
|
|
114
|
-
|
|
115
|
-
guard let documentDirectory = try? manager.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
|
|
116
|
-
else {
|
|
117
|
-
callback(["Error creating FileManager", NSNull()])
|
|
118
|
-
return
|
|
119
|
-
}
|
|
358
|
+
let useQuality = getQualityForAsset(quality: quality, asset: firstAsset)
|
|
120
359
|
|
|
121
|
-
|
|
122
|
-
let asset = AVAsset(url: sourceURL as URL)
|
|
360
|
+
// print("RNVideoTrimmer passed quality: \(quality). useQuality: \(useQuality)")
|
|
123
361
|
|
|
124
|
-
|
|
125
|
-
callback(["Error getting track info", NSNull()])
|
|
126
|
-
return
|
|
127
|
-
}
|
|
362
|
+
AVUtilities.reverse(firstAsset, outputURL: outputURL, completion: { [unowned self] (reversedAsset: AVAsset) in
|
|
128
363
|
|
|
129
|
-
let naturalSize = videoTrack.naturalSize.applying(videoTrack.preferredTransform)
|
|
130
|
-
let bps = videoTrack.estimatedDataRate
|
|
131
|
-
width = width ?? Float(abs(naturalSize.width))
|
|
132
|
-
height = height ?? Float(abs(naturalSize.height))
|
|
133
|
-
var averageBitrate = bps / bitrateMultiplier
|
|
134
|
-
if minimumBitrate != nil {
|
|
135
|
-
if averageBitrate < minimumBitrate! {
|
|
136
|
-
averageBitrate = minimumBitrate!
|
|
137
|
-
}
|
|
138
|
-
if bps < minimumBitrate! {
|
|
139
|
-
averageBitrate = bps
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
364
|
|
|
143
|
-
|
|
144
|
-
do {
|
|
145
|
-
try manager.createDirectory(at: outputURL, withIntermediateDirectories: true, attributes: nil)
|
|
146
|
-
let name = randomString()
|
|
147
|
-
outputURL = outputURL.appendingPathComponent("\(name)-compressed.mp4")
|
|
148
|
-
} catch {
|
|
149
|
-
callback([error.localizedDescription, NSNull()])
|
|
150
|
-
print(error)
|
|
151
|
-
}
|
|
365
|
+
let secondAsset = reversedAsset
|
|
152
366
|
|
|
153
|
-
|
|
154
|
-
|
|
367
|
+
// Credit: https://www.raywenderlich.com/94404/play-record-merge-videos-ios-swift
|
|
368
|
+
do {
|
|
369
|
+
try track?.insertTimeRange(CMTimeRangeMake(start: .zero, duration: firstAsset.duration), of: firstAsset.tracks(withMediaType: .video)[0], at: .zero)
|
|
370
|
+
} catch _ {
|
|
371
|
+
callback( ["Failed: Could not load 1st track", NSNull()] )
|
|
372
|
+
return
|
|
373
|
+
}
|
|
155
374
|
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
375
|
+
do {
|
|
376
|
+
try track?.insertTimeRange(CMTimeRangeMake(start: .zero, duration: secondAsset.duration), of: secondAsset.tracks(withMediaType: .video)[0], at: mixComposition.duration)
|
|
377
|
+
} catch _ {
|
|
378
|
+
callback( ["Failed: Could not load 2nd track", NSNull()] )
|
|
379
|
+
return
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
|
|
383
|
+
guard let exportSession = AVAssetExportSession(asset: mixComposition, presetName: useQuality) else {
|
|
384
|
+
callback(["Error creating AVAssetExportSession", NSNull()])
|
|
385
|
+
return
|
|
386
|
+
}
|
|
387
|
+
exportSession.outputURL = NSURL.fileURL(withPath: finalURL.path)
|
|
388
|
+
exportSession.outputFileType = .mp4
|
|
389
|
+
exportSession.shouldOptimizeForNetworkUse = true
|
|
390
|
+
let startTime = CMTime(seconds: Double(0), preferredTimescale: 1000)
|
|
391
|
+
let endTime = CMTime(seconds: mixComposition.duration.seconds, preferredTimescale: 1000)
|
|
392
|
+
let timeRange = CMTimeRange(start: startTime, end: endTime)
|
|
393
|
+
|
|
394
|
+
exportSession.timeRange = timeRange
|
|
395
|
+
|
|
396
|
+
exportSession.exportAsynchronously{
|
|
397
|
+
switch exportSession.status {
|
|
398
|
+
case .completed:
|
|
399
|
+
callback( [NSNull(), finalURL.absoluteString] )
|
|
400
|
+
|
|
401
|
+
case .failed:
|
|
402
|
+
callback( ["Failed: \(exportSession.error)", NSNull()] )
|
|
403
|
+
|
|
404
|
+
case .cancelled:
|
|
405
|
+
callback( ["Cancelled: \(exportSession.error)", NSNull()] )
|
|
406
|
+
|
|
407
|
+
default: break
|
|
170
408
|
}
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
409
|
+
}
|
|
410
|
+
})
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
@objc func reverse(_ source: String, options: NSDictionary, callback: @escaping RCTResponseSenderBlock) {
|
|
414
|
+
|
|
415
|
+
let quality = ""
|
|
416
|
+
|
|
417
|
+
let manager = FileManager.default
|
|
418
|
+
guard let documentDirectory = try? manager.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
|
|
419
|
+
else {
|
|
420
|
+
callback(["Error creating FileManager", NSNull()])
|
|
421
|
+
return
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
let sourceURL = getSourceURL(source: source)
|
|
425
|
+
let asset = AVAsset(url: sourceURL as URL)
|
|
426
|
+
|
|
427
|
+
var outputURL = documentDirectory.appendingPathComponent("output")
|
|
428
|
+
do {
|
|
429
|
+
try manager.createDirectory(at: outputURL, withIntermediateDirectories: true, attributes: nil)
|
|
430
|
+
let name = randomString()
|
|
431
|
+
outputURL = outputURL.appendingPathComponent("\(name).mp4")
|
|
432
|
+
} catch {
|
|
433
|
+
callback([error.localizedDescription, NSNull()])
|
|
434
|
+
print(error)
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
//Remove existing file
|
|
438
|
+
_ = try? manager.removeItem(at: outputURL)
|
|
439
|
+
|
|
440
|
+
let useQuality = getQualityForAsset(quality: quality, asset: asset)
|
|
441
|
+
|
|
442
|
+
print("RNVideoTrimmer passed quality: \(quality). useQuality: \(useQuality)")
|
|
443
|
+
|
|
444
|
+
AVUtilities.reverse(asset, outputURL: outputURL, completion: { [unowned self] (asset: AVAsset) in
|
|
445
|
+
callback( [NSNull(), outputURL.absoluteString] )
|
|
446
|
+
})
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
@objc func compress(_ source: String, options: NSDictionary, callback: @escaping RCTResponseSenderBlock) {
|
|
450
|
+
|
|
451
|
+
var width = options.object(forKey: "width") as? Float
|
|
452
|
+
var height = options.object(forKey: "height") as? Float
|
|
453
|
+
let bitrateMultiplier = options.object(forKey: "bitrateMultiplier") as? Float ?? 1
|
|
454
|
+
let saveToCameraRoll = options.object(forKey: "saveToCameraRoll") as? Bool ?? false
|
|
455
|
+
let minimumBitrate = options.object(forKey: "minimumBitrate") as? Float
|
|
456
|
+
let saveWithCurrentDate = options.object(forKey: "saveWithCurrentDate") as? Bool ?? false
|
|
457
|
+
let removeAudio = options.object(forKey: "removeAudio") as? Bool ?? false
|
|
458
|
+
|
|
459
|
+
let manager = FileManager.default
|
|
460
|
+
guard let documentDirectory = try? manager.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
|
|
461
|
+
else {
|
|
462
|
+
callback(["Error creating FileManager", NSNull()])
|
|
463
|
+
return
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
let sourceURL = getSourceURL(source: source)
|
|
467
|
+
let asset = AVAsset(url: sourceURL as URL)
|
|
468
|
+
|
|
469
|
+
guard let videoTrack = asset.tracks(withMediaType: .video).first else {
|
|
470
|
+
callback(["Error getting track info", NSNull()])
|
|
471
|
+
return
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
let naturalSize = videoTrack.naturalSize.applying(videoTrack.preferredTransform)
|
|
475
|
+
let bps = videoTrack.estimatedDataRate
|
|
476
|
+
width = width ?? Float(abs(naturalSize.width))
|
|
477
|
+
height = height ?? Float(abs(naturalSize.height))
|
|
478
|
+
var averageBitrate = bps / bitrateMultiplier
|
|
479
|
+
if minimumBitrate != nil {
|
|
480
|
+
if averageBitrate < minimumBitrate! {
|
|
481
|
+
averageBitrate = minimumBitrate!
|
|
482
|
+
}
|
|
483
|
+
if bps < minimumBitrate! {
|
|
484
|
+
averageBitrate = bps
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
var outputURL = documentDirectory.appendingPathComponent("output")
|
|
489
|
+
do {
|
|
490
|
+
try manager.createDirectory(at: outputURL, withIntermediateDirectories: true, attributes: nil)
|
|
491
|
+
let name = randomString()
|
|
492
|
+
outputURL = outputURL.appendingPathComponent("\(name)-compressed.mp4")
|
|
493
|
+
} catch {
|
|
494
|
+
callback([error.localizedDescription, NSNull()])
|
|
495
|
+
print(error)
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
//Remove existing file
|
|
499
|
+
_ = try? manager.removeItem(at: outputURL)
|
|
500
|
+
|
|
501
|
+
let compressionEncoder = SDAVAssetExportSession(asset: asset)
|
|
502
|
+
if compressionEncoder == nil {
|
|
503
|
+
callback(["Error creating AVAssetExportSession", NSNull()])
|
|
504
|
+
return
|
|
505
|
+
}
|
|
506
|
+
compressionEncoder!.outputFileType = AVFileType.mp4.rawValue
|
|
507
|
+
compressionEncoder!.outputURL = NSURL.fileURL(withPath: outputURL.path)
|
|
508
|
+
compressionEncoder!.shouldOptimizeForNetworkUse = true
|
|
509
|
+
if saveToCameraRoll && saveWithCurrentDate {
|
|
510
|
+
let metaItem = AVMutableMetadataItem()
|
|
511
|
+
metaItem.key = AVMetadataKey.commonKeyCreationDate as (NSCopying & NSObjectProtocol)
|
|
512
|
+
metaItem.keySpace = .common
|
|
513
|
+
metaItem.value = NSDate()
|
|
514
|
+
compressionEncoder!.metadata = [metaItem]
|
|
515
|
+
}
|
|
516
|
+
compressionEncoder?.videoSettings = [
|
|
517
|
+
AVVideoCodecKey: AVVideoCodecH264,
|
|
518
|
+
AVVideoWidthKey: NSNumber.init(value: width!),
|
|
519
|
+
AVVideoHeightKey: NSNumber.init(value: height!),
|
|
520
|
+
AVVideoCompressionPropertiesKey: [
|
|
521
|
+
AVVideoAverageBitRateKey: NSNumber.init(value: averageBitrate),
|
|
522
|
+
AVVideoProfileLevelKey: AVVideoProfileLevelH264HighAutoLevel
|
|
523
|
+
]
|
|
524
|
+
]
|
|
525
|
+
if !removeAudio {
|
|
180
526
|
compressionEncoder?.audioSettings = [
|
|
181
527
|
AVFormatIDKey: kAudioFormatMPEG4AAC,
|
|
182
528
|
AVNumberOfChannelsKey: 1,
|
|
183
529
|
AVSampleRateKey: 44100,
|
|
184
530
|
AVEncoderBitRateKey: 128000
|
|
185
531
|
]
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
532
|
+
}
|
|
533
|
+
compressionEncoder!.exportAsynchronously(completionHandler: {
|
|
534
|
+
switch compressionEncoder!.status {
|
|
535
|
+
case .completed:
|
|
536
|
+
callback( [NSNull(), outputURL.absoluteString] )
|
|
537
|
+
if saveToCameraRoll {
|
|
538
|
+
UISaveVideoAtPathToSavedPhotosAlbum(outputURL.relativePath, self, nil, nil)
|
|
539
|
+
}
|
|
540
|
+
case .failed:
|
|
541
|
+
callback( ["Failed: \(compressionEncoder!.error)", NSNull()] )
|
|
542
|
+
|
|
543
|
+
case .cancelled:
|
|
544
|
+
callback( ["Cancelled: \(compressionEncoder!.error)", NSNull()] )
|
|
545
|
+
|
|
546
|
+
default: break
|
|
547
|
+
}
|
|
548
|
+
})
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
@objc func getAssetInfo(_ source: String, callback: RCTResponseSenderBlock) {
|
|
552
|
+
let sourceURL = getSourceURL(source: source)
|
|
553
|
+
let asset = AVAsset(url: sourceURL)
|
|
554
|
+
var assetInfo: [String: Any] = [
|
|
555
|
+
"duration" : asset.duration.seconds
|
|
556
|
+
]
|
|
557
|
+
if let track = asset.tracks(withMediaType: .video).first {
|
|
558
|
+
let naturalSize = track.naturalSize
|
|
559
|
+
let t = track.preferredTransform
|
|
560
|
+
let isPortrait = t.a == 0 && abs(t.b) == 1 && t.d == 0
|
|
561
|
+
let size = [
|
|
562
|
+
"width": isPortrait ? naturalSize.height : naturalSize.width,
|
|
563
|
+
"height": isPortrait ? naturalSize.width : naturalSize.height
|
|
564
|
+
]
|
|
565
|
+
assetInfo["size"] = size
|
|
566
|
+
assetInfo["frameRate"] = Int(round(track.nominalFrameRate))
|
|
567
|
+
assetInfo["bitrate"] = Int(round(track.estimatedDataRate))
|
|
568
|
+
}
|
|
569
|
+
callback( [NSNull(), assetInfo] )
|
|
570
|
+
}
|
|
195
571
|
|
|
196
|
-
|
|
197
|
-
|
|
572
|
+
@objc func getPreviewImageAtPosition(_ source: String, atTime: Float = 0, maximumSize: NSDictionary, format: String = "base64", callback: @escaping RCTResponseSenderBlock) {
|
|
573
|
+
let sourceURL = getSourceURL(source: source)
|
|
574
|
+
let asset = AVAsset(url: sourceURL)
|
|
198
575
|
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
576
|
+
var width: CGFloat = 1080
|
|
577
|
+
if let _width = maximumSize.object(forKey: "width") as? CGFloat {
|
|
578
|
+
width = _width
|
|
579
|
+
}
|
|
580
|
+
var height: CGFloat = 1080
|
|
581
|
+
if let _height = maximumSize.object(forKey: "height") as? CGFloat {
|
|
582
|
+
height = _height
|
|
202
583
|
}
|
|
203
584
|
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
if let track = asset.tracks(withMediaType: AVMediaTypeVideo).first {
|
|
211
|
-
let naturalSize = track.naturalSize
|
|
212
|
-
let t = track.preferredTransform
|
|
213
|
-
let isPortrait = t.a == 0 && abs(t.b) == 1 && t.d == 0
|
|
214
|
-
let size = [
|
|
215
|
-
"width": isPortrait ? naturalSize.height : naturalSize.width,
|
|
216
|
-
"height": isPortrait ? naturalSize.width : naturalSize.height
|
|
217
|
-
]
|
|
218
|
-
assetInfo["size"] = size
|
|
219
|
-
}
|
|
220
|
-
callback( [NSNull(), assetInfo] )
|
|
585
|
+
let imageGenerator = AVAssetImageGenerator(asset: asset)
|
|
586
|
+
imageGenerator.maximumSize = CGSize(width: width, height: height)
|
|
587
|
+
imageGenerator.appliesPreferredTrackTransform = true
|
|
588
|
+
var second = atTime
|
|
589
|
+
if atTime > Float(asset.duration.seconds) || atTime < 0 {
|
|
590
|
+
second = 0
|
|
221
591
|
}
|
|
592
|
+
let timestamp = CMTime(seconds: Double(second), preferredTimescale: 600)
|
|
593
|
+
do {
|
|
594
|
+
let imageRef = try imageGenerator.copyCGImage(at: timestamp, actualTime: nil)
|
|
595
|
+
let image = UIImage(cgImage: imageRef)
|
|
596
|
+
if ( format == "base64" ) {
|
|
597
|
+
let imgData = image.pngData()
|
|
598
|
+
let base64string = imgData?.base64EncodedString(options: Data.Base64EncodingOptions.init(rawValue: 0))
|
|
599
|
+
if base64string != nil {
|
|
600
|
+
callback( [NSNull(), base64string!] )
|
|
601
|
+
} else {
|
|
602
|
+
callback( ["Unable to convert to base64)", NSNull()] )
|
|
603
|
+
}
|
|
604
|
+
} else if ( format == "JPEG" ) {
|
|
605
|
+
let imgData = image.jpegData(compressionQuality: 1)
|
|
222
606
|
|
|
223
|
-
|
|
224
|
-
let
|
|
225
|
-
let asset = AVAsset(url: sourceURL)
|
|
607
|
+
let fileName = ProcessInfo.processInfo.globallyUniqueString
|
|
608
|
+
let fullPath = "\(NSTemporaryDirectory())\(fileName).jpg"
|
|
226
609
|
|
|
227
|
-
|
|
228
|
-
if let _width = maximumSize.object(forKey: "width") as? CGFloat {
|
|
229
|
-
width = _width
|
|
230
|
-
}
|
|
231
|
-
var height: CGFloat = 1080
|
|
232
|
-
if let _height = maximumSize.object(forKey: "height") as? CGFloat {
|
|
233
|
-
height = _height
|
|
234
|
-
}
|
|
610
|
+
try imgData?.write(to: URL(fileURLWithPath: fullPath), options: .atomic)
|
|
235
611
|
|
|
236
|
-
let
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
let image = UIImage(cgImage: imageRef)
|
|
247
|
-
let imgData = UIImagePNGRepresentation(image)
|
|
248
|
-
let base64string = imgData?.base64EncodedString(options: Data.Base64EncodingOptions.init(rawValue: 0))
|
|
249
|
-
if base64string != nil {
|
|
250
|
-
callback( [NSNull(), base64string!] )
|
|
251
|
-
} else {
|
|
252
|
-
callback( ["Unable to convert to base64)", NSNull()] )
|
|
253
|
-
}
|
|
254
|
-
} catch {
|
|
255
|
-
callback( ["Failed to convert base64: \(error.localizedDescription)", NSNull()] )
|
|
256
|
-
}
|
|
612
|
+
let imageWidth = imageRef.width
|
|
613
|
+
let imageHeight = imageRef.height
|
|
614
|
+
let imageFormattedData: [AnyHashable: Any] = ["uri": fullPath, "width": imageWidth, "height": imageHeight]
|
|
615
|
+
|
|
616
|
+
callback( [NSNull(), imageFormattedData] )
|
|
617
|
+
} else {
|
|
618
|
+
callback( ["Failed format. Expected one of 'base64' or 'JPEG'", NSNull()] )
|
|
619
|
+
}
|
|
620
|
+
} catch {
|
|
621
|
+
callback( ["Failed to convert base64: \(error.localizedDescription)", NSNull()] )
|
|
257
622
|
}
|
|
623
|
+
}
|
|
258
624
|
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
625
|
+
|
|
626
|
+
@objc func getTrimmerPreviewImages(_ source: String, startTime: Float = 0, endTime: Float, step: Int = 1, maximumSize: NSDictionary, format: String = "base64", callback: @escaping RCTResponseSenderBlock) {
|
|
627
|
+
let sourceURL = getSourceURL(source: source)
|
|
628
|
+
let asset = AVAsset(url: sourceURL)
|
|
629
|
+
|
|
630
|
+
var width: CGFloat = 1080
|
|
631
|
+
if let _width = maximumSize.object(forKey: "width") as? CGFloat {
|
|
632
|
+
width = _width
|
|
267
633
|
}
|
|
634
|
+
var height: CGFloat = 1080
|
|
635
|
+
if let _height = maximumSize.object(forKey: "height") as? CGFloat {
|
|
636
|
+
height = _height
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
let imageGenerator = AVAssetImageGenerator(asset: asset)
|
|
640
|
+
imageGenerator.maximumSize = CGSize(width: width, height: height)
|
|
641
|
+
imageGenerator.appliesPreferredTrackTransform = true
|
|
268
642
|
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
643
|
+
do {
|
|
644
|
+
var returnData: Array<Any> = [];
|
|
645
|
+
|
|
646
|
+
for second in stride(from: Int(startTime), to: Int(endTime), by: step) {
|
|
647
|
+
let timestamp = CMTime(seconds: Double(second), preferredTimescale: 600)
|
|
648
|
+
let imageRef = try imageGenerator.copyCGImage(at: timestamp, actualTime: nil)
|
|
649
|
+
let image = UIImage(cgImage: imageRef)
|
|
650
|
+
// if ( format == "base64" ) {
|
|
651
|
+
let imgData = image.pngData()
|
|
652
|
+
let base64string = imgData?.base64EncodedString(options: Data.Base64EncodingOptions.init(rawValue: 0))
|
|
653
|
+
if base64string != nil {
|
|
654
|
+
returnData.append(base64string)
|
|
273
655
|
} else {
|
|
274
|
-
|
|
275
|
-
sourceURL = URL(string: source, relativeTo: bundleUrl)!
|
|
656
|
+
callback( ["Unable to convert to base64)", NSNull()] )
|
|
276
657
|
}
|
|
277
|
-
|
|
658
|
+
// } else if ( format == "JPEG" ) {
|
|
659
|
+
// let imgData = image.jpegData(compressionQuality: 1)
|
|
660
|
+
|
|
661
|
+
// let fileName = ProcessInfo.processInfo.globallyUniqueString
|
|
662
|
+
// let fullPath = "\(NSTemporaryDirectory())\(fileName).jpg"
|
|
663
|
+
|
|
664
|
+
// try imgData?.write(to: URL(fileURLWithPath: fullPath), options: .atomic)
|
|
665
|
+
|
|
666
|
+
// let imageWidth = imageRef.width
|
|
667
|
+
// let imageHeight = imageRef.height
|
|
668
|
+
// let imageFormattedData: [AnyHashable: Any] = ["uri": fullPath, "width": imageWidth, "height": imageHeight]
|
|
669
|
+
|
|
670
|
+
// returnData.append(imageFormattedData)
|
|
671
|
+
// } else {
|
|
672
|
+
// callback( ["Failed format. Expected one of 'base64' or 'JPEG'", NSNull()] )
|
|
673
|
+
// }
|
|
674
|
+
}
|
|
675
|
+
callback( [NSNull(), returnData] )
|
|
676
|
+
} catch {
|
|
677
|
+
callback( ["Failed to convert base64: \(error.localizedDescription)", NSNull()] )
|
|
278
678
|
}
|
|
679
|
+
}
|
|
279
680
|
|
|
280
|
-
func getQualityForAsset(quality: String, asset: AVAsset) -> String {
|
|
281
|
-
var useQuality: String
|
|
282
681
|
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
682
|
+
func randomString() -> String {
|
|
683
|
+
let letters: NSString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
|
684
|
+
let randomString: NSMutableString = NSMutableString(capacity: 20)
|
|
685
|
+
let s:String = "RNTrimmer-Temp-Video"
|
|
686
|
+
for _ in 0...19 {
|
|
687
|
+
randomString.appendFormat("%C", letters.character(at: Int(arc4random_uniform(UInt32(letters.length)))))
|
|
688
|
+
}
|
|
689
|
+
return s.appending(randomString as String)
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
func getSourceURL(source: String) -> URL {
|
|
693
|
+
var sourceURL: URL
|
|
694
|
+
if source.contains("assets-library") {
|
|
695
|
+
sourceURL = NSURL(string: source) as! URL
|
|
696
|
+
} else {
|
|
697
|
+
let bundleUrl = Bundle.main.resourceURL!
|
|
698
|
+
sourceURL = URL(string: source, relativeTo: bundleUrl)!
|
|
699
|
+
}
|
|
700
|
+
return sourceURL
|
|
701
|
+
}
|
|
286
702
|
|
|
287
|
-
|
|
288
|
-
|
|
703
|
+
func getQualityForAsset(quality: String, asset: AVAsset) -> String {
|
|
704
|
+
var useQuality: String
|
|
289
705
|
|
|
290
|
-
|
|
291
|
-
|
|
706
|
+
switch quality {
|
|
707
|
+
case QUALITY_ENUM.QUALITY_LOW.rawValue:
|
|
708
|
+
useQuality = AVAssetExportPresetLowQuality
|
|
292
709
|
|
|
293
|
-
|
|
294
|
-
|
|
710
|
+
case QUALITY_ENUM.QUALITY_MEDIUM.rawValue:
|
|
711
|
+
useQuality = AVAssetExportPresetMediumQuality
|
|
295
712
|
|
|
296
|
-
|
|
297
|
-
|
|
713
|
+
case QUALITY_ENUM.QUALITY_HIGHEST.rawValue:
|
|
714
|
+
useQuality = AVAssetExportPresetHighestQuality
|
|
298
715
|
|
|
299
|
-
|
|
300
|
-
|
|
716
|
+
case QUALITY_ENUM.QUALITY_640x480.rawValue:
|
|
717
|
+
useQuality = AVAssetExportPreset640x480
|
|
301
718
|
|
|
302
|
-
|
|
303
|
-
|
|
719
|
+
case QUALITY_ENUM.QUALITY_960x540.rawValue:
|
|
720
|
+
useQuality = AVAssetExportPreset960x540
|
|
304
721
|
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
useQuality = AVAssetExportPreset3840x2160
|
|
308
|
-
} else {
|
|
309
|
-
useQuality = AVAssetExportPresetPassthrough
|
|
310
|
-
}
|
|
722
|
+
case QUALITY_ENUM.QUALITY_1280x720.rawValue:
|
|
723
|
+
useQuality = AVAssetExportPreset1280x720
|
|
311
724
|
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
}
|
|
725
|
+
case QUALITY_ENUM.QUALITY_1920x1080.rawValue:
|
|
726
|
+
useQuality = AVAssetExportPreset1920x1080
|
|
315
727
|
|
|
316
|
-
|
|
317
|
-
if
|
|
318
|
-
|
|
728
|
+
case QUALITY_ENUM.QUALITY_3840x2160.rawValue:
|
|
729
|
+
if #available(iOS 9.0, *) {
|
|
730
|
+
useQuality = AVAssetExportPreset3840x2160
|
|
731
|
+
} else {
|
|
732
|
+
useQuality = AVAssetExportPresetPassthrough
|
|
319
733
|
}
|
|
320
|
-
|
|
734
|
+
|
|
735
|
+
default:
|
|
736
|
+
useQuality = AVAssetExportPresetPassthrough
|
|
737
|
+
}
|
|
738
|
+
|
|
739
|
+
let compatiblePresets = AVAssetExportSession.exportPresets(compatibleWith: asset)
|
|
740
|
+
if !compatiblePresets.contains(useQuality) {
|
|
741
|
+
useQuality = AVAssetExportPresetPassthrough
|
|
321
742
|
}
|
|
743
|
+
return useQuality
|
|
744
|
+
}
|
|
322
745
|
}
|