iOS 获取视频截图 以及 CMTimeMakeWithSeconds 与 CMTimeMake的区别

首先贴上获取视频截图的方法:

//获取网络视频的缩略图
+ (UIImage *)getThumbnailImageFromVideoURL:(NSURL *)URL time:(NSTimeInterval )videoTime{
    
    if (!URL) return nil;
    
    UIImage *shotImage;
    
    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:URL options:nil];
    
    AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:asset];
    
    gen.appliesPreferredTrackTransform = YES;
    
    CMTime time = CMTimeMakeWithSeconds(videoTime, 600);
    
    NSError *error = nil;
    
    CMTime actualTime;
    
    CGImageRef image = [gen copyCGImageAtTime:time actualTime:&actualTime error:&error];
    
    shotImage = [[UIImage alloc] initWithCGImage:image];
    
    CGImageRelease(image);
    
    return shotImage;
}

//获取本地视频缩略图
+ (UIImage *)getThumbnailImageFromFilePath:(NSString *)videoPath time:(NSTimeInterval )videoTime {
    
    if (!videoPath) {
        return nil;
    }
    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:[[NSURL alloc] initFileURLWithPath:videoPath] options:nil];
    AVAssetImageGenerator *assetImageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
    assetImageGenerator.appliesPreferredTrackTransform = YES;
    assetImageGenerator.apertureMode = AVAssetImageGeneratorApertureModeEncodedPixels;
    
    CGImageRef thumbnailImageRef = NULL;
    CFTimeInterval thumbnailImageTime = videoTime;
    thumbnailImageRef = [assetImageGenerator copyCGImageAtTime:CMTimeMake(thumbnailImageTime, 600)
                                                    actualTime:NULL error:nil];
    
    
    if (!thumbnailImageRef) {
        return nil;
    }
    
    UIImage *thumbnailImage = [[UIImage alloc] initWithCGImage:thumbnailImageRef];
    
    
    CFRelease(thumbnailImageRef);
    
    return thumbnailImage;
    
}

注意:这种获取视频截图的方法适用于mp4等视频,不能用于m3u8上。当然,用这种方式有一定几率会失败,这也是个很诡异的bug,我会在下一篇文章中讨论这个问题,今天就此略过。
CMTime 是一个用来描述视频时间的结构体。它有两个构造函数:CMTimeMake和CMTimeMakeWithSeconds 。这两个的区别是 :CMTimeMake(a,b) a当前第几帧, b每秒钟多少帧,当前播放时间a/b。 CMTimeMakeWithSeconds(a,b) a当前时间,b每秒钟多少帧。下面用代码来说明它:

Float64 seconds = 3;  
int32_t preferredTimeScale = 600;  //处理视频内容事常见的时间刻度为600,这是大部分视频帧率24fps、25fps、30fps的公倍数。音频数据常见的时间刻度就是采样率,譬如44 100(44.1kHZ)或48 000(48kHZ)。
CMTime inTime = CMTimeMakeWithSeconds(seconds, preferredTimeScale);  
CMTimeShow(inTime);  
输出: {1800/600 = 3.000} 
代表当前时间为3s,视频一共有1800帧,一秒钟600帧  
CMTimeMake  
 int64_t value = 1100;  
int32_t preferredTimeScale = 600;  
CMTime inTime = CMTimeMake(value, preferredTimeScale);  
CMTimeShow(inTime);  
OUTPUT: {1100/600 = 1.833}  
代表时间为1.833s, 视频一共1100帧,每秒600帧 。 其实,在我们这里,我们关心的只有最后那个总时间。 换句话说,我们把那个(0, 600)换成(x, 600) 是没问题的。 
requestedTimeTolerance  
那么为什么,效果差了这么多呢?我们可以把CGImageRef image = [gen copyCGImageAtTime:time  
  actualTime:&actualTime  
  error:&error];  
返回的 actualTime实际时间输出一下  
CMTimeShow(actualTime)  
就会发现时间差的很远。 这是为什么呢? 首先 actualTime 使用的 fps * 1000 当每秒的帧率, 顺便普及下fps的获取方法 :float fps = [[[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] nominalFrameRate];  
然后我们来思考为什么要有 requestTime 和 actualTime 呢? 开始对这个api 很困惑: 为什么我request的时间 不等于actualTime?后来查了一下文档,当你想要一个时间点的某一帧的时候,它会在一个范围内找,如果有缓存,或者有在索引内的关键帧,就直接返回,从而优化性能。  
这个定义范围的API就是 requestedTimeToleranceAfter 和 requestedTimeToleranceBefore  
如果我们要精确时间,那么可以设置:  
 gen.requestedTimeToleranceAfter = kCMTimeZero;  
 gen.requestedTimeToleranceBefore = kCMTimeZero;  

你可能感兴趣的:(iOS 获取视频截图 以及 CMTimeMakeWithSeconds 与 CMTimeMake的区别)