在iOS中拆分视频

时间:2013-09-20 07:48:38

标签: iphone ios objective-c video avfoundation

我必须将视频分成两个部分。我两次使用这个修剪功能是一个很好的方法我已经阅读了AVFoundation框架工作,但没有找到任何直接解决方案来分割视频。但有一些iPhone应用程序非常流畅地分割视频。他们使用修剪功能吗?需要建议

-(void)splitSecondVideo{
    NSString *deleteVideo = [_videoPath path];

    AVURLAsset *asset = [AVURLAsset URLAssetWithURL:_videoPath options:nil];

    AVAssetExportSession *exportSession = [[AVAssetExportSession alloc]initWithAsset:asset presetName:AVAssetExportPresetLowQuality];
     NSString *outputURL = nil;
     NSString *videoFileName = nil;
     videoFileName = [SPUtility getDefaultVideoName];
     outputURL = [NSString stringWithFormat:@"%@/%@.mov", _clipFolderPath, videoFileName];

    NSFileManager *manager = [NSFileManager defaultManager];
    [manager createDirectoryAtPath:outputURL withIntermediateDirectories:YES attributes:nil error:nil];
    [manager removeItemAtPath:outputURL error:nil] ;
     exportSession.outputURL = [NSURL fileURLWithPath:outputURL];
     exportSession.shouldOptimizeForNetworkUse = YES;
     exportSession.outputFileType = AVFileTypeQuickTimeMovie;
     // Trim to half duration
     double halfDuration = CMTimeGetSeconds([asset duration])/2.0;
     double fullDuration = CMTimeGetSeconds([asset duration]);

     CMTime firsthalfDuration = CMTimeMakeWithSeconds(halfDuration, 1);
     CMTime secondhalfDuration = CMTimeMakeWithSeconds(fullDuration, 1);

     CMTimeRange secondrange = CMTimeRangeMake(firsthalfDuration, secondhalfDuration);

     exportSession.timeRange = secondrange;

     [exportSession exportAsynchronouslyWithCompletionHandler:^(void)
     {
     switch (exportSession.status) {
     case AVAssetExportSessionStatusCompleted:
     [manager removeItemAtPath:deleteVideo error:nil];
     NSLog(@"Export Complete %d %@", exportSession.status, exportSession.error);
     break;
     case AVAssetExportSessionStatusFailed:
     NSLog(@"Failed:%@",exportSession.error);
     break;
     case AVAssetExportSessionStatusCancelled:
     NSLog(@"Canceled:%@",exportSession.error);
     break;
     default:
     break;
     }

     }];


}

0 个答案:

没有答案