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:avAssetTrack 19 atTime:kCMTimeZero 20 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 }