直播视频app源码,获取视频缩略图
作者:互联网
直播视频app源码,获取视频缩略图相关的代码
缩略图
AVAeest AVPlayer
NSMutableDictionary * headers = [NSMutableDictionary dictionary]; [headers setObject:@"yourHeader"forKey:@"User-Agent"]; self.urlAsset = [AVURLAsset URLAssetWithURL:self.videoURL options:@{@"AVURLAssetHTTPHeaderFieldsKey" : headers}]; self.playerItem = [AVPlayerItem playerItemWithAsset:self.urlAsset];
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:[NSURL fileURLWithPath:model.localPath] options:nil];
AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:asset];
gen.appliesPreferredTrackTransform = YES;
CMTime time = CMTimeMakeWithSeconds(0.1, 600);
NSError *error = nil;
CMTime actualTime;
CGImageRef image = [gen copyCGImageAtTime:time actualTime:&actualTime error:&error];
UIImage *thumb = [[UIImage alloc] initWithCGImage:image];
- (UIImage *)generatorImageVideoPath:(NSString *)videoPath
{
AVAsset *videoAsset = [AVAsset assetWithURL:[NSURL fileURLWithPath:videoPath]];
AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:videoAsset];
NSError *error = nil;
CMTime actualTime;
CGImageRef imageRef = [generator copyCGImageAtTime:CMTimeMakeWithSeconds(0.1, videoAsset.duration.timescale) actualTime:&actualTime error:&error];
UIImage *thumbnail = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return thumbnail;
}
+ (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;
}
-(void)encodeVideo
{
path = [[NSBundle mainBundle] pathForResource:@"sd" ofType:@"MP4"];
NSLog(@"=%@\n\n%@",NSHomeDirectory(),path);
NSArray *path2 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [path2 objectAtIndex:0];
bakPath =[documentsPath stringByAppendingPathComponent:@"aaa.MP4"];
AVURLAsset *avAsset = [AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:path] options:nil];
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:avAsset presetName:AVAssetExportPresetLowQuality];
exportSession.outputURL = [NSURL fileURLWithPath:bakPath];
exportSession.outputFileType = AVFileTypeMPEG4;
exportSession.shouldOptimizeForNetworkUse= YES;
[exportSession exportAsynchronouslyWithCompletionHandler:^(void)
{
switch (exportSession.status) {
case AVAssetExportSessionStatusCancelled:
NSLog(@"AVAssetExportSessionStatusCancelled");
break;
case AVAssetExportSessionStatusUnknown:
NSLog(@"AVAssetExportSessionStatusUnknown");
break;
case AVAssetExportSessionStatusWaiting:
NSLog(@"AVAssetExportSessionStatusWaiting");
break;
case AVAssetExportSessionStatusExporting:
NSLog(@"AVAssetExportSessionStatusExporting");
break;
case AVAssetExportSessionStatusCompleted:
path=bakPath;
//压缩成功
NSLog(@"Success");
break;
case AVAssetExportSessionStatusFailed:
{
NSError *error=exportSession.error;
if (error) {
}
NSLog(@"AVAssetExportSessionStatusFailed");
}
break;
default:
break;
}
}];
}
/**
* 把视频文件拆成图片保存在沙盒中
*
* @param fileUrl 本地视频文件URL
* @param fps 拆分时按此帧率进行拆分
* @param completedBlock 所有帧被拆完成后回调
*/
- (void)splitVideo:(NSURL *)fileUrl fps:(float)fps completedBlock:(void(^)(void))completedBlock {
if (!fileUrl) {
return;
}
NSDictionary *optDict = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:AVURLAssetPreferPreciseDurationAndTimingKey];
AVURLAsset *avasset = [[AVURLAsset alloc] initWithURL:fileUrl options:optDict];
CMTime cmtime = avasset.duration; //视频时间信息结构体
Float64 durationSeconds = CMTimeGetSeconds(cmtime); //视频总秒数
NSMutableArray *times = [NSMutableArray array];
Float64 totalFrames = durationSeconds * fps; //获得视频总帧数
totalNumber = (int)totalFrames;
CMTime timeFrame;
for (int i = 1; i <= totalFrames; i++) {
timeFrame = CMTimeMake(i, fps); //第i帧 帧率
NSValue *timeValue = [NSValue valueWithCMTime:timeFrame];
[times addObject:timeValue];
}
NSLog(@"------- start");
AVAssetImageGenerator *imgGenerator = [[AVAssetImageGenerator alloc] initWithAsset:avasset];
//防止时间出现偏差
imgGenerator.requestedTimeToleranceBefore = kCMTimeZero;
imgGenerator.requestedTimeToleranceAfter = kCMTimeZero;
NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSInteger timesCount = [times count];
[imgGenerator generateCGImagesAsynchronouslyForTimes:times completionHandler:^(CMTime requestedTime, CGImageRef _Nullable image, CMTime actualTime, AVAssetImageGeneratorResult result, NSError * _Nullable error) {
printf("current-----: %lld\n", requestedTime.value);
switch (result) {
case AVAssetImageGeneratorCancelled:
NSLog(@"Cancelled");
break;
case AVAssetImageGeneratorFailed:
NSLog(@"Failed");
break;
case AVAssetImageGeneratorSucceeded: {
NSString *filePath = [cachePath stringByAppendingPathComponent:[NSString stringWithFormat:@"/%lld.png",requestedTime.value]];
NSData *imgData = UIImagePNGRepresentation([UIImage imageWithCGImage:image]);
[imgData writeToFile:filePath atomically:YES];
if (requestedTime.value == timesCount) {
NSLog(@"completed");
if (completedBlock) {
completedBlock();
}
}
}
break;
}
}];
}
以上就是直播视频app源码,获取视频缩略图相关的代码, 更多内容欢迎关注之后的文章
标签:视频,alloc,缩略图,NSLog,AVURLAsset,break,源码,error,UIImage 来源: https://blog.csdn.net/yb1314111/article/details/112608764