工作需要

iOS 视频链接获取视频信息 预览图 视频时长

2020-03-18  本文已影响0人  前行的骆驼

获取视频预览图,这里用到了SDImage的缓存图片功能:

- (void)videoImageWithvideoURL:(NSURL *)videoURL atTime:(NSTimeInterval)time {

    //先从缓存中找是否有图片

    SDImageCache*cache =  [SDImageCache sharedImageCache];

    UIImage*memoryImage =  [cache imageFromMemoryCacheForKey:videoURL.absoluteString];

    if(memoryImage) {

        self.image= memoryImage;

        return;

    }else{

        UIImage*diskImage =  [cache imageFromDiskCacheForKey:videoURL.absoluteString];

        if(diskImage) {

            self.image= diskImage;

            return;

        }

    }

    if(!time) {

        time =1;

    }

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 

        AVURLAsset*asset = [[AVURLAsset alloc]initWithURL:videoURL options:nil];

        NSParameterAssert(asset);

        AVAssetImageGenerator*assetImageGenerator =[[AVAssetImageGenerator   alloc]initWithAsset:asset];

        assetImageGenerator.appliesPreferredTrackTransform=YES;

       assetImageGenerator.requestedTimeToleranceAfter = kCMTimeZero;//必须设置,否则时间对应不上

        assetImageGenerator.requestedTimeToleranceBefore = kCMTimeZero;//必须设置,否则时间对应不上

        assetImageGenerator.apertureMode = AVAssetImageGeneratorApertureModeEncodedPixels;

        CGImageRef thumbnailImageRef =NULL;

        CFTimeInterval thumbnailImageTime = time;

        NSError * thumbnailImageGenerationError =nil;

        thumbnailImageRef = [assetImageGenerator copyCGImageAtTime:CMTimeMakeWithSeconds(time, 10) actualTime:NULL error:&thumbnailImageGenerationError];

        if(!thumbnailImageRef)

            NSLog(@"thumbnailImageGenerationError %@",thumbnailImageGenerationError);

        UIImage*thumbnailImage = thumbnailImageRef ? [[UIImage alloc]initWithCGImage: thumbnailImageRef] :nil;

        dispatch_async(dispatch_get_main_queue(), ^{

            SDImageCache*cache =  [SDImageCache sharedImageCache];

            [cache storeImageToMemory:thumbnailImage forKey:videoURL.absoluteString];

            self.image= thumbnailImage;

        });

    });

}

//获取视频时长

+ (NSTimeInterval)getVideoTimeWithURL:(NSString *)urlStr {

    NSURL*movieURL = [NSURL URLWithString:urlStr];

    NSDictionary *opts = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:AVURLAssetPreferPreciseDurationAndTimingKey];

    AVURLAsset*urlAsset = [AVURLAsset URLAssetWithURL:movie URLoptions:opts];// 初始化视频媒体文件

    int minute =0, second =0;

    second = urlAsset.duration.value/ (float)urlAsset.duration.timescale;// 获取视频总时长,单位秒

    //NSLog(@"movie duration : %d", second);

    return second;

}

//获取视频大小

+ (long long)getVideoSizeWithURL:(NSString *)urlStr {

    NSURL*movieURL = [NSURL URLWithString:urlStr];

NSDictionary *opts = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:AVURLAssetPreferPreciseDurationAndTimingKey];

    AVURLAsset*urlAsset = [AVURLAsset URLAssetWithURL:movie URLoptions:opts];// 初始化视频媒体文件

    NSArray *array = [urlAsset tracksWithMediaType:AVMediaTypeVideo];// 明确媒体类型为视频

    long long length =0;    

    for(AVAssetTrack *track in array) {

        NSLog(@"track---:%@", track);

        NSLog(@"track.totalSampleDataLength---:%lld", track.totalSampleDataLength);// 视频文件字节大小

        length = track.totalSampleDataLength;

     }

     return length;

}

上一篇 下一篇

猜你喜欢

热点阅读