2012-10-27 55 views
3

我要修剪視頻的視頻:無法剪裁用AVAssetExportSession

-(void)trimVideo:(NSURL*)outputURL 
{ 
    //[[NSFileManager defaultManager] removeItemAtURL:outputURL error:nil]; 
    AVURLAsset *asset = [AVURLAsset URLAssetWithURL:outputURL options:nil]; 
    AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetLowQuality]; 

    NSString * outputFilePath = NSHomeDirectory(); 
    outputFilePath = [outputFilePath stringByAppendingPathComponent:@"Library"]; 
    outputFilePath = [outputFilePath stringByAppendingPathComponent:@"temp.mov"]; 
    NSURL * outputFileUrl = [NSURL fileURLWithPath:outputFilePath]; 

    exportSession.outputURL = outputFileUrl; 
    exportSession.shouldOptimizeForNetworkUse = YES; 
    exportSession.outputFileType = AVFileTypeMPEG4; 
    CMTime start = CMTimeMakeWithSeconds(1.0, 600); 
    CMTime duration = CMTimeMakeWithSeconds(3.0, 600); 
    CMTimeRange range = CMTimeRangeMake(start, duration); 
    exportSession.timeRange = range; 
    [exportSession exportAsynchronouslyWithCompletionHandler:^(void) 
    { 
     NSLog(@"Export Complete %d %@", exportSession.status, exportSession.error); 
     //[exportSession release]; 
    }]; 
} 

但我得到的錯誤:

Export Complete 4 Error Domain=AVFoundationErrorDomain Code=-11823 "Cannot Save" UserInfo=0x2008f420 {NSLocalizedRecoverySuggestion=Try saving again., NSLocalizedDescription=Cannot Save} 

不完全知道如何解決。

回答

13

這奏效了:

-(void)trimVideo:(NSURL*)videoToTrimURL 
{ 
    //[[NSFileManager defaultManager] removeItemAtURL:outputURL error:nil]; 
    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); 
    CMTime duration = CMTimeMakeWithSeconds(3.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

收到錯誤: - [經理writeVideoToPhotoLibrary:[NSURL fileURLWithPath:outputURL]; – Warewolf

+0

HELLO Khoool,你必須在你的課堂上創建這種方法。 (void)writeVideoToPhotoLibrary:(NSURL *)nsurlToSave { ALAssetsLibrary * library = [[ALAssetsLibrary alloc] init]; NSURL * recordedVideoURL = nsurlToSave; if([library videoAtPathIsCompatibleWithSavedPhotosAlbum:recordedVideoURL]){ ];}} } [library release] } – TurboManolo

+0

偉大的人。這工作完美。 – Kalaichelvan