我正在实现AVAssetExportSession来在线修剪视频,但总是返回失败。
这是我的实现:
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
}