码迷,mamicode.com
首页 > 移动开发 > 详细

ios 获得本地视频的帧数的截图

时间:2018-10-29 11:16:12      阅读:241      评论:0      收藏:0      [点我收藏+]

标签:height   cat   ror   image   gen   top   imeoption   play   lease   

ios 本地视频文件截图,有的类已经deprecated,提供三个函数以供使用

+ (UIImage*)getVideoThumbnailWithUrl:(NSURL*)videoUrl  second:(CGFloat)second size:(CGSize)size
{
    if (!videoUrl)
    {
        NSLog(@"WARNING:videoUrl为空");
        return nil;
    }
    AVURLAsset *urlSet = [AVURLAsset assetWithURL:videoUrl];
    AVAssetImageGenerator *imageGenerator = [AVAssetImageGenerator assetImageGeneratorWithAsset:urlSet];
    imageGenerator.appliesPreferredTrackTransform = YES;
    imageGenerator.apertureMode = AVAssetImageGeneratorApertureModeEncodedPixels;
    
    /*
     如果不需要获取缩略图,就设置为NO,如果需要获取缩略图,则maximumSize为获取的最大尺寸。
     以BBC为例,getThumbnail = NO时,打印宽高数据为:1920*1072。
     getThumbnail = YES时,maximumSize为100*100。打印宽高数据为:100*55.
     注:不乘[UIScreen mainScreen].scale,会发现缩略图在100*100很虚。
     */
    BOOL getThumbnail = YES;
    if (getThumbnail)
    {
        CGFloat width = [UIScreen mainScreen].scale * size.width;
        CGFloat height = [UIScreen mainScreen].scale * size.height;
        imageGenerator.maximumSize =  CGSizeMake(height, width);
    }
    NSError *error = nil;
    CMTime time = CMTimeMake(second,k_FPS);
    CMTime actucalTime;
    CGImageRef cgImage = [imageGenerator copyCGImageAtTime:time actualTime:&actucalTime error:&error];
    if (error) {
        NSLog(@"ERROR:获取视频图片失败,%@",error.domain);
    }
    CMTimeShow(actucalTime);
    UIImage *image = [UIImage imageWithCGImage:cgImage];
    NSLog(@"imageWidth=%f,imageHeight=%f",image.size.width,image.size.height);
    CGImageRelease(cgImage);
    return image;
}


+(UIImage*)getVideoImageWithUrl:(NSURL*)videoUrl second:(CGFloat)second{
    MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc]initWithContentURL:videoUrl];
    moviePlayer.shouldAutoplay = NO;
    UIImage *thumbnail = [moviePlayer thumbnailImageAtTime:second timeOption:MPMovieTimeOptionNearestKeyFrame];
    return thumbnail;
}

+(UIImage*)getFristFrameImageWithUrl:(NSURL*)videoUrl{
    MPMoviePlayerController *mp = [[MPMoviePlayerController alloc]
                                   initWithContentURL:videoUrl];
    UIImage *img = [mp thumbnailImageAtTime:0.0
                                 timeOption:MPMovieTimeOptionNearestKeyFrame];
    [mp stop];
    return img;
}

第一个函数的size填你得视频的尺寸为最佳

ios 获得本地视频的帧数的截图

标签:height   cat   ror   image   gen   top   imeoption   play   lease   

原文地址:https://www.cnblogs.com/RoysPhoneBlog/p/9869240.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!