如何克服AVfoundation中的错误“在此服务器上找不到请求的URL”

时间:2013-06-28 05:34:54

标签: iphone ios ipad avfoundation avassetexportsession

我正在处理视频应用程序。我必须捕获并修剪视频。我使用AVFoundation框架完成了这项工作。当我调用trim方法时,我收到错误“在此服务器上找不到请求的URL”。< / p>

我使用以下代码修剪和播放视频

- (IBAction)showTrimmedVideo:(UIButton *)sender
{

[self deleteTmpFile];

   NSURL *videoFileUrl = [NSURL fileURLWithPath:originalVideoPath];
NSLog(@"Video to trim is %@",videoFileUrl);

AVAsset *anAsset = [[AVURLAsset alloc] initWithURL:videoFileUrl options:nil];
NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:anAsset];
if ([compatiblePresets containsObject:AVAssetExportPresetMediumQuality])
{

    self.exportSession = [[AVAssetExportSession alloc]
                          initWithAsset:anAsset presetName:AVAssetExportPresetPassthrough];
    // Implementation continues.

           NSURL *furl = [NSURL fileURLWithPath:originalVideoPath];
    NSLog(@"Original file path is %@",furl);

    self.exportSession.outputURL = furl;
    self.exportSession.outputFileType = AVFileTypeQuickTimeMovie;

    CMTime start = CMTimeMakeWithSeconds(self.startTime, anAsset.duration.timescale);
    CMTime duration = CMTimeMakeWithSeconds(self.stopTime-self.startTime, anAsset.duration.timescale);
    CMTimeRange range = CMTimeRangeMake(start, duration);
    self.exportSession.timeRange = range;

    self.trimBtn.hidden = YES;
    self.myActivityIndicator.hidden = NO;
    [self.myActivityIndicator startAnimating];
    [self.exportSession exportAsynchronouslyWithCompletionHandler:^{

        switch ([self.exportSession status])
        {
            case AVAssetExportSessionStatusFailed:
                NSLog(@"Export failed: %@", [[self.exportSession error] localizedDescription]);
                break;
            case AVAssetExportSessionStatusCancelled:
                NSLog(@"Export canceled");
                break;
            default:
                NSLog(@"NONE");
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self.myActivityIndicator stopAnimating];
                    self.myActivityIndicator.hidden = YES;
                    self.trimBtn.hidden = NO;
                    [self playMovie:self.tmpVideoPath];
                });

                break;
        }
    }];

}

}

 -(void)deleteTmpFile
{

NSURL *url = [NSURL fileURLWithPath:originalVideoPath];
NSFileManager *fm = [NSFileManager defaultManager];
BOOL exist = [fm fileExistsAtPath:url.path];
NSError *err;
if (exist) {
    [fm removeItemAtURL:url error:&err];
    NSLog(@"file deleted");
    if (err)
    {
        NSLog(@"file remove error, %@", err.localizedDescription );
    }
} else {
    NSLog(@"no file by that name");
}

}

每次进入“AVAssetExportSessionStatusFailed:”的情况并显示上面的错误。 我没有得到我出错的地方。请告诉我现在要做什么。

1 个答案:

答案 0 :(得分:0)

你的问题是,你首先删除文件并尝试将其用于导出会话。您不应在导出过程中删​​除该文件,我建议您将导出的文件保存到临时目录,导出完成后,只需删除旧文件并将导出的文件移动到原始文件URL。祝你好运!