AV Foundation animation never starts

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];

    // Create an AVMutableVideoCompositionLayerInstruction for the video track.
    AVMutableVideoCompositionInstruction *mainInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
    mainInstruction.timeRange = CMTimeRangeMake(kCMTimeZero, videoAsset.duration);

    AVMutableVideoCompositionLayerInstruction *videolayerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:videoTrack];

    // Setup video composition
    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);

    // Setup animation layer
    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];

    // Add animation
    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]);

    // Build layer hierarchy
    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];

    // Export 
    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");
            }
        };
    }];
}
+3
source share
2 answers

I'm not sure what I did. I rewrote all the code from scratch and added

animation.removedOnCompletion = NO;

for all animations, and now it works.

+2
source

The documentation states:

You use the AVVideoCompositionCoreAnimationTool object to enable Core Animation in a video.

, , :

  • beginTime AVCoreAnimationBeginTimeAtZero, 0 ( CoreAnimation CACurrentMediaTime);
  • removeOnCompletion , ;
  • , UIView.

https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVVideoCompositionCoreAnimationTool_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40009744-CH1-DontLinkElementID_1

,

animation.removedOnCompletion = NO;

, . 100%, , , , , ​​ . , , .

CALayers Animations 0, , , .


.

() (Opacity 0.0). 2 (startTime) 0.5 1.0 ( ) 3 . "" - ( ) .

float startTime = 2.0f;
float duration = 3.0f;
CALayer layerToAnimate = [CALayer layer];
layerToAnimate.opacity = 0.0;

// Just turn it on then off.
CABasicAnimation *myAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
[myAnimation setBeginTime: startTime];
[myAnimation setDuration: duration];
[myAnimation setFromValue:[NSNumber numberWithFloat:0.5]];
[myAnimation setToValue:[NSNumber numberWithFloat:1.0]];
[myAnimation setRemovedOnCompletion:NO];

[layerToAnimate addAnimation:myAnimation forKey:@"myUniqueAnimationKey"];

// NB You may not need to be specific on track ID. I like to do so to use to sort layers in to the proper order.
AVVideoCompositionCoreAnimationTool *aniTool = [AVVideoCompositionCoreAnimationTool videoCompositionCoreAnimationToolWithAdditionalLayer:_layerToAnimate asTrackID:intTrackID];

, . "" , . , , . - , , .

0

All Articles