AVErrorInvalidVideoComposition = -11841

Estoy fusionando varios videos y varias canciones y estoy obteniendo lo que está mal en el código porque el mismo código se ejecutaba absolutamente bien ayer pero hoy AVAssetExportSessionStatus = 4,error = Error Domain=AVFoundationErrorDomain Code=-11841 "The operation couldn't be completed. (AVFoundationErrorDomain error -11841.)" la siguiente respuesta AVAssetExportSessionStatus = 4,error = Error Domain=AVFoundationErrorDomain Code=-11841 "The operation couldn't be completed. (AVFoundationErrorDomain error -11841.)" Hice un poco de investigación y encontré que la export está recibiendo un AVAssetExportSessionStatus = 4,error = Error Domain=AVFoundationErrorDomain Code=-11841 "The operation couldn't be completed. (AVFoundationErrorDomain error -11841.)" debido a la composition de video inválida AVAssetExportSessionStatus = 4,error = Error Domain=AVFoundationErrorDomain Code=-11841 "The operation couldn't be completed. (AVFoundationErrorDomain error -11841.)" averigüe qué está mal con la composition de video.

 - (void)mergeAllselectedVideos { NSArray *pathArray = [DocumentDirectory getUrlFromDocumentDirectoryOfList:self.selectedClipsArray]; AVMutableComposition *mixComposition = [[AVMutableComposition alloc]init]; NSMutableArray *layerinstructions = [[NSMutableArray alloc]init]; CMTime time = kCMTimeZero; CMTime previousSongDuration = kCMTimeZero; for (int i = 0 ; i < pathArray.count; i++) { //VIDEO TRACK// time = CMTimeAdd(time, previousSongDuration); NSURL *url = [NSURL URLWithString:[pathArray objectAtIndex:i]]; AVAsset *avAsset = [AVAsset assetWithURL:url]; AVMutableCompositionTrack *track = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo prefernetworkingTrackID:kCMPersistentTrackID_Invalid]; [track insertTimeRange:CMTimeRangeMake(kCMTimeZero, avAsset.duration) ofTrack:[[avAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:time error:nil]; previousSongDuration = avAsset.duration; } CMTime audioTime = kCMTimeZero; for (int i = 0; i < self.selectedSongsArray.count; i++) { MPMediaItem * songItem = [self.selectedSongsArray objectAtIndex:i]; NSURL *songURL = [songItem valueForProperty: MPMediaItemPropertyAssetURL]; AVAsset *audioAsset = [AVAsset assetWithURL:songURL]; AVMutableCompositionTrack *AudioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio prefernetworkingTrackID:kCMPersistentTrackID_Invalid]; CMTimeRange timeRange = CMTimeRangeMake(audioTime, audioAsset.duration); if(CMTimeGetSeconds(CMTimeAdd(audioTime, audioAsset.duration)) > CMTimeGetSeconds(time)) { timeRange = CMTimeRangeMake(audioTime, CMTimeSubtract(time,audioTime)); } [AudioTrack insertTimeRange:timeRange ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:kCMTimeZero error:nil]; audioTime = CMTimeAdd(audioTime, audioAsset.duration); } AVMutableVideoCompositionInstruction * MainInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction]; MainInstruction.timeRange = CMTimeRangeMake(kCMTimeZero, time); MainInstruction.layerInstructions = layerinstructions; AVMutableVideoComposition *MainCompositionInst = [AVMutableVideoComposition videoComposition]; MainCompositionInst.instructions = [NSArray arrayWithObject:MainInstruction]; MainCompositionInst.frameDuration = CMTimeMake(1, 30); MainCompositionInst.renderSize = CGSizeMake(320.0, 480.0); NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; movieName = [CoreDataFunctions getNameForMovieForDate:[CalendarFunctions getCurrentDateString]]; self.moviePlayButton.titleLabel.text = movieName; NSString *myPathDocs = [documentsDirectory stringByAppendingPathComponent:movieName]; NSURL *url = [NSURL fileURLWithPath:myPathDocs]; AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetHighestQuality]; exporter.outputURL=url; exporter.outputFileType = AVFileTypeQuickTimeMovie; exporter.videoComposition = MainCompositionInst; exporter.shouldOptimizeForNetworkUse = YES; [exporter exportAsynchronouslyWithCompletionHandler:^{dispatch_async(dispatch_get_main_queue(), ^{[self exportDidFinish:exporter];});}]; } - (void)exportDidFinish:(AVAssetExportSession*)session { //Printing error NSLog(@"AVAssetExportSessionStatus = %i,error = %@",session.status,session.error); } 

Encontré su pregunta mientras tenía el mismo problema. Mi teoría sobre este problema es que todas las properties para la composition del video no se configuran en el momento de la export, por lo que está terminando. Aquí está la estrofa que ahora estoy usando, que ahora resulta en una export sin errores:

 AVMutableVideoComposition *videoComposition = [AVMutableVideoComposition videoComposition]; videoComposition.frameDuration = CMTimeMake(1,30); videoComposition.renderScale = 1.0; videoComposition.renderSize = CGSizeMake(352.0, 288.0); instruction.layerInstructions = [NSArray arrayWithObject: layerInstruction]; instruction.timeRange = CMTimeRangeMake(kCMTimeZero, videoAsset.duration); videoComposition.instructions = [NSArray arrayWithObject: instruction]; 

En mi caso, me faltaba la propiedad timeRange en las instrucciones. Compruebe sus propias properties para asegurarse de que obtienen los valores correctos. ¡Buena suerte! Esto es difícil.

LayerInstruction establecer la opacity para la primera LayerInstruction , por ejemplo:

 [FirstlayerInstruction setOpacity:0.0 atTime:firstAsset.duration];