获取视频预览图,这里用到了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)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;
}
//由于该方法如果获取网络视频链接,会有个网络加载的过程,所以需要将其放到分线程中执行,避免阻塞主线程
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
long videoTime = [UIImageView getVideoTimeWithURL:vedioUrl];
dispatch_async(dispatch_get_main_queue(), ^{
//获取完回到主线程执行相应的操作
});
});