我已經創建了一個代碼來在特定時間獲取幀,如下所示。請檢查它可能會幫助你。
-(void)getArrayOfFrameFromVideoURLs:(NSURL*)outputFileURL
{
ImagesMainArray = [NSMutableArray array];
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:outputFileURL
options:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], AVURLAssetPreferPreciseDurationAndTimingKey, nil]] ;
AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
generator.appliesPreferredTrackTransform = YES; // if I omit this, the frames are rotated 90° (didn't try in landscape)
AVVideoComposition * composition = [AVVideoComposition videoCompositionWithPropertiesOfAsset:asset];
// Retrieving the video properties
NSTimeInterval duration = CMTimeGetSeconds(asset.duration);
CGFloat frameDuration = CMTimeGetSeconds(composition.frameDuration);
CGSize renderSize = composition.renderSize;
CGFloat totalFrames = round(duration/frameDuration);
// Selecting each frame we want to extract : all of them.
// Get Count of Frames Per Second
AVAssetTrack * videoATrack = [[asset tracksWithMediaType:AVMediaTypeVideo] lastObject];
float fps = 0.0;
if(videoATrack) {
fps = videoATrack.nominalFrameRate;
}
NSLog(@"FramesPerSecond=%f",fps);
NSMutableArray * times = [NSMutableArray arrayWithCapacity:round(duration/frameDuration)];
for (int i=0; i<totalFrames; i++) {
NSValue *time = [NSValue valueWithCMTime:CMTimeMakeWithSeconds(i*frameDuration, composition.frameDuration.timescale)];
[times addObject:time];
}
__block int i = 0;
AVAssetImageGeneratorCompletionHandler handler = ^(CMTime requestedTime, CGImageRef im, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error){
if (result == AVAssetImageGeneratorSucceeded) {
[ImagesMainArray addObject:[UIImage imageWithCGImage:im]];
}
else
NSLog(@"Ouch: %@", error.description);
i++;
if(i == totalFrames) {
dispatch_async(dispatch_get_main_queue(), ^{
[self showArrayOfImgsMain];
});
}
};
// Launching the process...
generator.requestedTimeToleranceBefore = kCMTimeZero;
generator.requestedTimeToleranceAfter = kCMTimeZero;
generator.maximumSize = renderSize;
[generator generateCGImagesAsynchronouslyForTimes:times completionHandler:handler];
}
更多,您可以檢查:
https://developer.apple.com/library/ios/samplecode/AVPlayerDemo/Introduction/Intro.htm
http://iosguy.com/tag/avplayer/
https://stackoverflow.com/a/16398642
感謝您的建議。我決定使用MPMoviePlayerController,因爲它看起來更簡單一些。 – 2015-02-08 05:58:27