欢迎光临外链购买平台,轻松为你获得外链购买的成功案例,友链相关事宜咨询:18978701720,黄经理

外链购买,专注打造匠心好平台

一个好的外链购买,可以为你轻松解决获客难题

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

作者:jcmp      发布时间:2021-04-12      浏览量:0
获取视频预览图,这里用到了SDIm

获取视频预览图,这里用到了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;

}

returnlength ;

}