iOS - AVAssetExportSession 未知错误 -12769

iOS - AVAssetExportSession unknown error -12769

AVFoundation 似乎无法接受我的其中一个视频。我真的不知道为什么。它适用于其他视频,但不适用于此视频。

我什至没有修改视频,我只是用视频轨道做一个合成,然后用预设导出它"AVAssetExportPresetHighestQuality"。

我收到这个错误:

Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed" UserInfo={NSUnderlyingError=0x60000045a8e0 {Error Domain=NSOSStatusErrorDomain Code=-12769 "(null)"}, NSLocalizedFailureReason=An unknown error occurred (-12769), NSLocalizedDescription=The operation could not be completed}

你知道我的代码是否有问题,或者视频是否不受 AVFoundation 支持?

这是 Github 上的项目(它只是将视频导出到相机胶卷):

https://github.com/moonshaped/ExportSessionCrash

或者如果您不想使用 Github :

这是视频:

投递箱 link : https://www.dropbox.com/s/twgah26gqgsv9y9/localStoreTempVideoPath.mp4?dl=0

或WeTransfer link : https://wetransfer.com/downloads/8f8ab257068461a2c9a051542610725220170606122640/8d934c

这是代码:

- (void)exportVideo:(AVAsset *)videoAsset
      videoDuration:(Float64)videoAssetDuration
                 to:(NSString *)resultPath{

    [Utilities deleteFileIfExists:resultPath];

    AVMutableComposition *mainComposition = [[AVMutableComposition alloc] init];
    AVMutableCompositionTrack *compositionVideoTrack = [mainComposition addMutableTrackWithMediaType:AVMediaTypeVideo
                                                                                    preferredTrackID:kCMPersistentTrackID_Invalid];

    int timeScale = 100000;
    int videoDurationI = (int) (videoAssetDuration * (float) timeScale);
    CMTime videoDuration = CMTimeMake(videoDurationI, timeScale);
    CMTimeRange videoTimeRange = CMTimeRangeMake(kCMTimeZero, videoDuration);

    NSArray<AVAssetTrack *> *videoTracks = [videoAsset tracksWithMediaType:AVMediaTypeVideo];
    AVAssetTrack *videoTrack = [videoTracks objectAtIndex:0];

    [compositionVideoTrack insertTimeRange:videoTimeRange
                                   ofTrack:videoTrack
                                    atTime:kCMTimeZero
                                     error:nil];

    NSURL *outptVideoUrl = [NSURL fileURLWithPath:resultPath];
    self.exporter = [[AVAssetExportSession alloc] initWithAsset:mainComposition
                                                     presetName:AVAssetExportPresetHighestQuality];

    self.exporter.outputURL = outptVideoUrl;
    self.exporter.outputFileType = AVFileTypeMPEG4;
    self.exporter.shouldOptimizeForNetworkUse = YES;

    [self.exporter exportAsynchronouslyWithCompletionHandler:^{
        dispatch_async(dispatch_get_main_queue(), ^{
            switch (self.exporter.status) {
                case AVAssetExportSessionStatusFailed:{
                    @throw [NSException exceptionWithName:@"failed export"
                                                   reason:[self.exporter.error description]
                                                 userInfo:nil];
                }
                case AVAssetExportSessionStatusCancelled:
                    @throw [NSException exceptionWithName:@"cancelled export"
                                                   reason:@"Export cancelled"
                                                 userInfo:nil];

                case AVAssetExportSessionStatusCompleted: {
                    NSLog(@"Export finished");
                }
                    break;

                default:
                    break;
            }
        });
    }];
}

您尝试测试的设备无法对其进行解码。请在一些较新的设备上尝试,例如iPhone 6. 我在 iPad 模拟器 iOS10.3 上测试了你的媒体,它在那里工作正常,所以它一定与编码有关。

我做了一个实验,得出了这个结论。如果您从 videoTimeRange 减少 1 毫秒或更多毫秒,那么它将起作用。尝试替换以下代码块:

int timeScale = 100000;
Float64 seconds = CMTimeGetSeconds([videoAsset duration]) - 0.001;
NSUInteger videoDurationI = (NSUInteger) (seconds * (float) timeScale);
CMTime videoDuration = CMTimeMake(videoDurationI, timeScale);
CMTimeRange videoTimeRange = CMTimeRangeMake(kCMTimeZero, videoDuration);