iOS AVAssetExportSession 视频剪切、合并、压缩

 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 }

 

转载于:https://www.cnblogs.com/devfan/p/7238590.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值