美文网首页
使用AVFoundation生成视频缩略图

使用AVFoundation生成视频缩略图

作者: zhu哥哥 | 来源:发表于2019-03-21 16:13 被阅读0次

    概述

    AVFoundation生成视频缩略图主要靠如下两个类. AVURLAsset、AVAssetImageGenerator

    类名 说明
    AVURLAsset 该类是AVAsset的子类,AVAsset类专门用于获取多媒体的相关信息,包括获取多媒体的画面、声音等信息。而AVURLAsset子类的作用则是根据NSURL来初始化AVAsset对象。
    AVAssetImageGenerator 该类专门用于截取视频指定帧的画面。

    使用AVFoundation生成视频缩略图的步骤如下

    1、根据视频的NSURL创建AVURLAsset对象

       NSString *path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"MOV"];
       AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:[NSURL fileURLWithPath:path] options:nil];
    
    

    2、根据AVURLAsset对象创建AVAssetImageGenerator对象

        AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
        generator.appliesPreferredTrackTransform = YES;
        generator.requestedTimeToleranceAfter = kCMTimeZero;
        generator.requestedTimeToleranceBefore = kCMTimeZero;
    
    

    3、生成图像

        //CMTime是专门用于标识电影时间的结构体
        /**
         CMTimeMake(int64_t  value, int_32  timescale):
         第1个参数代表获取第几帧的截图,
         第2个参数代表每秒的帧数.因此实际截取的时间点是value/timescale.
         
         CMTimeMakeWithSeconds(Float64  seconds, int32_t  preferredTimeScale):
         第1个参数代表获取第几秒的截图,
         第2个参数则代表每秒的帧数.
         */
        CMTime time = CMTimeMakeWithSeconds(atTime, 600);
        NSError *error = nil;
        CMTime actualTime;
        
        //获取该视频指定时间点的视频截图
        /**
         该方法的第一个CMTime参数用于指定获取哪个时间点的视频截图
         第2个CMTime参数用于获取实际截图 位于哪个时间点.
         */
        CGImageRef image = [generator copyCGImageAtTime:time actualTime:&actualTime error:&error];
        
        UIImage *img = [[UIImage alloc] initWithCGImage:image];
        UIGraphicsBeginImageContext(CGSizeMake([[[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] naturalSize].width, [[[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] naturalSize].height));
        [img drawInRect:CGRectMake(0, 0, [[[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] naturalSize].width, [[[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] naturalSize].height)];
        UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        CGImageRelease(image);
    
    

    4、获取视频时长

    //获取本地视频的时长
    - (NSUInteger)durationWithVideo:(NSString *)videoPath {
        NSDictionary *opts = @{AVURLAssetPreferPreciseDurationAndTimingKey: @(NO)};
        AVURLAsset *urlAsset = [AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:videoPath] options:opts];     //初始化视频媒体文件
        NSUInteger second = 0;
        second = ceilf((double)urlAsset.duration.value / (double)urlAsset.duration.timescale); // 获取视频总时长,单位秒
        return second;
    }
    
    

    5、获取每一秒的图片

    self.videoDuration = [self durationWithVideo:self.videoUrlStr]; 
      //每隔1s截取一张图片
     for (int i = 0; i < self.videoDuration-1; i++) {
            UIImage *image = [self getVideoPreViewImageFromVideoPath:self.videoUrlStr withAtTime:i+0.1];
            [coverImgs addObject:image];       
     }
    
    

    6、图片数组添加到scrollView

     //图片宽
        float imgWidth = self.maxWidth/10.0;
        for (int i = 0; i< self.coverImgs.count; i++) {
            UIImageView *imageView = [[UIImageView alloc] initWithImage:[self.coverImgs objectAtIndex:i]];
            [imageView setFrame:CGRectMake(i*imgWidth, 0, imgWidth, 50)];
            [self.scrollView addSubview:imageView];
        }
        [self.scrollView setContentSize:CGSizeMake(imgWidth*self.coverImgs.count, 50)];
    
    

    未完待续...
    Demo稍后奉上

    相关文章

      网友评论

          本文标题:使用AVFoundation生成视频缩略图

          本文链接:https://www.haomeiwen.com/subject/qpfcvqtx.html