iOS 5:使用AVAssetExportSession合并3个视频时出错 [英] iOS 5: Error merging 3 videos with AVAssetExportSession

查看:281
本文介绍了iOS 5:使用AVAssetExportSession合并3个视频时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to merge (append) 3 videos using AVAssetExportSession, but I keep getting this error. Weirdly for 1 or 2 videos it worked.

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

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

I even tried to redo the function in case of error but what I got is only infinite error message. This is the snippet of my code.

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有一些错误吗?

Is there something wrong with my code or iOS 5 has some bug?

推荐答案

我发现了问题。所以问题实际上是因为我使用AVPlayerLayer同时以预览模式显示每个视频。参照该问题 AVPlayerItem失败,AVStatusFailed和错误代码"不能解码,有最大4个同步AVPlayer的无证限制。而这种限制某种方式从时,有在那一刻,4 AVPlayer例如工作阻碍AVAssetExportSession。

I've found the problem. So the problem was actually because I use AVPlayerLayer to display each video in preview mode simultaneously. Referring to this question AVPlayerItem fails with AVStatusFailed and error code "Cannot Decode" , there's undocumented limit of maximum 4 simultaneous AVPlayer to work. And this limit somehow hinders AVAssetExportSession from working when there's 4 AVPlayer instance at that moment.

解决方案是出口,还是不能完全使用AVPlayer之前释放的AVPlayer。

The solution is to release the AVPlayer before exporting, or not using AVPlayer altogether.

这篇关于iOS 5:使用AVAssetExportSession合并3个视频时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆