AVAssetExportSession exportAsynchronouslyWithCompletionHandler returns failed

末鹿安然 提交于 2019-12-13 02:02:39

问题


I'm implementing AVAssetExportSession to trim a video online but always returns failed.

Here is my implementation:

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

Any of you knows why this happening or what I'm doing wrong?


回答1:


You are attempting to create an AVAsset with a remote URL and you need to know that the asset has loaded before you can begin your export.

AVAsset conforms to the AVAsynchronousKeyValueLoading protocol, which means you can observe the tracks key and start your export once the value changes:

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

}];

Then you can have your export code in a separate method:

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

    //Your export code here
}



回答2:


documentsDirectory should be an is exiting path . if it doesn't , exportSession.status will equals to AVAssetExportSessionStatusFailed



来源:https://stackoverflow.com/questions/32930885/avassetexportsession-exportasynchronouslywithcompletionhandler-returns-failed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!