0

我正在开发视频应用程序。我必须捕获和修剪视频。我已经使用 AVFoundation 框架完成了这项工作。当我调用修剪方法时,我收到错误“在此服务器上找不到请求的 URL”。

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

- (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:”案例并显示上述错误。我没有找到我出错的地方。请建议我现在该怎么做。

4

1 回答 1

0

您的问题是,您首先删除文件并尝试将其与导出会话一起使用。导出过程中请勿删除文件,建议将导出的文件保存到临时目录,导出完成后,删除旧文件并将导出的文件移至原始文件 URL。祝你好运!

于 2013-06-28T05:50:20.780 回答