2013-11-27 1 views
5

ビデオファイルをトリミングしたい。ギャラリーからビデオを選んで15秒間のビデオに変換したいだけです。 pickerviewcontrollerで通常のトリミングを使用すると、時間が指定されず、フレームが表示されますが、15秒間固定する必要があります。どうすればこれを達成できますか?iOS SDKで動画ファイルをトリミングして15秒間の動画に変換するにはどうすればよいですか?

答えて

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

私たちは時間の開始と継続時間を変えて、時にはビデオをトリミングしないことがあります。これを確認してください。 – Imran

+0

@ram動画の再生時間は60秒です。15から30秒の間に削除したいです。 – sohil

+0

CMTime start = CMTimeMakeWithSeconds(15,600); // CMTime duration = CMTimeMakeWithSeconds(30,600)を変更します。 @sohilそれはあなたのために動作します – ram

0

上記の答えは、開始時刻と終了時刻の両方を調整する必要がある場合に少し変更して私の仕事をしてくれました。それは私のために働いた

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); 

:これに

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

は、私はこれを変更しました。

+0

私のビデオ時間は60秒です。私は15秒から30秒に削除したいです。 – sohil

+0

CMTime start = CMTimeMakeWithSeconds(15,600); //あなたは変更されます CMTime duration = CMTimeMakeWithSeconds(30,600);あなたのためにうまくいく – ram

関連する問題