1 - (void) loadVideoByPath:(NSString*) v_strVideoPath andSavePath:(NSString*) v_strSavePath { 2 3 NSLog(@"\nv_strVideoPath = %@ \nv_strSavePath = %@\n ",v_strVideoPath,v_strSavePath); 4 AVAsset *avAsset = [AVAsset assetWithURL:[NSURL fileURLWithPath:v_strVideoPath]]; 5 CMTime assetTime = [avAsset duration]; 6 Float64 duration = CMTimeGetSeconds(assetTime); 7 NSLog(@"视频时长 %f\n",duration); 8 9 AVMutableComposition *avMutableComposition = [AVMutableComposition composition];10 11 AVMutableCompositionTrack *avMutableCompositionTrack = [avMutableComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];12 13 AVAssetTrack *avAssetTrack = [[avAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];14 15 NSError *error = nil;16 // 这块是裁剪,rangtime .前面的是开始时间,后面是裁剪多长 (我这裁剪的是从第二秒开始裁剪,裁剪2.55秒时长.)17 [avMutableCompositionTrack insertTimeRange:CMTimeRangeMake(CMTimeMakeWithSeconds(2.0f, 30), CMTimeMakeWithSeconds(2.55f, 30))18 ofTrack:avAssetTrack19 atTime:kCMTimeZero20 error:&error];21 22 AVMutableVideoComposition *avMutableVideoComposition = [[AVMutableVideoComposition videoComposition] retain];23 // 这个视频大小可以由你自己设置。比如源视频640*480.而你这320*480.最后出来的是320*480这么大的,640多出来的部分就没有了。并非是把图片压缩成那么大了。24 avMutableVideoComposition.renderSize = CGSizeMake(320.0f, 480.0f);25 avMutableVideoComposition.frameDuration = CMTimeMake(1, 30); 26 // 这句话暂时不用理会,我正在看是否能添加logo而已。 27 [self addDataToVideoByTool:avMutableVideoComposition.animationTool];28 29 AVMutableVideoCompositionInstruction *avMutableVideoCompositionInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];30 31 [avMutableVideoCompositionInstruction setTimeRange:CMTimeRangeMake(kCMTimeZero, [avMutableComposition duration])];32 33 AVMutableVideoCompositionLayerInstruction *avMutableVideoCompositionLayerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:avAssetTrack];34 [avMutableVideoCompositionLayerInstruction setTransform:avAssetTrack.preferredTransform atTime:kCMTimeZero];35 36 avMutableVideoCompositionInstruction.layerInstructions = [NSArray arrayWithObject:avMutableVideoCompositionLayerInstruction];37 38 39 avMutableVideoComposition.instructions = [NSArray arrayWithObject:avMutableVideoCompositionInstruction];40 41 42 NSFileManager *fm = [[NSFileManager alloc] init];43 if ([fm fileExistsAtPath:v_strSavePath]) {44 NSLog(@"video is have. then delete that");45 if ([fm removeItemAtPath:v_strSavePath error:&error]) {46 NSLog(@"delete is ok");47 }else {48 NSLog(@"delete is no error = %@",error.description);49 }50 }51 [fm release];52 53 AVAssetExportSession *avAssetExportSession = [[AVAssetExportSession alloc] initWithAsset:avMutableComposition presetName:AVAssetExportPreset640x480];54 [avAssetExportSession setVideoComposition:avMutableVideoComposition];55 [avAssetExportSession setOutputURL:[NSURL fileURLWithPath:v_strSavePath]];56 [avAssetExportSession setOutputFileType:AVFileTypeQuickTimeMovie];57 [avAssetExportSession setShouldOptimizeForNetworkUse:YES];58 [avAssetExportSession exportAsynchronouslyWithCompletionHandler:^(void){59 switch (avAssetExportSession.status) {60 case AVAssetExportSessionStatusFailed:61 NSLog(@"exporting failed %@",[avAssetExportSession error]);62 break;63 case AVAssetExportSessionStatusCompleted:64 NSLog(@"exporting completed");65 // 想做什么事情在这个做66 [self cutImageByVideoPath:v_strSavePath];67 break;68 case AVAssetExportSessionStatusCancelled:69 NSLog(@"export cancelled");70 break;71 }72 }];73 if (avAssetExportSession.status != AVAssetExportSessionStatusCompleted){74 NSLog(@"Retry export");75 }76 [avMutableVideoComposition release];77 [avAssetExportSession release];78 }