直接上代码
- (void)cropWithVideoUrlStr:(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];
//获取视频总时长
Float64 duration = CMTimeGetSeconds(asset.duration);
NSString *outputFilePath = [self createVideoFilePath];
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 - startTime,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;
}
}];
}
}
- (void)cropWithVideoUrlStr:(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];
//获取视频总时长
Float64 duration = CMTimeGetSeconds(asset.duration);
NSString *outputFilePath = [self createVideoFilePath];
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 - startTime,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;
}
}];
}
}