IOS 5: error merging 3 videos with AVAssetExportSession

I am trying to combine (add) 3 videos using AVAssetExportSession, but I keep getting this error. It is unlikely that it worked for 1 or 2 videos.

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

I even tried to repeat the function in case of an error, but what I got is just an endless error message. This is a piece of 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]; } }]; 

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

+7
source share
1 answer

I found a problem. So the problem was that I use AVPlayerLayer to simultaneously display each video in preview mode. Referring to this question AVPlayerItem crashes with AVStatusFailed and error code "Can not Decode" , there is an undocumented limit of a maximum of 4 simultaneous AVPlayer. And this limit somehow prevents AVAssetExportSession from working when 4 instances of AVPlayer are present at that moment.

The solution is to free AVPlayer before exporting or not to use AVPlayer at all.

+5
source

All Articles