使用AVAssetExportSession无法裁剪视频

5

我想剪辑一个视频:

-(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}

不太确定如何解决。

1个回答

18

这个方法很管用:

-(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];
     }];
}

出现错误:-- [manager writeVideoToPhotoLibrary:[NSURL fileURLWithPath:outputURL]]; - Prince Kumar Sharma
你好Khoool,你需要在你的类中创建这个方法。 -(void)writeVideoToPhotoLibrary:(NSURL *)nsurlToSave{ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; NSURL *recordedVideoURL= nsurlToSave; if ([library videoAtPathIsCompatibleWithSavedPhotosAlbum:recordedVideoURL]) { [library writeVideoAtPathToSavedPhotosAlbum:recordedVideoURL completionBlock:^(NSURL *assetURL, NSError *error){} ]; } [library release] } - TurboManolo
终于过了几个小时!你解决了这个问题。fileURLWithPath 真是太神奇了。 - jgvb
9
问题在于尝试向一个已经存在的文件中写入内容。使用removeItemAtPath在导出之前删除了该文件,从而解决了问题。 - David Pettigrew
@DavidPettigrew 感谢您,David!对于2018年使用Swift 4的任何人来说,代码如下:FileManager.default.removeItem(atPath: exportPath) - AdjunctProfessorFalcon
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接