YOUPAISHMediaManager.m 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. //
  2. // YOUPAISHMediaManager.m
  3. // LoongsCity
  4. //
  5. // Created by 查斯图 on 2018/3/30.
  6. // Copyright © 2018年 xiaoxiong. All rights reserved.
  7. //
  8. #import "YOUPAISHMediaManager.h"
  9. #import <AVFoundation/AVFoundation.h>
  10. @implementation YOUPAISHMediaManager
  11. + (void)youpaifcropWithVideoUrlStr:(NSURL *)videoUrl start:(CGFloat)startTime end:(CGFloat)endTime completion:(void (^)(NSURL *outputURL, Float64 videoDuration, BOOL isSuccess))completionHandle
  12. {
  13. AVURLAsset *asset =[[AVURLAsset alloc] initWithURL:videoUrl options:nil];
  14. NSString *docDirPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0];
  15. NSString *outputFilePath = [NSString stringWithFormat:@"%@/MediaTool%@.mp4", docDirPath , [self getCurrentTime]];
  16. NSURL *outputFileUrl = [NSURL fileURLWithPath:outputFilePath];
  17. NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:asset];
  18. if ([compatiblePresets containsObject:AVAssetExportPresetMediumQuality])
  19. {
  20. AVAssetExportSession *exportSession = [[AVAssetExportSession alloc]
  21. initWithAsset:asset presetName:AVAssetExportPresetPassthrough];
  22. NSURL *outputURL = outputFileUrl;
  23. exportSession.outputURL = outputURL;
  24. exportSession.outputFileType = AVFileTypeMPEG4;
  25. exportSession.shouldOptimizeForNetworkUse = YES;
  26. CMTime start = CMTimeMakeWithSeconds(startTime, asset.duration.timescale);
  27. CMTime duration = CMTimeMakeWithSeconds(endTime,asset.duration.timescale);
  28. CMTimeRange range = CMTimeRangeMake(start, duration);
  29. exportSession.timeRange = range;
  30. [exportSession exportAsynchronouslyWithCompletionHandler:^{
  31. switch ([exportSession status]) {
  32. case AVAssetExportSessionStatusFailed:
  33. {
  34. NSLog(@"合成失败:%@", [[exportSession error] description]);
  35. completionHandle(outputURL, endTime, NO);
  36. }
  37. break;
  38. case AVAssetExportSessionStatusCancelled:
  39. {
  40. completionHandle(outputURL, endTime, NO);
  41. }
  42. break;
  43. case AVAssetExportSessionStatusCompleted:
  44. {
  45. completionHandle(outputURL, endTime, YES);
  46. }
  47. break;
  48. default:
  49. {
  50. completionHandle(outputURL, endTime, NO);
  51. } break;
  52. }
  53. }];
  54. }
  55. }
  56. //获取当地时间
  57. + (NSString *)getCurrentTime {
  58. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  59. [formatter setDateFormat:@"HH:mm:ss"];
  60. NSString *dateTime = [formatter stringFromDate:[NSDate date]];
  61. return dateTime;
  62. }
  63. @end