iOS 5:使用AVAssetExportSession合并3个视频时出错

时间:2012-03-30 11:58:26

标签: ios ios5 avfoundation avassetexportsession

我正在尝试使用AVAssetExportSession合并(追加)3个视频,但我一直收到此错误。奇怪的是它有1或2个视频。

Error Domain=AVFoundationErrorDomain Code=-11820 "Cannot Complete Export" UserInfo=0x458120 {NSLocalizedRecoverySuggestion=Try exporting again., NSLocalizedDescription=Cannot Complete Export}

我甚至尝试重做函数以防出错,但我得到的只是无限错误信息。这是我的代码片段。

AVMutableComposition *mixComposition = [AVMutableComposition composition];
AVMutableCompositionTrack *compositionTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
NSError * error = nil;
NSMutableArray * timeRanges = [NSMutableArray arrayWithCapacity:arrayMovieUrl.count];
NSMutableArray * tracks = [NSMutableArray arrayWithCapacity:arrayMovieUrl.count];

for (int i=0; i<[arrayMovieUrl count]; i++) {
    AVURLAsset *assetClip = [arrayMovieUrl objectAtIndex:i];
    AVAssetTrack *clipVideoTrackB = [[assetClip tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];

    [timeRanges addObject:[NSValue valueWithCMTimeRange:CMTimeRangeMake(kCMTimeZero, assetClip.duration)]];
    [tracks addObject:clipVideoTrackB];
}
[compositionTrack insertTimeRanges:timeRanges ofTracks:tracks atTime:kCMTimeZero error:&error];

AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPreset1280x720];
NSParameterAssert(exporter != nil);
exporter.outputFileType = AVFileTypeQuickTimeMovie;
exporter.outputURL = outputUrl;
[exporter exportAsynchronouslyWithCompletionHandler:^{
    switch ([exporter status]) {
        case AVAssetExportSessionStatusFailed:
            NSLog(@"Export failed: %@", [exporter error]);
            break;
        case AVAssetExportSessionStatusCancelled:
            NSLog(@"Export canceled");
            break;
        case AVAssetExportSessionStatusCompleted:
            NSLog(@"Export successfully");
            break;
        default:
            break;
    }
    if (exporter.status != AVAssetExportSessionStatusCompleted){
        NSLog(@"Retry export");
        [self renderMovie];
    }
}];

我的代码有问题或iOS 5有一些错误吗?

1 个答案:

答案 0 :(得分:5)

我发现了这个问题。所以问题实际上是因为我使用AVPlayerLayer同时在预览模式下显示每个视频。参考这个问题AVPlayerItem fails with AVStatusFailed and error code "Cannot Decode",有最多4个同步AVPlayer的无证限制。这个限制在某种程度上阻碍了AVAssetExportSession在那时有4个AVPlayer实例工作。

解决方案是在导出之前释放AVPlayer,或者完全不使用AVPlayer。