// // YOUPAISHMediaManager.m // LoongsCity // // Created by 查斯图 on 2018/3/30. // Copyright © 2018年 xiaoxiong. All rights reserved. // #import "YOUPAISHMediaManager.h" #import @implementation YOUPAISHMediaManager + (void)youpaifcropWithVideoUrlStr:(NSURL *)videoUrl start:(CGFloat)startTime end:(CGFloat)endTime completion:(void (^)(NSURL *outputURL, Float64 videoDuration, BOOL isSuccess))completionHandle { AVURLAsset *asset =[[AVURLAsset alloc] initWithURL:videoUrl options:nil]; NSString *docDirPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0]; NSString *outputFilePath = [NSString stringWithFormat:@"%@/MediaTool%@.mp4", docDirPath , [self getCurrentTime]]; NSURL *outputFileUrl = [NSURL fileURLWithPath:outputFilePath]; NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:asset]; if ([compatiblePresets containsObject:AVAssetExportPresetMediumQuality]) { AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetPassthrough]; NSURL *outputURL = outputFileUrl; exportSession.outputURL = outputURL; exportSession.outputFileType = AVFileTypeMPEG4; exportSession.shouldOptimizeForNetworkUse = YES; CMTime start = CMTimeMakeWithSeconds(startTime, asset.duration.timescale); CMTime duration = CMTimeMakeWithSeconds(endTime,asset.duration.timescale); CMTimeRange range = CMTimeRangeMake(start, duration); exportSession.timeRange = range; [exportSession exportAsynchronouslyWithCompletionHandler:^{ switch ([exportSession status]) { case AVAssetExportSessionStatusFailed: { NSLog(@"合成失败:%@", [[exportSession error] description]); completionHandle(outputURL, endTime, NO); } break; case AVAssetExportSessionStatusCancelled: { completionHandle(outputURL, endTime, NO); } break; case AVAssetExportSessionStatusCompleted: { completionHandle(outputURL, endTime, YES); } break; default: { completionHandle(outputURL, endTime, NO); } break; } }]; } } //获取当地时间 + (NSString *)getCurrentTime { NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"HH:mm:ss"]; NSString *dateTime = [formatter stringFromDate:[NSDate date]]; return dateTime; } @end