Skip to content

Commit aa11305

Browse files
committed
#5 Check file is supported or not
1 parent 164fd36 commit aa11305

File tree

3 files changed

+57
-34
lines changed

3 files changed

+57
-34
lines changed

DPVideoMerger/DPVideoMerger.xcodeproj/project.pbxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@
157157
developmentRegion = English;
158158
hasScannedForEncodings = 0;
159159
knownRegions = (
160+
English,
160161
en,
161162
Base,
162163
);

DPVideoMerger/DPVideoMerger/DPVideoMerger/DPVideoMerger.m

Lines changed: 52 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -44,32 +44,43 @@ + (void)mergeVideosWithFileURLs:(NSArray *)videoFileURLs
4444
__block int32_t highestFrameRate = 0;
4545
if (CGSizeEqualToSize(finalVideoSize, CGSizeMake(-1, -1))) {
4646
[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+
}];
7384
} else {
7485
if (finalVideoSize.height < 100 || finalVideoSize.width < 100) {
7586
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
8091
}
8192
videoSize = finalVideoSize;
8293
}
83-
94+
8495
[videoFileURLs enumerateObjectsUsingBlock:^(NSURL *videoFileURL, NSUInteger idx, BOOL *stop) {
8596
NSDictionary *options = @{AVURLAssetPreferPreciseDurationAndTimingKey:@YES};
8697
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:videoFileURL options:options];
@@ -113,7 +124,7 @@ + (void)mergeVideosWithFileURLs:(NSArray *)videoFileURLs
113124
videoCompositionInstruction.timeRange = CMTimeRangeMake(currentTime, timeRange.duration);
114125

115126
AVMutableVideoCompositionLayerInstruction * layerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:videoTrack];
116-
127+
117128
BOOL isVideoAssetPortrait_ = NO;
118129
CGAffineTransform videoTransform = videoAsset.preferredTransform;
119130
UIImageOrientation videoAssetOrientation_ = UIImageOrientationUp;
@@ -182,9 +193,8 @@ + (void)mergeVideosWithFileURLs:(NSArray *)videoFileURLs
182193
default:
183194
break;
184195
}
185-
196+
186197
videoCompositionInstruction.layerInstructions = @[layerInstruction];
187-
188198

189199
[instructions addObject:videoCompositionInstruction];
190200
currentTime = CMTimeAdd(currentTime, timeRange.duration);
@@ -253,21 +263,18 @@ + (void)gridMergeVideosWithFileURLs:(NSArray *)videoFileURLs
253263
andVideoResolution:(CGSize)resolution
254264
completion:(void(^)(NSURL *mergedVideoURL, NSError *error))completion {
255265
[DPVideoMerger gridMergeVideosWithFileURLs:videoFileURLs andVideoResolution:resolution andRepeatVideo:false andVideoDuration:-1 completion:completion];
256-
257266
}
258267
+ (void)gridMergeVideosWithFileURLs:(NSArray *)videoFileURLs
259268
andVideoResolution:(CGSize)resolution
260269
andRepeatVideo:(BOOL)isRepeatVideo
261270
completion:(void(^)(NSURL *mergedVideoURL, NSError *error))completion {
262271
[DPVideoMerger gridMergeVideosWithFileURLs:videoFileURLs andVideoResolution:resolution andRepeatVideo:isRepeatVideo andVideoDuration:-1 completion:completion];
263-
264272
}
265273
+ (void)gridMergeVideosWithFileURLs:(NSArray *)videoFileURLs
266274
andVideoResolution:(CGSize)resolution
267275
andVideoDuration:(NSInteger)videoDuration
268276
completion:(void(^)(NSURL *mergedVideoURL, NSError *error))completion {
269277
[DPVideoMerger gridMergeVideosWithFileURLs:videoFileURLs andVideoResolution:resolution andRepeatVideo:false andVideoDuration:videoDuration completion:completion];
270-
271278
}
272279
+ (void)gridMergeVideosWithFileURLs:(NSArray *)videoFileURLs
273280
andVideoResolution:(CGSize)resolution
@@ -299,7 +306,7 @@ + (void)gridMergeVideosWithFileURLs:(NSArray *)videoFileURLs
299306
if (videoFileURLs.count != 4) {
300307
NSError *error = [[NSError alloc] initWithDomain:@"DPVideoMerger" code:404 userInfo:@{NSLocalizedDescriptionKey : @"Provide 4 Videos",NSLocalizedFailureReasonErrorKey : @"error"}];
301308
dispatch_async(dispatch_get_main_queue(), ^{
302-
completion(nil,error);
309+
completion(nil,error);
303310
});
304311
return;
305312
}
@@ -311,6 +318,17 @@ + (void)gridMergeVideosWithFileURLs:(NSArray *)videoFileURLs
311318
[videoFileURLs enumerateObjectsUsingBlock:^(NSURL *videoFileURL, NSUInteger idx, BOOL *stop) {
312319
NSDictionary *options = @{AVURLAssetPreferPreciseDurationAndTimingKey:@YES};
313320
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+
}
314332
if (CMTimeCompare(maxTime, asset.duration) == -1) {
315333
maxTime = asset.duration;
316334
}

DPVideoMerger/DPVideoMerger/ViewController.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ - (void)viewDidLoad {
3434
[results enumerateObjectsUsingBlock:^(PHAsset *obj, NSUInteger idx, BOOL * _Nonnull stop) {
3535
[self->arrImgAssets addObject:obj];
3636
}];
37+
38+
DLog(@"MIME types the AVURLAsset class understands:-");
39+
DLog(@"%@", [AVURLAsset audiovisualMIMETypes]);
40+
3741
// Do any additional setup after loading the view, typically from a nib.
3842
}
3943

0 commit comments

Comments
 (0)