123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- //
- // YOUPAISHMediaManager.m
- // LoongsCity
- //
- // Created by 查斯图 on 2018/3/30.
- // Copyright © 2018年 xiaoxiong. All rights reserved.
- //
- #import "YOUPAISHMediaManager.h"
- #import <AVFoundation/AVFoundation.h>
- @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
|