Skip to content

Instantly share code, notes, and snippets.

@jvcleave
Created January 1, 2017 22:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jvcleave/c37f0bbcefde0ebc4f2467f0f6ff57b0 to your computer and use it in GitHub Desktop.
Save jvcleave/c37f0bbcefde0ebc4f2467f0f6ff57b0 to your computer and use it in GitHub Desktop.
video frames to ofImage
//--------------------------------------------------------------
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