-
- 1.
iphone ios取出视频的每一帧图片
- [mImageGenerator generateCGImagesAsynchronouslyForTimes:[NSArray arrayWithObject:[NSValue valueWithCMTime:CMTimeMakeWithSeconds(time, NSEC_PER_SEC)]] completionHandler:
- ^(CMTime requestedTime, CGImageRef image, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error)
- {
- NSLog(@"actual got image at time:%f", CMTimeGetSeconds(actualTime));
- if (image)
- {
- [CATransaction begin];
- [CATransaction setDisableActions:YES];
- [layer setContents:(id)image];
- //UIImage *img = [UIImage imageWithCGImage:image];
- //UIImageWriteToSavedPhotosAlbum(img, self, nil, nil);
- [CATransaction commit];
- }
- }];
2.(1)如何取得第一帧
AVFoundationframework
- + (UIImage*) thumbnailImageForVideo:(NSURL *)videoURL atTime:(NSTimeInterval)time {
- AVURLAsset *asset = [[[AVURLAsset alloc] initWithURL:videoURL options:nil] autorelease];
- NSParameterAssert(asset);
- AVAssetImageGenerator *assetImageGenerator = [[[AVAssetImageGenerator alloc] initWithAsset:asset] autorelease];
- assetImageGenerator.appliesPreferredTrackTransform = YES;
- assetImageGenerator.apertureMode = AVAssetImageGeneratorApertureModeEncodedPixels;
- CGImageRef thumbnailImageRef = NULL;
- CFTimeInterval thumbnailImageTime = time;
- NSError *thumbnailImageGenerationError = nil;
- thumbnailImageRef = [assetImageGenerator copyCGImageAtTime:CMTimeMake(thumbnailImageTime, 60) actualTime:NULL error:&thumbnailImageGenerationError];
- if (!thumbnailImageRef)
- NSLog(@"thumbnailImageGenerationError %@", thumbnailImageGenerationError);
- UIImage *thumbnailImage = thumbnailImageRef ? [[[UIImage alloc] initWithCGImage:thumbnailImageRef] autorelease] : nil;
“CMTime可是專門用來表示影片時間用的類別,
他的用法為: CMTimeMake(time, timeScale)
time指的就是時間(不是秒),
而時間要換算成秒就要看第二個參數timeScale了.
timeScale指的是1秒需要由幾個frame構成(可以視為fps),
因此真正要表達的時間就會是 time / timeScale 才會是秒.”
上面的代码可以这么理解,视频的fps(帧率)是10,firstframe是第一帧的视频时间为0.1秒,lastframe是第10帧视频时间为1秒。
或者换种写法 CMTime curFrame = CMTimeMake(第几帧, 帧率); CMTimeMake(a, b); //a当前第几帧, b每秒钟多少帧.当前播放时间a/b
获得总帧数和帧率的方法
NSBundle *mainBundle = [NSBundle mainBundle]; NSString *urlAsString = [mainBundle pathForResource:@"asdf" ofType:@"mp4"]; NSDictionary *opts = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:AVURLAssetPreferPreciseDurationAndTimingKey]; NSURL *url = [NSURL fileURLWithPath:urlAsString]; AVURLAsset *myAsset = [[AVURLAsset alloc] initWithURL:url options:opts];CMTimeValue value <span style="font-size: 14px; color: rgb(255, 0, 0); font-family: Times;">= myAsset.duration.value;//</span><span style="color: rgb(255, 0, 0); font-size: 14px; font-family: Times;">总帧数</span><pre class="declaration" name="code" style="margin: 0px 0.333em 1em 0.5em; white-space: pre-wrap; word-wrap: break-word; font-family: Courier, Consolas, monospace;"> CMTimeScale timeScale = <span style="color: rgb(255, 0, 0); font-family: Times; font-size: 14px;"> myAsset.duration.timescale; //</span><span style="color: rgb(255, 0, 0); font-size: 14px; font-family: Times;">timescale为 fps</span>
- (2).
MPMoviePlayerController*moviePlayer = [[MPMoviePlayerControlleralloc] initWithContentURL:videoURL];
moviePlayer.shouldAutoplay =NO;
[moviePlayer Play];
UIImage*thumbnail = [moviePlayer thumbnailImageAtTime:time timeOption:MPMovieTimeOptionNearestKeyFrame];
[MoviePlayer Pause];