获取视频的缩略图

方法:

AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:(NSURL *)videoPath options:nil];
    AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:asset];
    gen.appliesPreferredTrackTransform = YES;
    CMTime time = CMTimeMakeWithSeconds(1, 10);
    NSError *error = nil;
    CMTime actualTime;
    CGImageRef image = [gen copyCGImageAtTime:time actualTime:&actualTime error:&error];
  UIImage *img  = [[UIImage alloc] initWithCGImage:image];

注意:
1、要导入头文件如下
#import
#import
2、

编码中,我们经常使用
CMTime firstframe=CMTimeMake(1,10);
CMTime lastframe=CMTimeMake(10, 10);
“CMTime可是專門用來表示影片時間用的類別,
他的用法為: CMTimeMake(time, timeScale)
time指的就是時間(不是秒),
而時間要換算成秒就要看第二個參數timeScale了.
timeScale指的是1秒需要由幾個frame構成(可以視為fps),
因此真正要表達的時間就會是 time / timeScale 才會是秒.”
上面的代码可以这么理解,视频的fps(帧率)是10,firstframe是第一帧的视频时间为0.1秒,lastframe是第10帧视频时间为1秒。

或者换种写法 CMTime curFrame = CMTimeMake(第几帧, 帧率)

你可能感兴趣的:(获取视频的缩略图)