场景:相册视频导出,导出出现下面错误:
Error Domain=AVFoundationErrorDomain Code=-11800 "这项操作无法完成" UserInfo={NSLocalizedFailureReason=发生未知错误(-12769), NSLocalizedDescription=这项操作无法完成, NSUnderlyingError=0x281c65ec0 {Error Domain=NSOSStatusErrorDomain Code=-12769 "(null)"}}
出现此错误,
1)、首先检查传入的输出路径是否有问题;
2)、presetName是否支持
3)、若其他配置参数都正确,还考虑是否为该视频编码有问题,可能是生成该视频时编码器出现了问题,可以通过下面方式让AVAssetExportSession重新编码来解决:
AVMutableComposition*mainComposition=[[AVMutableComposition alloc]init];
AVMutableCompositionTrack*compositionVideoTrack=[mainComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
inttimeScale=100000;
Float64 seconds=CMTimeGetSeconds([avAsset duration])-0.001;
NSUInteger videoDurationI=(NSUInteger)(seconds*timeScale);
CMTime videoDuration=CMTimeMake(videoDurationI,timeScale);
CMTimeRange videoTimeRange=CMTimeRangeMake(kCMTimeZero,videoDuration);
NSArray<AVAssetTrack*>*videoTracks=[avAsset tracksWithMediaType:AVMediaTypeVideo];
AVAssetTrack*videoTrack=[videoTracks objectAtIndex:0];
[compositionVideoTrack insertTimeRange:videoTimeRange ofTrack:videoTrack atTime:kCMTimeZero error:nil];
if ([compatiblePresets containsObject:AVAssetExportPresetMediumQuality]) {
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc]initWithAsset:mainComposition presetName:AVAssetExportPresetMediumQuality];
NSString *mp4Path = [NSString stringWithFormat:@"%@/%d%d.mp4", [self _getAudioOrVideoPath], (int)[[NSDate date] timeIntervalSince1970], arc4random() % 100000];
mp4Url = [NSURLfileURLWithPath:mp4Path];
exportSession.outputURL= mp4Url;
exportSession.shouldOptimizeForNetworkUse = YES;
exportSession.outputFileType = AVFileTypeMPEG4;
dispatch_semaphore_t wait = dispatch_semaphore_create(0l);
[exportSessionexportAsynchronouslyWithCompletionHandler:^{
switch([exportSessionstatus]) {
case AVAssetExportSessionStatusFailed: {
NSLog(@"failed, error:%@.", exportSession.error);
}break;
case AVAssetExportSessionStatusCancelled: {
NSLog(@"cancelled.");
}break;
case AVAssetExportSessionStatusCompleted: {
NSLog(@"completed.");
[selfalertUploadVideo:mp4UrlwithImage:image];
}break;
default: {
NSLog(@"others.");
}break;
}
dispatch_semaphore_signal(wait);
}];
longtimeout =dispatch_semaphore_wait(wait,DISPATCH_TIME_FOREVER);
if(timeout) {
NSLog(@"timeout.");
}
if(wait) {
//dispatch_release(wait);
wait =nil;
}
}