I am trying to upload a video, add animation on top of it, and then export it, but the animation never starts playing in the exported video. It just shows the image "dogge_icon.png" as is.
I tried different types of animations, not sure what I am doing wrong. Any help would be greatly appreciated.
The code:
-(void) createCompositionWithPicture {
AVMutableComposition* composition = [AVMutableComposition composition];
NSString *videoPath = [[NSBundle mainBundle] pathForResource:@"Movie" ofType:@"m4v"];
NSLog(@"Path: %@", videoPath);
NSURL *videoURL = [[NSURL alloc] initFileURLWithPath:videoPath];
AVURLAsset *videoAsset = [AVURLAsset URLAssetWithURL:videoURL options:nil];
AVMutableCompositionTrack *videoTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
[videoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration) ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:kCMTimeZero error:nil];
AVMutableVideoCompositionInstruction *mainInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
mainInstruction.timeRange = CMTimeRangeMake(kCMTimeZero, videoAsset.duration);
AVMutableVideoCompositionLayerInstruction *videolayerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:videoTrack];
AVMutableVideoComposition *videoComposition = [AVMutableVideoComposition videoComposition];
videoComposition.renderSize = CGSizeMake(videoTrack.naturalSize.width,videoTrack.naturalSize.height);
videoComposition.frameDuration = CMTimeMake(1, 30);
mainInstruction.layerInstructions = [NSArray arrayWithObject:videolayerInstruction];
videoComposition.instructions = [NSArray arrayWithObject:mainInstruction];
NSLog(@"Width: %f Height: %f", videoTrack.naturalSize.width, videoTrack.naturalSize.height);
UIImage* image = [UIImage imageNamed:@"dogge_icon.png"];
CALayer *animationLayer = [CALayer layer];
animationLayer.frame = CGRectMake(0, 0, image.size.width, image.size.height);
[animationLayer setMasksToBounds:YES];
[animationLayer setContents: (id)image.CGImage];
CABasicAnimation *animation =
[CABasicAnimation animationWithKeyPath:@"transform.scale"];
animation.duration=5.0;
animation.autoreverses=YES;
animation.fromValue = [NSNumber numberWithFloat:1.0f];
animation.toValue = [NSNumber numberWithFloat:2.0f];
animation.repeatCount=10;
animation.beginTime = AVCoreAnimationBeginTimeAtZero;
[animationLayer addAnimation:animation forKey:@"scale"];
NSLog(@"animationLayer animations: %@", [animationLayer animationKeys]);
CALayer *parentLayer = [CALayer layer];
CALayer *videoLayer = [CALayer layer];
parentLayer.frame = CGRectMake(0, 0, videoTrack.naturalSize.width, videoTrack.naturalSize.height);
videoLayer.frame = CGRectMake(0, 0, videoTrack.naturalSize.width, videoTrack.naturalSize.height);
[parentLayer addSublayer:videoLayer];
[parentLayer addSublayer:animationLayer];
videoComposition.animationTool = [AVVideoCompositionCoreAnimationTool
videoCompositionCoreAnimationToolWithPostProcessingAsVideoLayer:videoLayer inLayer:parentLayer];
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:composition presetName:AVAssetExportPresetHighestQuality];
NSString *exportVideoPath = [STPFileUtilities getPathToFileIn: NSDocumentDirectory WithName: @"composition.mov"];
[STPFileUtilities deleteFileIfExists:exportVideoPath];
NSURL *exportURL = [NSURL fileURLWithPath:exportVideoPath];
exportSession.outputURL = exportURL;
exportSession.outputFileType = AVFileTypeQuickTimeMovie;
exportSession.videoComposition = videoComposition;
[exportSession exportAsynchronouslyWithCompletionHandler:^{
switch (exportSession.status) {
case AVAssetExportSessionStatusFailed:{
NSLog(@"FAIL: %@", exportSession.error);
break;
}
case AVAssetExportSessionStatusCompleted: {
NSLog (@"SUCCESS");
}
};
}];
}
source
share