@@ -44,32 +44,43 @@ + (void)mergeVideosWithFileURLs:(NSArray *)videoFileURLs
44
44
__block int32_t highestFrameRate = 0 ;
45
45
if (CGSizeEqualToSize (finalVideoSize, CGSizeMake (-1 , -1 ))) {
46
46
[videoFileURLs enumerateObjectsUsingBlock: ^(NSURL *videoFileURL, NSUInteger idx, BOOL *stop) {
47
- NSDictionary *options = @{AVURLAssetPreferPreciseDurationAndTimingKey:@YES };
48
- AVURLAsset *asset = [AVURLAsset URLAssetWithURL: videoFileURL options: options];
49
- AVAssetTrack *videoAsset = [[asset tracksWithMediaType: AVMediaTypeVideo] firstObject ];
50
- if (CGSizeEqualToSize (videoSize, CGSizeZero )) {
51
- videoSize = videoAsset.naturalSize ;
52
- }
53
- BOOL isVideoAssetPortrait_ = NO ;
54
- CGAffineTransform videoTransform = videoAsset.preferredTransform ;
55
-
56
- if (videoTransform.a == 0 && videoTransform.b == 1.0 && videoTransform.c == -1.0 && videoTransform.d == 0 ) { isVideoAssetPortrait_ = YES ;}
57
- if (videoTransform.a == 0 && videoTransform.b == -1.0 && videoTransform.c == 1.0 && videoTransform.d == 0 ) { isVideoAssetPortrait_ = YES ;}
58
-
59
- CGFloat videoAssetWidth = videoAsset.naturalSize .width ;
60
- CGFloat videoAssetHeight = videoAsset.naturalSize .height ;
61
- if (isVideoAssetPortrait_) {
62
- videoAssetWidth = videoAsset.naturalSize .height ;
63
- videoAssetHeight = videoAsset.naturalSize .width ;
64
- }
65
-
66
- if (videoSize.height < videoAssetHeight){
67
- videoSize.height = videoAssetHeight;
68
- }
69
- if (videoSize.width < videoAssetWidth){
70
- videoSize.width = videoAssetWidth;
71
- }
72
- }];
47
+ NSDictionary *options = @{AVURLAssetPreferPreciseDurationAndTimingKey:@YES };
48
+ AVURLAsset *asset = [AVURLAsset URLAssetWithURL: videoFileURL options: options];
49
+ CGFloat length = (asset.duration .value )/(asset.duration .timescale );
50
+ if (length == 0.0 ) {
51
+ NSError *error = [[NSError alloc ] initWithDomain: @" DPVideoMerger" code: 404 userInfo: @{NSLocalizedDescriptionKey : [NSString stringWithFormat: @" File not suppoted '%@ '" ,videoFileURL] ,NSLocalizedFailureReasonErrorKey : @" error" }];
52
+ dispatch_async (dispatch_get_main_queue (), ^{
53
+ completion (nil ,error);
54
+ });
55
+ DLog (@" MIME types the AVURLAsset class understands:-" );
56
+ DLog (@" %@ " , [AVURLAsset audiovisualMIMETypes ]);
57
+ *stop = YES ;
58
+ return ;
59
+ }
60
+ AVAssetTrack *videoAsset = [[asset tracksWithMediaType: AVMediaTypeVideo] firstObject ];
61
+ if (CGSizeEqualToSize (videoSize, CGSizeZero )) {
62
+ videoSize = videoAsset.naturalSize ;
63
+ }
64
+ BOOL isVideoAssetPortrait_ = NO ;
65
+ CGAffineTransform videoTransform = videoAsset.preferredTransform ;
66
+
67
+ if (videoTransform.a == 0 && videoTransform.b == 1.0 && videoTransform.c == -1.0 && videoTransform.d == 0 ) { isVideoAssetPortrait_ = YES ;}
68
+ if (videoTransform.a == 0 && videoTransform.b == -1.0 && videoTransform.c == 1.0 && videoTransform.d == 0 ) { isVideoAssetPortrait_ = YES ;}
69
+
70
+ CGFloat videoAssetWidth = videoAsset.naturalSize .width ;
71
+ CGFloat videoAssetHeight = videoAsset.naturalSize .height ;
72
+ if (isVideoAssetPortrait_) {
73
+ videoAssetWidth = videoAsset.naturalSize .height ;
74
+ videoAssetHeight = videoAsset.naturalSize .width ;
75
+ }
76
+
77
+ if (videoSize.height < videoAssetHeight){
78
+ videoSize.height = videoAssetHeight;
79
+ }
80
+ if (videoSize.width < videoAssetWidth){
81
+ videoSize.width = videoAssetWidth;
82
+ }
83
+ }];
73
84
} else {
74
85
if (finalVideoSize.height < 100 || finalVideoSize.width < 100 ) {
75
86
NSError *error = [[NSError alloc ] initWithDomain: @" DPVideoMerger" code: 404 userInfo: @{NSLocalizedDescriptionKey : @" videoSize height/width should grater than equal to 100" ,NSLocalizedFailureReasonErrorKey : @" error" }];
@@ -80,7 +91,7 @@ + (void)mergeVideosWithFileURLs:(NSArray *)videoFileURLs
80
91
}
81
92
videoSize = finalVideoSize;
82
93
}
83
-
94
+
84
95
[videoFileURLs enumerateObjectsUsingBlock: ^(NSURL *videoFileURL, NSUInteger idx, BOOL *stop) {
85
96
NSDictionary *options = @{AVURLAssetPreferPreciseDurationAndTimingKey:@YES };
86
97
AVURLAsset *asset = [AVURLAsset URLAssetWithURL: videoFileURL options: options];
@@ -113,7 +124,7 @@ + (void)mergeVideosWithFileURLs:(NSArray *)videoFileURLs
113
124
videoCompositionInstruction.timeRange = CMTimeRangeMake (currentTime, timeRange.duration );
114
125
115
126
AVMutableVideoCompositionLayerInstruction * layerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack: videoTrack];
116
-
127
+
117
128
BOOL isVideoAssetPortrait_ = NO ;
118
129
CGAffineTransform videoTransform = videoAsset.preferredTransform ;
119
130
UIImageOrientation videoAssetOrientation_ = UIImageOrientationUp;
@@ -182,9 +193,8 @@ + (void)mergeVideosWithFileURLs:(NSArray *)videoFileURLs
182
193
default :
183
194
break ;
184
195
}
185
-
196
+
186
197
videoCompositionInstruction.layerInstructions = @[layerInstruction];
187
-
188
198
189
199
[instructions addObject: videoCompositionInstruction];
190
200
currentTime = CMTimeAdd (currentTime, timeRange.duration );
@@ -253,21 +263,18 @@ + (void)gridMergeVideosWithFileURLs:(NSArray *)videoFileURLs
253
263
andVideoResolution : (CGSize )resolution
254
264
completion : (void (^)(NSURL *mergedVideoURL, NSError *error))completion {
255
265
[DPVideoMerger gridMergeVideosWithFileURLs: videoFileURLs andVideoResolution: resolution andRepeatVideo: false andVideoDuration: -1 completion: completion];
256
-
257
266
}
258
267
+ (void )gridMergeVideosWithFileURLs : (NSArray *)videoFileURLs
259
268
andVideoResolution : (CGSize )resolution
260
269
andRepeatVideo : (BOOL )isRepeatVideo
261
270
completion : (void (^)(NSURL *mergedVideoURL, NSError *error))completion {
262
271
[DPVideoMerger gridMergeVideosWithFileURLs: videoFileURLs andVideoResolution: resolution andRepeatVideo: isRepeatVideo andVideoDuration: -1 completion: completion];
263
-
264
272
}
265
273
+ (void )gridMergeVideosWithFileURLs : (NSArray *)videoFileURLs
266
274
andVideoResolution : (CGSize )resolution
267
275
andVideoDuration : (NSInteger )videoDuration
268
276
completion : (void (^)(NSURL *mergedVideoURL, NSError *error))completion {
269
277
[DPVideoMerger gridMergeVideosWithFileURLs: videoFileURLs andVideoResolution: resolution andRepeatVideo: false andVideoDuration: videoDuration completion: completion];
270
-
271
278
}
272
279
+ (void )gridMergeVideosWithFileURLs : (NSArray *)videoFileURLs
273
280
andVideoResolution : (CGSize )resolution
@@ -299,7 +306,7 @@ + (void)gridMergeVideosWithFileURLs:(NSArray *)videoFileURLs
299
306
if (videoFileURLs.count != 4 ) {
300
307
NSError *error = [[NSError alloc ] initWithDomain: @" DPVideoMerger" code: 404 userInfo: @{NSLocalizedDescriptionKey : @" Provide 4 Videos" ,NSLocalizedFailureReasonErrorKey : @" error" }];
301
308
dispatch_async (dispatch_get_main_queue (), ^{
302
- completion (nil ,error);
309
+ completion (nil ,error);
303
310
});
304
311
return ;
305
312
}
@@ -311,6 +318,17 @@ + (void)gridMergeVideosWithFileURLs:(NSArray *)videoFileURLs
311
318
[videoFileURLs enumerateObjectsUsingBlock: ^(NSURL *videoFileURL, NSUInteger idx, BOOL *stop) {
312
319
NSDictionary *options = @{AVURLAssetPreferPreciseDurationAndTimingKey:@YES };
313
320
AVURLAsset *asset = [AVURLAsset URLAssetWithURL: videoFileURL options: options];
321
+ CGFloat length = (asset.duration .value )/(asset.duration .timescale );
322
+ if (length == 0.0 ) {
323
+ NSError *error = [[NSError alloc ] initWithDomain: @" DPVideoMerger" code: 404 userInfo: @{NSLocalizedDescriptionKey : [NSString stringWithFormat: @" File not suppoted '%@ '" ,videoFileURL] ,NSLocalizedFailureReasonErrorKey : @" error" }];
324
+ dispatch_async (dispatch_get_main_queue (), ^{
325
+ completion (nil ,error);
326
+ });
327
+ DLog (@" MIME types the AVURLAsset class understands:-" );
328
+ DLog (@" %@ " , [AVURLAsset audiovisualMIMETypes ]);
329
+ *stop = YES ;
330
+ return ;
331
+ }
314
332
if (CMTimeCompare (maxTime, asset.duration ) == -1 ) {
315
333
maxTime = asset.duration ;
316
334
}
0 commit comments