1
votes

Getting this error when exporting file

Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed" UserInfo={NSUnderlyingError=0x608000642310 {Error Domain=NSOSStatusErrorDomain Code=-12120 "(null)"}

AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL:url options:nil];

AVMutableComposition* miComposition = [AVMutableComposition composition];

AVMutableCompositionTrack *compositionVideoTrack = [miComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
AVAssetTrack *clipVideoTrack = [videoAsset tracksWithMediaType:AVMediaTypeVideo].firstObject;
AVMutableCompositionTrack *compositionAudioTrack = [miComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
AVAssetTrack *clipAudioTrack = [videoAsset tracksWithMediaType:AVMediaTypeAudio].firstObject;
//If you need audio as well add the Asset Track for audio here

[compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, [videoAsset duration]) ofTrack:clipVideoTrack atTime:kCMTimeZero error:nil];
[compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, [videoAsset duration]) ofTrack:clipAudioTrack atTime:kCMTimeZero error:nil];

[compositionVideoTrack setPreferredTransform:[[videoAsset tracksWithMediaType:AVMediaTypeVideo].firstObject preferredTransform]];



CGSize sizeOfVideo=[clipVideoTrack naturalSize];



//Image of watermark
UIImage *myImage=[UIImage imageNamed:@"_minutestory.png"];
CALayer *layerCa = [CALayer layer];
layerCa.contents = (id)myImage.CGImage;
layerCa.frame = CGRectMake((sizeOfVideo.width - 100),20 , 100, 100);
layerCa.opacity = 0.6;

CALayer *optionalLayer=[CALayer layer];

optionalLayer.frame=CGRectMake(0, 0, sizeOfVideo.width, sizeOfVideo.height);
[optionalLayer setMasksToBounds:YES];

CALayer *parentLayer=[CALayer layer];
CALayer *videoLayer=[CALayer layer];
parentLayer.frame=CGRectMake(0, 0, sizeOfVideo.width, sizeOfVideo.height);
videoLayer.frame=CGRectMake(0, 0, sizeOfVideo.width, sizeOfVideo.height);
[parentLayer addSublayer:videoLayer];
[parentLayer addSublayer:optionalLayer];
[parentLayer addSublayer:layerCa];

AVMutableVideoComposition *videoComposition=[AVMutableVideoComposition videoComposition] ;
videoComposition.frameDuration=CMTimeMake(1, 30);
videoComposition.renderSize=sizeOfVideo;
videoComposition.animationTool=[AVVideoCompositionCoreAnimationTool videoCompositionCoreAnimationToolWithPostProcessingAsVideoLayer:videoLayer inLayer:parentLayer];

AVMutableVideoCompositionInstruction *instruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
instruction.timeRange = CMTimeRangeMake(kCMTimeZero, [miComposition duration]);
AVAssetTrack *vidTrack = [[miComposition tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
AVMutableVideoCompositionLayerInstruction* layerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:vidTrack];
instruction.layerInstructions = [NSArray arrayWithObject:layerInstruction];
videoComposition.instructions = [NSArray arrayWithObject: instruction];

[[NSFileManager defaultManager] removeItemAtPath:url error:NULL];

exporter2 =
[[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetHighestQuality];
exporter2.videoComposition = videoComposition;

exporter2.outputURL=url;
exporter2.outputFileType = AVFileTypeMPEG4;
exporter2.shouldOptimizeForNetworkUse = YES;




exportProgressBarTimer = [NSTimer scheduledTimerWithTimeInterval:.1 target:self selector:@selector(updateExportDisplay) userInfo:nil repeats:YES];

[exporter2 exportAsynchronouslyWithCompletionHandler:^{
    dispatch_async(dispatch_get_main_queue(), ^{
        [self exportFinish:exporter2];


    });
}];
1
Welcome to Stack Overflow! To give you a great answer, it might help us if you have a glance at How to Ask if you haven't already. It might be also useful if you could provide a minimal reproducible example.Mat
This error isn't really very descriptive, however what it means is that you're giving the composition an instruction that it doesn't know how to complete. Try narrowing down where the error occurs by commenting out various parts of the code till you get a working video, then adding them back in. There will be something you're doing that doesn't make "sense", typically in the layer instructions - you'll have an incorrect frame, bad starting time, overlapping instructions, something like that.Tim Bull

1 Answers

0
votes

First maybe you should used another url as outputURL for your exporter, the code above use AVURLAsset's source url as exporter's outputURL, and based on you do remove file at url before export(which means you delete source asset used to exported), there's no way you can achieve your goal.
By the way, if you want to remove file based on url. You should use

[[NSFileManager defaultManager] removeItemAtURL:url error:nil];

instead.