//首先拿到要裁剪视频的url
AVAsset* asset = [AVAssetassetWithURL:self.outputUrl];
AVMutableComposition *composition = [AVMutableComposition composition];
[composition addMutableTrackWithMediaType:AVMediaTypeVideopreferredTrackID:kCMPersistentTrackID_Invalid];
AVAssetTrack *videoTrack = [[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
//判断视频的方向,看是否需要旋转
UIInterfaceOrientation orientation = UIInterfaceOrientationPortrait;
NSArray *tracks = [asset tracksWithMediaType:AVMediaTypeVideo];
if([tracks count] >0) {
AVAssetTrack *videoTrack = [tracks objectAtIndex:0];
CGAffineTransform t = videoTrack.preferredTransform;
// Portrait
if(t.a ==0 && t.b ==1.0 && t.c == -1.0 && t.d ==0) {
orientation = UIInterfaceOrientationPortrait;
}
// PortraitUpsideDown
if(t.a ==0 && t.b == -1.0 && t.c ==1.0 && t.d ==0) {
orientation = UIInterfaceOrientationPortraitUpsideDown;
}
// LandscapeRight
if(t.a ==1.0 && t.b ==0 && t.c ==0 && t.d ==1.0) {
orientation = UIInterfaceOrientationLandscapeRight;
}
// LandscapeLeft
if(t.a == -1.0 && t.b ==0 && t.c ==0 && t.d == -1.0) {
orientation = UIInterfaceOrientationLandscapeLeft;
}
}
BOOL isPortrait = (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) ? YES : NO;
//视频渲染大小,这里以4:3为例
CGFloat complimentSize = videoTrack.naturalSize.height *4 /3;
CGSize videoSize;
if(isPortrait) {
videoSize = CGSizeMake(videoTrack.naturalSize.height, complimentSize);
} else {
videoSize = CGSizeMake(complimentSize, videoTrack.naturalSize.height);
}
AVMutableVideoComposition *videoComposition = [AVMutableVideoComposition videoComposition];
videoComposition.renderSize = videoSize;
videoComposition.frameDuration = CMTimeMake(1, 30);
AVMutableVideoCompositionInstruction *instruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
instruction.timeRange = CMTimeRangeMake(kCMTimeZero,asset.duration);
// rotate and position video
AVMutableVideoCompositionLayerInstruction *transformer = [AVMutableVideoCompositionLayerInstructionvideoCompositionLayerInstructionWithAssetTrack:videoTrack];
CGFloat tx = (videoTrack.naturalSize.width-complimentSize)/2;
if (orientation ==UIInterfaceOrientationPortrait || orientation ==UIInterfaceOrientationLandscapeRight) {
// invert translation
tx *= -1;
}
CGAffineTransform t1 =CGAffineTransformTranslate(videoTrack.preferredTransform, tx,0);
CGAffineTransform t2 = CGAffineTransformTranslate(t1, isPortrait?0:videoTrack.naturalSize.width, isPortrait?videoTrack.naturalSize.height:0);
CGAffineTransform t3 = CGAffineTransformScale(t2, isPortrait?1:-1, isPortrait?-1:1);
[transformer setTransform:t1 atTime:kCMTimeZero];
instruction.layerInstructions = [NSArray arrayWithObject: transformer];
videoComposition.instructions = [NSArray arrayWithObject: instruction];
// 设置要输出文件的位置
NSString* outputPath = [self.tempVideoPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.mp4",[TRCommonTool getCurrentDate]]];
NSURL *outPutUrl = [NSURL fileURLWithPath:outputPath];
if ([[NSFileManager defaultManager] fileExistsAtPath:outputPath]){
[[NSFileManager defaultManager] removeItemAtPath:outputPatherror:nil];
}
// export
AVAssetExportSession *exporter = [[AVAssetExportSessionalloc]initWithAsset:asset presetName:AVAssetExportPreset640x480];
exporter.videoComposition = videoComposition;
exporter.outputURL = outPutUrl;
exporter.outputFileType =AVFileTypeMPEG4;
exporter.shouldOptimizeForNetworkUse =true;
//开始时间,要裁剪的视频长度
CMTime beforeTime = CMTimeMakeWithSeconds(beginSecond, 30);
CMTime rangeTime = CMTimeMakeWithSeconds(timeRange, 30);
exporter.timeRange = CMTimeRangeMake(beforeTime, rangeTime);
[exporter exportAsynchronouslyWithCompletionHandler:^{
AVAssetExportSessionStatus status = exporter.status;
//注意这里一定要回到主线程,因为该任务是在子线程
dispatch_async(dispatch_get_main_queue(), ^{
switch (status) {
caseAVAssetExportSessionStatusCompleted:{
//将视频存入系统相册 [[[ALAssetsLibraryalloc]init] writeVideoAtPathToSavedPhotosAlbum:outPutUrl completionBlock:^(NSURL *assetURL,NSError *error) {
break;
}
caseAVAssetExportSessionStatusFailed:{
break;
}
default:{
HHLog(@"exporter%@",exporter.error);
break;
}
}
});
}];