Video capture picture AVAssetImageGenerator

//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:

Usually developers think that the presentation format of time should be floating-point data. We generally use NSTimeInterval. In fact, it is a simple double-precision double type, but it is just typedef. However, due to floating-point data calculation, it is easy to cause loss of precision. Some application scenarios that require high precision are obviously not suitable, so Apple defines the CMTime data type as the time format in the Core Media framework. The structure is as follows:
typedef struct{
CMTimeValuevalue;
 CMTimeScaletimescale; 
CMTimeFlagsflags; 
CMTimeEpochepoch; } CMTime;
Obviously, the CMTime definition is a C language structure, CMTime represents time in the form of a fraction, value represents the numerator, timescale represents the denominator, and flags is a bit mask, which represents the specified state of the time.
The following code represents the 5th second, 10 frames per second.
CMTime time=CMTimeMakeWithSeconds(5, 10); 

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.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324953314&siteId=291194637
Recommended