• iOS 对视频抽帧。


    这里有两种方法可以采用,

    方法一:使用MPMoviePlayerController

    MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc]  initWithContentURL:videoURL];
    
    moviePlayer.shouldAutoplay = NO; 
    UIImage *thumbnail = [moviePlayer thumbnailImageAtTime:time timeOption:MPMovieTimeOptionNearestKeyFrame];
    

    优点:操作简单,省事

    缺点:不够轻量,抓取图片耗时。

    方法二:使用AVAssetImageGenerator

    + (UIImage*) thumbnailImageForVideo :( NSURL *)videoURL atTime :( NSTimeInterval)time {
        AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil];
        NSParameterAssert(asset);
        AVAssetImageGenerator *assetImageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
        assetImageGenerator.appliesPreferredTrackTransform = YES;
        assetImageGenerator.apertureMode = AVAssetImageGeneratorApertureModeEncodedPixels;
    
        CGImageRef thumbnailImageRef = NULL;
        CFTimeInterval thumbnailImageTime = time;
        NSError *thumbnailImageGenerationError = nil;
        thumbnailImageRef = [assetImageGenerator copyCGImageAtTime:CMTimeMake(thumbnailImageTime, 60) actualTime:NULL error:&thumbnailImageGenerationError];
    
        if (!thumbnailImageRef)
            NSLog(@"thumbnailImageGenerationError %@", thumbnailImageGenerationError);
    
        UIImage *thumbnailImage = thumbnailImageRef ? [[UIImage alloc] initWithCGImage:thumbnailImageRef] : nil;
    
        return thumbnailImage;
    }
    
  • 相关阅读:
    Collection
    DP
    JVM
    算法 *-* 并查集Union-Find(连通性)
    log4j
    log4j
    第254期:宠物如何导航回家
    第254期:宠物如何导航回家
    多线程
    多线程
  • 原文地址:https://www.cnblogs.com/sparks/p/4173882.html
Copyright © 2020-2023  润新知