http://www.raywenderlich.com/13418/how-to-play-record-edit-videos-in-ios?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed:+RayWenderlich+(Ray+Wenderlich+|+iPhone+Developer+and+Gamer)
这个编写出来的。
原文是合并视频.而我的项目要求是裁剪视频,所以我跟他的代码实现了自己的需求而已。
目前这块代码应该是能给视频里面添加logo之类的东西。但是我目前没实现,网上的代码我也暂时没找到。
- (void) loadVideoByPath:(NSString*) v_strVideoPathandSavePath:(NSString*) v_strSavePath {
NSLog(@"nv_strVideoPath = %@nv_strSavePath = %@n ",v_strVideoPath,v_strSavePath);
AVAsset*avAsset = [AVAsset assetWithURL:[NSURLfileURLWithPath:v_strVideoPath]];
CMTimeassetTime = [avAsset duration];
Float64duration = CMTimeGetSeconds(assetTime);
NSLog(@"视频时长%fn",duration);
AVMutableComposition *avMutableComposition = [AVMutableCompositioncomposition];
AVMutableCompositionTrack *avMutableCompositionTrack =[avMutableComposition addMutableTrackWithMediaType:AVMediaTypeVideopreferredTrackID:kCMPersistentTrackID_Invalid];
AVAssetTrack*avAssetTrack = [[avAsset tracksWithMediaType:AVMediaTypeVideo]objectAtIndex:0];
NSError*error = nil;
//这块是裁剪,rangtime .前面的是开始时间,后面是裁剪多长 (我这裁剪的是从第二秒开始裁剪,裁剪2.55秒时长.)
[avMutableCompositionTrackinsertTimeRange:CMTimeRangeMake(CMTimeMakeWithSeconds(2.0f, 30),CMTimeMakeWithSeconds(2.55f, 30))
ofTrack:avAssetTrack
atTime:kCMTimeZero
error:&error];
AVMutableVideoComposition *avMutableVideoComposition =[[AVMutableVideoComposition videoComposition] retain];
//这个视频大小可以由你自己设置。比如源视频640*480.而你这320*480.最后出来的是320*480这么大的,640多出来的部分就没有了。并非是把图片压缩成那么大了。
avMutableVideoComposition.renderSize = CGSizeMake(320.0f,480.0f);
avMutableVideoComposition.frameDuration = CMTimeMake(1,30);
// 这句话暂时不用理会,我正在看是否能添加logo而已。
[selfaddDataToVideoByTool:avMutableVideoComposition.animationTool];
AVMutableVideoCompositionInstruction*avMutableVideoCompositionInstruction =[AVMutableVideoCompositionInstructionvideoCompositionInstruction];
[avMutableVideoCompositionInstructionsetTimeRange:CMTimeRangeMake(kCMTimeZero, [avMutableCompositionduration])];
AVMutableVideoCompositionLayerInstruction*avMutableVideoCompositionLayerInstruction =[AVMutableVideoCompositionLayerInstructionvideoCompositionLayerInstructionWithAssetTrack:avAssetTrack];
[avMutableVideoCompositionLayerInstructionsetTransform:avAssetTrack.preferredTransformatTime:kCMTimeZero];
avMutableVideoCompositionInstruction.layerInstructions = [NSArrayarrayWithObject:avMutableVideoCompositionLayerInstruction];
avMutableVideoComposition.instructions = [NSArrayarrayWithObject:avMutableVideoCompositionInstruction];
NSFileManager *fm = [[NSFileManager alloc] init];
if ([fmfileExistsAtPath:v_strSavePath]) {
NSLog(@"video is have. then delete that");
if ([fmremoveItemAtPath:v_strSavePath error:&error]){
NSLog(@"delete is ok");
}else{
NSLog(@"delete is no error = %@",error.description);
}
}
[fmrelease];
AVAssetExportSession *avAssetExportSession = [[AVAssetExportSessionalloc] initWithAsset:avMutableCompositionpresetName:AVAssetExportPreset640x480];
[avAssetExportSessionsetVideoComposition:avMutableVideoComposition];
[avAssetExportSession setOutputURL:[NSURLfileURLWithPath:v_strSavePath]];
[avAssetExportSessionsetOutputFileType:AVFileTypeQuickTimeMovie];
[avAssetExportSession setShouldOptimizeForNetworkUse:YES];
[avAssetExportSessionexportAsynchronouslyWithCompletionHandler:^(void){
switch(avAssetExportSession.status) {
caseAVAssetExportSessionStatusFailed:
NSLog(@"exporting failed %@",[avAssetExportSession error]);
break;
caseAVAssetExportSessionStatusCompleted:
NSLog(@"exporting completed");
//想做什么事情在这个做
[selfcutImageByVideoPath:v_strSavePath];
break;
caseAVAssetExportSessionStatusCancelled:
NSLog(@"export cancelled");
break;
}
}];
if(avAssetExportSession.status !=AVAssetExportSessionStatusCompleted){
NSLog(@"Retry export");
}
[avMutableVideoComposition release];
[avAssetExportSession release];
}
![视频裁剪合并ios ios 裁剪视频 矩阵](http://img.aihuau.com/images/31101031/31084938t01676dfbdbd5bb7aa9.jpg)