Created
January 1, 2017 22:09
-
-
Save jvcleave/c37f0bbcefde0ebc4f2467f0f6ff57b0 to your computer and use it in GitHub Desktop.
video frames to ofImage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//-------------------------------------------------------------- | |
void ofApp::setup() { | |
NSURL* videoUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"hands" ofType:@"m4v"]]; | |
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoUrl options:nil]; | |
AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset]; | |
generator.requestedTimeToleranceAfter = kCMTimeZero; | |
generator.requestedTimeToleranceBefore = kCMTimeZero; | |
float seconds = (asset.duration.value/asset.duration.timescale); | |
CGSize size = [[[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] naturalSize]; | |
NSLog(@"seconds: %f width: %f height: %f ", seconds, size.width, size.height); | |
int withFPS = 5; | |
NSMutableArray* frameImages = [NSMutableArray new]; | |
#if 1 | |
for (Float64 i = 0; i < CMTimeGetSeconds(asset.duration) * withFPS ; i++){ | |
@autoreleasepool { | |
CMTime time = CMTimeMake(i, withFPS); | |
NSError *err; | |
CMTime actualTime; | |
CGImageRef image = [generator copyCGImageAtTime:time actualTime:&actualTime error:&err]; | |
UIImage *generatedImage = [[UIImage alloc] initWithCGImage:image]; | |
[frameImages addObject:generatedImage]; | |
ofImage imageOF; | |
ofxiOSUIImageToOFImage(generatedImage, imageOF, size.width, size.height); | |
images.push_back(imageOF); | |
CGImageRelease(image); | |
} | |
} | |
#endif | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment