When I create a video using this tutorial and I try to move the AVAssetTrack* firstTrack
(without animation, so I cannot use CAAnimation
s) using AVMutableVideoCompositionInstruction
with a AVMutableVideoCompositionLayerInstruction
that contains some CGAffineTransform
s, the transformations just get ignored.
Does anyone know this problem and knows how to solve it?
Thanks in advance!
This info might help: I use AVAssetExportPreset1280x720 as preset when exporting. So I think (might be wrong) this is not the problem.
EDIT: The code was split in various functions, but I tried to get everything together to one code. I hopefully did not forget anything or messed something up.
// The layer I want to move
CALayer* layerToMove = nil;
// A layer containing just the black background
CALayer* backgroundLayer = nil;
// The videos duration
double duration = 12.0;
AVMutableComposition *exportAsset = [[AVMutableComposition alloc] init];
[exportAsset setNaturalSize:layerToMove.frame.size];
AVMutableCompositionTrack* dstTrack = [self addBlackVideoTrackOfDuration:duration toComposition:exportAsset];
AVMutableVideoCompositionInstruction * MainInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
MainInstruction.timeRange = CMTimeRangeMake(kCMTimeZero, exportAsset.duration);
CGAffineTransform Scale = CGAffineTransformMakeScale(0.1f,0.1f);
CGAffineTransform Move = CGAffineTransformMakeTranslation(230,230);
AVMutableVideoCompositionLayerInstruction *FirstlayerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:dstTrack];
[FirstlayerInstruction setTransform:CGAffineTransformConcat(Scale,Move) atTime:kCMTimeZero];
[MainInstruction setLayerInstructions:@[FirstlayerInstruction]];
CALayer* compositionLayer = [CALayer layer];
compositionLayer.bounds = layerToMove.frame;
compositionLayer.anchorPoint = CGPointMake(0, 0);
compositionLayer.position = CGPointMake(0, 0);
[compositionLayer setFrame:layerToMove.frame];
[compositionLayer setBounds:layerToMove.frame];
[compositionLayer addSublayer:backgroundLayer];
[compositionLayer addSublayer:layerToMove];
AVVideoCompositionCoreAnimationTool *animationTool = [AVVideoCompositionCoreAnimationTool videoCompositionCoreAnimationToolWithPostProcessingAsVideoLayer:layerToMove inLayer:compositionLayer];
CMTime frameDuration = CMTimeMakeWithSeconds(1.0 / 25,
1000);
AVMutableVideoComposition *videoComposition = [AVMutableVideoComposition videoComposition];
[videoComposition setFrameDuration:frameDuration];
[videoComposition setInstructions:@[MainInstruction]];
[videoComposition setAnimationTool:animationTool];
[videoComposition setRenderSize:layerToMove.frame.size];
[videoComposition setRenderScale:1.0];
AVAssetExportSession *exportSession = [AVAssetExportSession exportSessionWithAsset:exportAsset presetName:AVAssetExportPreset1280x720];
[exportSession setOutputFileType:AVFileTypeQuickTimeMovie];
[exportSession setOutputURL:nil/*Any url*/];
[exportSession setVideoComposition:videoComposition];
// The export is complete
[exportSession exportAsynchronouslyWithCompletionHandler:^
{
/*Whatever*/
}];