-
Notifications
You must be signed in to change notification settings - Fork 4
/
RNAVEditing.m
364 lines (274 loc) · 14.6 KB
/
RNAVEditing.m
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#import "RNAVEditing.h"
#if __has_include("RCTUtils.h")
#import "RCTUtils.h"
#else
#import <React/RCTUtils.h>
#endif
#if __has_include("RCTConvert.h")
#import "RCTConvert.h"
#else
#import <React/RCTConvert.h>
#endif
@implementation RNAVEditing
@synthesize videoAsset,audioAsset;
RCT_EXPORT_MODULE();
RCT_EXPORT_METHOD(audioVideoSpeedFilter:(NSDictionary *)videoObject
audioObject:(NSDictionary *)audioObject
errorCallback:(RCTResponseSenderBlock)failureCallback
callback:(RCTResponseSenderBlock)successCallback){
AVMutableComposition* mixComposition = [AVMutableComposition composition];
AVURLAsset *videoAsset = [self uriSource:videoObject];
//NSLog(@"%@",videoObject[@"motion"]);
CMTime duration;
if ([videoObject[@"duration"] doubleValue] == 0.0) {
duration = videoAsset.duration;
}else{
duration = CMTimeMakeWithSeconds([videoObject[@"duration"] doubleValue], 600);
}
//slow down whole video by 2.0
double videoScaleFactor = 2.0;
CMTime videoDuration = videoAsset.duration;
CMTime audioDuration = duration;
//Now we are creating the second AVMutableCompositionTrack containing our video and add it to our AVMutableComposition object
AVAssetTrack *videoAssetTrack = [[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
AVMutableCompositionTrack *a_compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
CMTime start = CMTimeMakeWithSeconds([videoObject[@"VideoStartTime"] doubleValue], 600);
CMTimeRange videoRange = CMTimeRangeMake(start, duration);
[a_compositionVideoTrack insertTimeRange:videoRange ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:kCMTimeZero error:nil];
double a_durarion = CMTimeGetSeconds(duration);
CMTime filterVideoMotion = CMTimeMake(videoDuration.value, videoDuration.timescale);
if([videoObject[@"motion"] intValue] == -2){
duration = CMTimeMakeWithSeconds(a_durarion * 2.0, 600);
filterVideoMotion = CMTimeMake(videoDuration.value*videoScaleFactor, videoDuration.timescale);
}
else if ([videoObject[@"motion"] intValue] == 2){
duration = CMTimeMakeWithSeconds(a_durarion / 2.0, 600);
filterVideoMotion = CMTimeMake(videoDuration.value/videoScaleFactor, videoDuration.timescale);
}
else if ([videoObject[@"motion"] intValue] == 4){
duration = CMTimeMakeWithSeconds(a_durarion / 4.0, 600);
filterVideoMotion = CMTimeMake(videoDuration.value/4.0, videoDuration.timescale);
}
//// ----------------------------- Filter Time -----------------------
[a_compositionVideoTrack scaleTimeRange:CMTimeRangeMake(kCMTimeZero, videoDuration)
toDuration:filterVideoMotion];
[a_compositionVideoTrack setPreferredTransform:videoAssetTrack.preferredTransform];
//Now we are creating the first AVMutableCompositionTrack containing our audio and add it to our AVMutableComposition object.
AVURLAsset *audioAsset = [self uriSource:audioObject];
start = CMTimeMakeWithSeconds([audioObject[@"AudioStartTime"] doubleValue], 600);
AVMutableCompositionTrack *b_compositionAudioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
//audioMatched audioDuration
if ([videoObject[@"audioMatched"] boolValue]){
CMTimeRange AudioRange = CMTimeRangeMake(start, audioDuration);
[b_compositionAudioTrack insertTimeRange:AudioRange ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:kCMTimeZero error:nil];
[b_compositionAudioTrack scaleTimeRange:CMTimeRangeMake(kCMTimeZero, videoDuration)
toDuration:filterVideoMotion];
}else{
CMTimeRange AudioRange = CMTimeRangeMake(start, duration);
[b_compositionAudioTrack insertTimeRange:AudioRange ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:kCMTimeZero error:nil];
}
//decide the path where you want to store the final video created with audio and video merge.
NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsDir = [dirPaths objectAtIndex:0];
NSString *outputFilePath = [docsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"Groups.mp4"]];
NSURL *outputFileUrl = [NSURL fileURLWithPath:outputFilePath];
if ([[NSFileManager defaultManager] fileExistsAtPath:outputFilePath])
[[NSFileManager defaultManager] removeItemAtPath:outputFilePath error:nil];
//Now create an AVAssetExportSession object that will save your final video at specified path.
AVAssetExportSession* _assetExport;
switch([videoObject[@"videoQuality"] intValue]){
case 1 :
_assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetLowQuality];
break;
case 2 :
_assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetMediumQuality];
break;
case 3 :
_assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetHighestQuality];
break;
case 4 :
_assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPreset1280x720];
break;
case 5 :
_assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPreset960x540];
break;
case 6 :
_assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPreset640x480];
break;
/* you can have any number of case statements */
default : /* Optional */
_assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetHighestQuality];
}
//Now create an AVAssetExportSession object that will save your final video at specified path.
_assetExport.outputFileType = @"com.apple.quicktime-movie";
_assetExport.outputURL = outputFileUrl;
//_assetExport.videoComposition = videoComposition;
if([videoObject[@"videoFileLimit"] intValue] != 0){
_assetExport.fileLengthLimit = [videoObject[@"videoFileLimit"] intValue];
}
[_assetExport exportAsynchronouslyWithCompletionHandler:
^(void) {
dispatch_async(dispatch_get_main_queue(), ^{
[self exportDidFinish:_assetExport errorCallback:failureCallback callback:successCallback];
});
}
];
}
RCT_EXPORT_METHOD(videoTriming:(NSDictionary *)videoObject
audioObject:(NSDictionary *)audioObject
errorCallback:(RCTResponseSenderBlock)failureCallback
callback:(RCTResponseSenderBlock)successCallback){
NSLog(@"%@ %@", NSStringFromClass([self class]), NSStringFromSelector(_cmd));
AVURLAsset *videoAsset = [self uriSource:videoObject];
CMTime duration;
if ([videoObject[@"duration"] doubleValue] == 0.0) {
duration = videoAsset.duration;
}else{
duration = CMTimeMakeWithSeconds([videoObject[@"Duration"] doubleValue], 600);
}
CMTime start = CMTimeMakeWithSeconds([videoObject[@"VideoStartTime"] doubleValue], 600);
CMTimeRange videoRange = CMTimeRangeMake(start, duration);
AVURLAsset *audioAsset = [self uriSource:audioObject];
start = CMTimeMakeWithSeconds([audioObject[@"AudioStartTime"] doubleValue], 600);
CMTimeRange AudioRange = CMTimeRangeMake(start, duration);
AVMutableComposition* mixComposition = [AVMutableComposition composition];
//slow down whole video by 2.0
double videoScaleFactor = 2.0;
CMTime videoDuration = videoAsset.duration;
//Now we are creating the first AVMutableCompositionTrack containing our audio and add it to our AVMutableComposition object.
AVMutableCompositionTrack *b_compositionAudioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
[b_compositionAudioTrack insertTimeRange:AudioRange ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:kCMTimeZero error:nil];
//Now we are creating the second AVMutableCompositionTrack containing our video and add it to our AVMutableComposition object.
AVAssetTrack *videoAssetTrack = [[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
AVMutableCompositionTrack *a_compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
[a_compositionVideoTrack insertTimeRange:videoRange ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:kCMTimeZero error:nil];
[a_compositionVideoTrack setPreferredTransform:videoAssetTrack.preferredTransform];
//decide the path where you want to store the final video created with audio and video merge.
NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsDir = [dirPaths objectAtIndex:0];
NSString *outputFilePath = [docsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"Groups.mp4"]];
NSURL *outputFileUrl = [NSURL fileURLWithPath:outputFilePath];
if ([[NSFileManager defaultManager] fileExistsAtPath:outputFilePath])
[[NSFileManager defaultManager] removeItemAtPath:outputFilePath error:nil];
//Now create an AVAssetExportSession object that will save your final video at specified path.
AVAssetExportSession* _assetExport;
switch([videoObject[@"videoQuality"] intValue]){
case 1 :
_assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetLowQuality];
break;
case 2 :
_assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetMediumQuality];
break;
case 3 :
_assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetHighestQuality];
break;
case 4 :
_assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPreset1280x720];
break;
case 5 :
_assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPreset960x540];
break;
case 6 :
_assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPreset640x480];
break;
/* you can have any number of case statements */
default : /* Optional */
_assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetHighestQuality];
}
_assetExport.outputFileType = AVFileTypeQuickTimeMovie;
_assetExport.outputURL = outputFileUrl;
if([videoObject[@"videoFileLimit"] intValue] != 0){
_assetExport.fileLengthLimit = [videoObject[@"videoFileLimit"] intValue];
}
[_assetExport exportAsynchronouslyWithCompletionHandler:
^(void) {
dispatch_async(dispatch_get_main_queue(), ^{
[self exportDidFinish:_assetExport errorCallback:failureCallback callback:successCallback];
});
}
];
}
//RCT_EXPORT_METHOD(transcodeItem:(NSDictionary *)videoObject){
// AVURLAsset *videoAsset = [self uriSource:videoObject];
// // see if it's possible to export at the requested quality
// NSArray *compatiblePresets = [AVAssetExportSession
// exportPresetsCompatibleWithAsset:videoAsset];
//
//}
RCT_EXPORT_METHOD(deleteItem:(NSDictionary *)videoObject){
NSURL *videourl = [self uriNSURL:videoObject];
NSArray * arr = @[videourl];
PHFetchResult *asset = [PHAsset fetchAssetsWithALAssetURLs:(NSArray<NSURL *> *) arr options:nil];
NSLog(@"Working Fine");
NSLog(@"%@",asset);
[asset enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSLog(@"Working Fine");
NSLog(@"%@",[obj class]);
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
BOOL req = [obj canPerformEditOperation:PHAssetEditOperationDelete];
if (req) {
NSLog(@"true");
[PHAssetChangeRequest deleteAssets:@[obj]];
}
} completionHandler:^(BOOL success, NSError *error) {
NSLog(@"Finished Delete asset. %@", (success ? @"Success." : error));
if (success) {
NSLog(@"true");
}
}];
}];
}
- (void)exportDidFinish:(AVAssetExportSession*)session
errorCallback:(RCTResponseSenderBlock)failureCallback
callback:(RCTResponseSenderBlock)successCallback
{
NSURL *outputURL;
if(session.status == AVAssetExportSessionStatusCompleted){
outputURL = session.outputURL;
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
if ([library videoAtPathIsCompatibleWithSavedPhotosAlbum:outputURL]) {
[library writeVideoAtPathToSavedPhotosAlbum:outputURL
completionBlock:^(NSURL *assetURL, NSError *error){
dispatch_async(dispatch_get_main_queue(), ^{
if (error) {
NSLog(@"%@",error);
failureCallback(@[error]);
}else{
NSLog(@"%@",outputURL);
NSLog(@"VIdeo has been saved");
successCallback(@[@"merge video complete", outputURL.absoluteString]);
//[self loadMoviePlayer:outputURL];
}
});
}];
NSLog(@"%@",outputURL);
}
}
}
- (AVURLAsset*)uriSource:(NSDictionary *)pathObject
{
bool isNetwork = [RCTConvert BOOL:pathObject[@"isNetwork"]];
bool isAsset = [RCTConvert BOOL:pathObject[@"isAsset"]];
NSString *uri = pathObject[@"uri"];
NSString *type = pathObject[@"type"];
NSURL *url = (isNetwork || isAsset) ?
[NSURL URLWithString:uri] :
[[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:uri ofType:type]];
AVURLAsset *asset;
if (isAsset || isNetwork) {
asset = [[AVURLAsset alloc]initWithURL:url options:nil];
}
return asset;
}
- (NSURL*)uriNSURL:(NSDictionary *)pathObject
{
bool isNetwork = [RCTConvert BOOL:pathObject[@"isNetwork"]];
bool isAsset = [RCTConvert BOOL:pathObject[@"isAsset"]];
NSString *uri = pathObject[@"uri"];
NSString *type = pathObject[@"type"];
NSURL *url = (isNetwork || isAsset) ?
[NSURL URLWithString:uri] :
[[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:uri ofType:type]];
return url;
}
@end