//Get the video url address
NSURL *url=[NSURL URLWithString:[@"http://static.tripbe.com/videofiles/20121214/9533522808.f4v.mp4"
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
//Initialize AVURLAsset according to the address
AVURLAsset *urlAsset=[AVURLAsset assetWithURL:url];
//Initialize AVAssetImageGenerator
AVAssetImageGenerator * imageGenerator = [AVAssetImageGenerator assetImageGeneratorWithAsset: urlAsset];
NSError *error=nil;
//Get the 5th second of the video, 10 frames per second
CMTime time=CMTimeMakeWithSeconds(5, 10);
CMTime actualTime;
//Get the picture of the 5th second time through copyCGImageAtTime actualTime can be passed or not
CGImageRef cgImage= [imageGenerator copyCGImageAtTime:time actualTime:&actualTime error:&error];
if(error){
//Print Time
CMTimeShow(actualTime);
// get image
UIImage *image=[UIImage imageWithCGImage:cgImage];
//freed
CGImageRelease(cgImage);
}
The structure and use of these classes are described in detail below:
AVURLAsset: Inherited from AVAsset, which is the main class used by the AVFoundation framework to represent media. An AVAsset instance is an aggregate representation of a collection of one or more media data (audio and video tracks), which provides speech as an integrated collection of information, such as title, duration, etc. AVAsset is the superclass of other classes used to create asset instances for URLs and media and create new ones.
CMTime:
AVAssetImageGenerator: It is a class used to provide a thumbnail of the video or preview the frame of the video (there is an article saying that the real time of image generation may be inrequestedTimeToleranceBefore
和
requestedTimeToleranceAfter之间,也可能和请求时间无关)。
copyCGImageAtTime:actualTime:error: The meaning of the three parameters of this method is as follows,
The first one represents the picture you want to get at that point in time (the frames per second are different, and the pictures you get are also different).
The second represents the return time. The official document says that if you don't need it, you can pass NULL
The third one represents the error message, if there is an error message, it means that the picture has not been generated.