AVAssetExportSession exportAsynchronouslyWithCompletionHandler returns 失败

AVAssetExportSession exportAsynchronouslyWithCompletionHandler returns failed

我正在为 trim 在线视频实施 AVAssetExportSession,但总是 returns 失败。

这是我的实现:

NSString *url = @"http://www.ebookfrenzy.com/ios_book/movie/movie.mov";
NSURL *fileURL = [NSURL URLWithString:url];
AVAsset *asset = [AVAsset assetWithURL:fileURL];
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetHighestQuality];
NSURL *exportUrl = [NSURL fileURLWithPath:[documentsDirectory stringByAppendingPathComponent:@"export.m4a"]];

exportSession.outputURL = exportUrl;
exportSession.outputFileType = AVFileTypeQuickTimeMovie;
CMTime time = CMTimeMake(1, 10);
exportSession.timeRange = CMTimeRangeMake(kCMTimeZero, time);
[exportSession exportAsynchronouslyWithCompletionHandler:^(void) {

    switch (exportSession.status)
    {
        case AVAssetExportSessionStatusCompleted:
            /*expor is completed*/
            NSLog(@"Completed!!");
            break;
            case AVAssetExportSessionStatusFailed:
            NSLog(@"failed!!");
            /*failed*/
            break;
        default:
            break;
    }
}];

你们中有人知道为什么会这样或者我做错了什么吗?

您正在尝试使用远程 URL 创建一个 AVAsset,并且您需要知道资源已加载,然后才能开始导出。

AVAsset 符合 AVAsynchronousKeyValueLoading 协议,这意味着您可以观察 tracks 键并在值更改后开始导出:

NSURL *myURL = [NSURL URLWithString:myMovieURLString];
AVAsset *asset = [AVAsset assetWithURL:myURL];

__weak typeof(self) weakSelf = self;

[asset loadValuesAsynchronouslyForKeys:@[@"tracks"] completionHandler:^{

    //Error checking here - make sure there are tracks

    [weakSelf exportAsset:asset];

}];

然后您可以在单独的方法中获取导出代码:

- (void)exportAsset:(AVAsset *)asset {

    //Your export code here
}

documentsDirectory 应该是退出路径。如果不是,exportSession.status 将等于 AVAssetExportSessionStatusFailed