KZVideoConfig.m 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. //
  2. // KZVideoConfig.m
  3. // KZWeChatSmallVideo_OC
  4. //
  5. // Created by HouKangzhu on 16/7/19.
  6. // Copyright © 2016年 侯康柱. All rights reserved.
  7. //
  8. #import "KZVideoConfig.h"
  9. #import <AVFoundation/AVFoundation.h>
  10. void kz_dispatch_after(float time, dispatch_block_t block)
  11. {
  12. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(time * NSEC_PER_SEC)), dispatch_get_main_queue(), block);
  13. }
  14. @implementation KZVideoConfig
  15. + (CGRect)viewFrameWithType:(KZVideoViewShowType)type {
  16. if (type == KZVideoViewShowTypeSingle) {
  17. return [UIScreen mainScreen].bounds;
  18. }
  19. CGFloat viewHeight = kzSCREEN_WIDTH/kzVideo_w_h + 20 + kzControViewHeight;
  20. return CGRectMake(0, kzSCREEN_HEIGHT - viewHeight, kzSCREEN_WIDTH, viewHeight);
  21. }
  22. + (CGSize)videoViewDefaultSize {
  23. return [UIScreen mainScreen].bounds.size;
  24. }
  25. + (CGSize)defualtVideoSize {
  26. CGSize size = [UIScreen mainScreen].bounds.size;
  27. return CGSizeMake(kzVideoWidthPX, kzVideoWidthPX * size.height / size.width);
  28. }
  29. + (NSArray *)gradualColors {
  30. return @[(__bridge id)[UIColor greenColor].CGColor,(__bridge id)[UIColor yellowColor].CGColor,];
  31. }
  32. + (void)motionBlurView:(UIView *)superView {
  33. superView.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8];
  34. UIToolbar *bar = [[UIToolbar alloc] initWithFrame:superView.bounds];
  35. [bar setBarStyle:UIBarStyleBlackTranslucent];
  36. bar.clipsToBounds = YES;
  37. [superView addSubview:bar];
  38. }
  39. + (void)showHinInfo:(NSString *)text inView:(UIView *)superView frame:(CGRect)frame timeLong:(NSTimeInterval)time {
  40. __block UILabel *zoomLab = [[UILabel alloc] initWithFrame:frame];
  41. zoomLab.font = [UIFont boldSystemFontOfSize:15.0];
  42. zoomLab.text = text;
  43. zoomLab.textColor = [UIColor whiteColor];
  44. zoomLab.textAlignment = NSTextAlignmentCenter;
  45. [superView addSubview:zoomLab];
  46. [superView bringSubviewToFront:zoomLab];
  47. kz_dispatch_after(1.6, ^{
  48. [zoomLab removeFromSuperview];
  49. });
  50. }
  51. @end
  52. @implementation KZVideoModel
  53. + (instancetype)modelWithPath:(NSString *)videoPath thumPath:(NSString *)thumPath recordTime:(NSDate *)recordTime {
  54. KZVideoModel *model = [[KZVideoModel alloc] init];
  55. model.videoAbsolutePath = videoPath;
  56. model.thumAbsolutePath = thumPath;
  57. model.recordTime = recordTime;
  58. return model;
  59. }
  60. @end
  61. @implementation KZVideoUtil
  62. + (BOOL)existVideo {
  63. NSFileManager *fileManager = [NSFileManager defaultManager];
  64. NSArray *nameList = [fileManager subpathsAtPath:[self getVideoPath]];
  65. return nameList.count > 0;
  66. }
  67. + (NSMutableArray *)getVideoList {
  68. NSFileManager *fileManager = [NSFileManager defaultManager];
  69. NSMutableArray *modelList = [NSMutableArray array];
  70. NSArray *nameList = [fileManager subpathsAtPath:[self getVideoPath]];
  71. for (NSString *name in nameList) {
  72. if ([name hasSuffix:@".jpg"]) {
  73. KZVideoModel *model = [[KZVideoModel alloc] init];
  74. NSString *thumAbsolutePath = [[self getVideoPath] stringByAppendingPathComponent:name];
  75. model.thumAbsolutePath = thumAbsolutePath;
  76. NSString *totalVideoPath = [thumAbsolutePath stringByReplacingOccurrencesOfString:@"jpg" withString:@"mp4"];
  77. if ([fileManager fileExistsAtPath:totalVideoPath]) {
  78. model.videoAbsolutePath = totalVideoPath;
  79. }
  80. NSString *timeString = [name substringToIndex:(name.length-4)];
  81. NSDateFormatter *dateformate = [[NSDateFormatter alloc]init];
  82. dateformate.dateFormat = @"yyyy-MM-dd_HH-mm-ss";
  83. NSDate *date = [dateformate dateFromString:timeString];
  84. model.recordTime = date;
  85. [modelList addObject:model];
  86. }
  87. }
  88. return modelList;
  89. }
  90. + (NSArray *)getSortVideoList {
  91. NSArray *oldList = [self getVideoList];
  92. NSArray *sortList = [oldList sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
  93. KZVideoModel *model1 = obj1;
  94. KZVideoModel *model2 = obj2;
  95. NSComparisonResult compare = [model1.recordTime compare:model2.recordTime];
  96. switch (compare) {
  97. case NSOrderedDescending:
  98. return NSOrderedAscending;
  99. case NSOrderedAscending:
  100. return NSOrderedDescending;
  101. default:
  102. return compare;
  103. }
  104. }];
  105. return sortList;
  106. }
  107. + (void)saveThumImageWithVideoURL:(NSURL *)videoUrl second:(int64_t)second {
  108. AVURLAsset *urlSet = [AVURLAsset assetWithURL:videoUrl];
  109. AVAssetImageGenerator *imageGenerator = [AVAssetImageGenerator assetImageGeneratorWithAsset:urlSet];
  110. CMTime time = CMTimeMake(second, 10);
  111. NSError *error = nil;
  112. CGImageRef cgimage = [imageGenerator copyCGImageAtTime:time actualTime:nil error:&error];
  113. if (error) {
  114. NSLog(@"缩略图获取失败!:%@",error);
  115. return;
  116. }
  117. UIImage *image = [UIImage imageWithCGImage:cgimage scale:0.6 orientation:UIImageOrientationRight];
  118. NSData *imgData = UIImageJPEGRepresentation(image, 1.0);
  119. NSString *videoPath = [videoUrl.absoluteString stringByReplacingOccurrencesOfString:@"file://" withString: @""];
  120. NSString *thumPath = [videoPath stringByReplacingOccurrencesOfString:@"mp4" withString: @"jpg"];
  121. BOOL isok = [imgData writeToFile:thumPath atomically: YES];
  122. NSLog(@"缩略图获取结果:%d",isok);
  123. CGImageRelease(cgimage);
  124. }
  125. + (KZVideoModel *)createNewVideo {
  126. NSDate *currentDate = [NSDate date];
  127. NSDateFormatter *formate = [[NSDateFormatter alloc] init];
  128. formate.dateFormat = @"yyyy-MM-dd_HH:mm:ss";
  129. NSString *videoName = [formate stringFromDate:currentDate];
  130. NSString *videoPath = [self getVideoPath];
  131. KZVideoModel *model = [[KZVideoModel alloc] init];
  132. model.videoAbsolutePath = [videoPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.mp4",videoName]];
  133. model.thumAbsolutePath = [videoPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.jpg",videoName]];
  134. model.recordTime = currentDate;
  135. return model;
  136. }
  137. + (void)deleteVideo:(NSString *)videoPath {
  138. NSFileManager *fileManager = [NSFileManager defaultManager];
  139. NSError *error = nil;
  140. [fileManager removeItemAtPath:videoPath error:&error];
  141. if (error) {
  142. NSLog(@"删除视频失败:%@",error);
  143. }
  144. NSString *thumPath = [videoPath stringByReplacingOccurrencesOfString:@"mp4" withString:@"jpg"];
  145. NSError *error2 = nil;
  146. [fileManager removeItemAtPath:thumPath error:&error2];
  147. if (error2) {
  148. NSLog(@"删除缩略图失败:%@",error);
  149. }
  150. }
  151. + (NSString *)getVideoPath {
  152. return [self getDocumentSubPath:kzVideoDicName];
  153. }
  154. + (NSString *)getDocumentSubPath:(NSString *)dirName {
  155. NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES) firstObject];
  156. return [documentPath stringByAppendingPathComponent:dirName];
  157. }
  158. + (void)initialize {
  159. NSFileManager *fileManager = [NSFileManager defaultManager];
  160. NSString *dirPath = [self getVideoPath];
  161. NSError *error = nil;
  162. [fileManager createDirectoryAtPath:dirPath withIntermediateDirectories:YES attributes:nil error:&error];
  163. if (error) {
  164. NSLog(@"创建文件夹失败:%@",error);
  165. }
  166. }
  167. @end