2013-11-27 70 views

回答

9
-(void)cropVideo:(NSURL*)videoToTrimURL{ 
    AVURLAsset *asset = [AVURLAsset URLAssetWithURL:videoToTrimURL options:nil]; 
    AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetHighestQuality]; 

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *outputURL = paths[0]; 
    NSFileManager *manager = [NSFileManager defaultManager]; 
    [manager createDirectoryAtPath:outputURL withIntermediateDirectories:YES attributes:nil error:nil]; 
    outputURL = [outputURL stringByAppendingPathComponent:@"output.mp4"]; 
    // Remove Existing File 
    [manager removeItemAtPath:outputURL error:nil]; 


    exportSession.outputURL = [NSURL fileURLWithPath:outputURL]; 
    exportSession.shouldOptimizeForNetworkUse = YES; 
    exportSession.outputFileType = AVFileTypeQuickTimeMovie; 
    CMTime start = CMTimeMakeWithSeconds(1.0, 600); // you will modify time range here 
    CMTime duration = CMTimeMakeWithSeconds(15.0, 600); 
    CMTimeRange range = CMTimeRangeMake(start, duration); 
    exportSession.timeRange = range; 
    [exportSession exportAsynchronouslyWithCompletionHandler:^(void) 
    { 
     switch (exportSession.status) { 
      case AVAssetExportSessionStatusCompleted: 
       [self writeVideoToPhotoLibrary:[NSURL fileURLWithPath:outputURL]]; 
       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; 
     } 

     //[exportSession release]; 
    }]; 
} 
+0

如果我们改变时间开始和时间,有时它不修剪视频right.Can请检查这一个。 – Imran

+0

@ram我的视频持续时间是60秒我想要从15秒到30秒 – sohil

+0

CMTime start = CMTimeMakeWithSeconds(15,600); //你将修改CMTime持续时间= CMTimeMakeWithSeconds(30,600); @sohil它会为你工作 – ram

0

上面的答案在我们需要设置开始时间和结束时间来修剪时稍作改动。

我改变了这个:

CMTime start = CMTimeMakeWithSeconds(1.0, 600); // you will modify time range here 
CMTime duration = CMTimeMakeWithSeconds(15.0, 600); 
CMTimeRange range = CMTimeRangeMake(start, duration); 

要这样:

CMTime start = CMTimeMakeWithSeconds(self.StartTime, 600); // you will modify time range here 
    CMTime duration = CMTimeSubtract(CMTimeMakeWithSeconds(self.EndTime, 600), start); 
    CMTimeRange range = CMTimeRangeMake(start, duration); 

它为我工作。

+0

我的视频时长是60秒,我想从15秒删除到30秒 – sohil

+0

CMTime start = CMTimeMakeWithSeconds(15,600); //你将修改 CMTime duration = CMTimeMakeWithSeconds(30,600); @sohil它会为你工作 – ram