iOS - AVAssetExportSession 未知错误 -12769

标签 ios objective-c video avfoundation avassetexportsession

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:

这是视频:

Dropbox 链接:https://www.dropbox.com/s/twgah26gqgsv9y9/localStoreTempVideoPath.mp4?dl=0

或 WeTransfer 链接: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;
            }
        });
    }];
}

最佳答案

我做了一个实验,得出了这个结论。如果您从 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);

关于iOS - AVAssetExportSession 未知错误 -12769,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44410305/

相关文章:

ios - 使用 AVFoundation 类播放音频文件的引用部分

ios - 无法将带有撇号的字符串插入 sqlite ios

objective-c - ios - 核心数据更新记录

android - 更改视频聊天帧率 tokbox

php - 使用 php 在 PDF 中嵌入视频/flash

multithreading - 在后台和主线程 ios 中执行

ios - 如何在 IOS 上使用 Phonegap 读取具有群组名称的联系人

iphone - 按下取消时显示警报 View 并保持在同一 View Controller 上

video - 将 .mp4 文件流式传输到 http 服务器

ios - NSAttributedString 不工作 - 为分发编译时不出现