获取视频的缩略图

方法:

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 <MediaPlayer/MediaPlayer.h>
#import <AVFoundation/AVFoundation.h>
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(第几帧, 帧率)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容