获取视频帧图片

PublicUtils.m

+(UIImage *) imageWithVideoUrl:(NSURL *)videoURL time:(CMTime) time
{
    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil];
    AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:asset];
    [asset release];
    asset = nil;
    gen.appliesPreferredTrackTransform = YES;
    NSError *error  =nil;
    CMTime actualTime;
    //获取time处的视频截图
    CGImageRef image = [gen copyCGImageAtTime:time actualTime:&actualTime error:&error];
    [gen release];
    gen = nil;
    //将CGImageref转换成UIImage
    UIImage *thumb = [[[UIImage alloc] initWithCGImage:image] autorelease];
    CGImageRelease(image);
    return thumb;
}

例如获取第一帧截图:

NSURL* Url = [NSURL URLWithString:urlStr];
CMTime time = CMTimeMake(0,30);
[PublicUtils imageWithVideoUrl:Url time:time]

关于CMTime

CMTimeMake(a,b) a当前第几帧, b每秒钟多少帧.当前播放时间a/b

CMTimeMakeWithSeconds(a,b) a当前时间,b每秒钟多少帧.

CMTimeMake

CMTime CMTimeMake (
   int64_t value,
   int32_t timescale
);
posted @ 2015-08-24 16:25  沙影无痕  阅读(250)  评论(0编辑  收藏  举报