IOS How to get all frame from movie
/* Get all frame from movie */
+ (NSMutableArray*) getAllFrameFromMovie:(NSString*) folderName file:(NSString *) fileName {
/* Setting input movie */
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath;
if([folderName length] == 0) {
filePath = [documentsDirectory stringByAppendingPathComponent:fileName];
} else {
filePath = [[[[documentsDirectory stringByAppendingString:@"/"] stringByAppendingString:folderName]
stringByAppendingString:@"/"] stringByAppendingString:fileName];
}
NSString* video_inputFileName = fileName;
NSString* video_inputFilePath = filePath;
NSURL* video_inputFileUrl = [NSURL fileURLWithPath:video_inputFilePath];
NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:AVURLAssetPreferPreciseDurationAndTimingKey];
AVURLAsset* asset = [AVURLAsset URLAssetWithURL:video_inputFileUrl options:options];;
Float64 durationFrames = CMTimeGetSeconds([asset duration]) * 24.0;
AVMutableComposition *myComp = [AVMutableComposition composition];
AVMutableCompositionTrack *compositionVideoTrack = [myComp addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
NSError *error = nil;
BOOL ok = NO;
NSMutableArray *frameArray = [[NSMutableArray alloc] init];
AVAssetImageGenerator *generate = [[AVAssetImageGenerator alloc] initWithAsset:myComp];
NSError *err = nil;
for (int i = 0; i < floor(durationFrames); i++) {
CMTime startTime = CMTimeMake(i, 24);
CMTime endTime = CMTimeMake(i+1, 24);
CMTimeRange myRange = CMTimeRangeMake(startTime, endTime);
AVAssetTrack *sourceVideoTrack = [[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
ok = [compositionVideoTrack insertTimeRange:myRange ofTrack:sourceVideoTrack atTime:kCMTimeZero error:&error];
if (!ok) {
// Deal with the error.
}
CMTime time = CMTimeMake(0,1);
UIImage *currentImg = [UIImage imageWithCGImage:[generate copyCGImageAtTime:time actualTime:nil error:&err]];
[frameArray addObject:currentImg];
}
NSLog(@"This video is calculated at %f Frames..",durationFrames);
NSLog(@"You made a total of %i Frames!!",[frameArray count]);
[generate release];
return frameArray;
}
+ (NSMutableArray*) getAllFrameFromMovie:(NSString*) folderName file:(NSString *) fileName {
/* Setting input movie */
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath;
if([folderName length] == 0) {
filePath = [documentsDirectory stringByAppendingPathComponent:fileName];
} else {
filePath = [[[[documentsDirectory stringByAppendingString:@"/"] stringByAppendingString:folderName]
stringByAppendingString:@"/"] stringByAppendingString:fileName];
}
NSString* video_inputFileName = fileName;
NSString* video_inputFilePath = filePath;
NSURL* video_inputFileUrl = [NSURL fileURLWithPath:video_inputFilePath];
NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:AVURLAssetPreferPreciseDurationAndTimingKey];
AVURLAsset* asset = [AVURLAsset URLAssetWithURL:video_inputFileUrl options:options];;
Float64 durationFrames = CMTimeGetSeconds([asset duration]) * 24.0;
AVMutableComposition *myComp = [AVMutableComposition composition];
AVMutableCompositionTrack *compositionVideoTrack = [myComp addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
NSError *error = nil;
BOOL ok = NO;
NSMutableArray *frameArray = [[NSMutableArray alloc] init];
AVAssetImageGenerator *generate = [[AVAssetImageGenerator alloc] initWithAsset:myComp];
NSError *err = nil;
for (int i = 0; i < floor(durationFrames); i++) {
CMTime startTime = CMTimeMake(i, 24);
CMTime endTime = CMTimeMake(i+1, 24);
CMTimeRange myRange = CMTimeRangeMake(startTime, endTime);
AVAssetTrack *sourceVideoTrack = [[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
ok = [compositionVideoTrack insertTimeRange:myRange ofTrack:sourceVideoTrack atTime:kCMTimeZero error:&error];
if (!ok) {
// Deal with the error.
}
CMTime time = CMTimeMake(0,1);
UIImage *currentImg = [UIImage imageWithCGImage:[generate copyCGImageAtTime:time actualTime:nil error:&err]];
[frameArray addObject:currentImg];
}
NSLog(@"This video is calculated at %f Frames..",durationFrames);
NSLog(@"You made a total of %i Frames!!",[frameArray count]);
[generate release];
return frameArray;
}
Comments
Post a Comment