视频 裁剪 合并 ios

35 篇文章 0 订阅
声明:此文我是根据
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_strVideoPath andSavePath:(NSString*) v_strSavePath {
     NSLog(@"\nv_strVideoPath = %@ \nv_strSavePath = %@\n ",v_strVideoPath,v_strSavePath);
       AVAsset *avAsset = [AVAsset assetWithURL:[NSURL fileURLWithPath:v_strVideoPath]];
       CMTime assetTime = [avAsset duration];
       Float64 duration = CMTimeGetSeconds(assetTime);
       NSLog(@"视频时长 %f\n",duration);
     
       AVMutableComposition *avMutableComposition = [AVMutableComposition composition];
     
       AVMutableCompositionTrac k *avMutableCompositionTrac k = [avMutableComposition addMutableTrackWithMedia Type:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
     
       AVAssetTrack *avAssetTrack = [[avAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
     
       NSError *error = nil;
       // 这块是裁剪,rangtime .前面的是开始时间,后面是裁剪多长 (我这裁剪的是从第二秒开始裁剪,裁剪2.55秒时长.)
       [avMutableCompositionTrac k insertTimeRange:CMTimeRangeMake(CMTimeMakeWithSeconds(2.0f, 30), CMTimeMakeWithSeconds(2.55f, 30))
                                                                    ofTrack:avAssetTrack
                                                                      atTime:kCMTimeZero
                                                                        error:&error];
     
       AVMutableVideoCompositio n *avMutableVideoCompositio n = [[AVMutableVideoCompositio n videoComposition] retain];
// 这个视频大小可以由你自己设置。比如源视频640*480.而你这320*480.最后出来的是320*480这么大的,640多出来的部分就没有了。并非是把图片压缩成那么大了。
       avMutableVideoCompositio n.renderSize = CGSizeMake(320.0f, 480.0f);
       avMutableVideoCompositio n.frameDuration = CMTimeMake(1, 30); 
// 这句话暂时不用理会,我正在看是否能添加logo而已。 
       [self addDataToVideoByTool:avMutableVideoCompositio n.animationTool];
     
       AVMutableVideoCompositio nInstruction *avMutableVideoCompositio nInstruction = [AVMutableVideoCompositio nInstruction videoCompositionInstruct ion];
     
       [avMutableVideoCompositio nInstruction setTimeRange:CMTimeRangeMake(kCMTimeZero, [avMutableComposition duration])];
     
       AVMutableVideoCompositio nLayerInstruction *avMutableVideoCompositio nLayerInstruction = [AVMutableVideoCompositio nLayerInstruction videoCompositionLayerIns tructionWithAssetTrack:avAssetTrack];
       [avMutableVideoCompositio nLayerInstruction setTransform:avAssetTrack.preferredTransform atTime:kCMTimeZero];

       avMutableVideoCompositio nInstruction.layerInstructions = [NSArray arrayWithObject:avMutableVideoCompositio nLayerInstruction];
            

       avMutableVideoCompositio n.instructions = [NSArray arrayWithObject:avMutableVideoCompositio nInstruction];

     
       NSFileManager *fm = [[NSFileManager alloc] init];
       if ([fm fileExistsAtPath:v_strSavePath]) {
              NSLog(@"video is have. then delete that");
              if ([fm removeItemAtPath:v_strSavePath error:&error]) {
                     NSLog(@"delete is ok");
              }else {
                     NSLog(@"delete is no error = %@",error.description);
              }
       }
       [fm release];
     
       AVAssetExportSession *avAssetExportSession = [[AVAssetExportSession alloc] initWithAsset:avMutableComposition presetName:AVAssetExportPreset640x4 80];
       [avAssetExportSession setVideoComposition:avMutableVideoCompositio n];
       [avAssetExportSession setOutputURL:[NSURL fileURLWithPath:v_strSavePath]];
       [avAssetExportSession setOutputFileType:AVFileTypeQuickTimeMovie ];
       [avAssetExportSession setShouldOptimizeForNetw orkUse:YES];
       [avAssetExportSession exportAsynchronouslyWith CompletionHandler:^(void){
              switch (avAssetExportSession.status) {
                     case AVAssetExportSessionStat usFailed:
                            NSLog(@"exporting failed %@",[avAssetExportSession error]);
                            break;
                     case AVAssetExportSessionStat usCompleted:
                            NSLog(@"exporting completed");
                            // 想做什么事情在这个做
                            [self cutImageByVideoPath:v_strSavePath];
                            break;
                     case AVAssetExportSessionStat usCancelled:
                            NSLog(@"export cancelled");
                            break;
              }
       }];
       if (avAssetExportSession.status != AVAssetExportSessionStat usCompleted){
              NSLog(@"Retry export");
       }
       [avMutableVideoCompositio n release];
         [avAssetExportSession release];
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值