views:

374

answers:

1

The AVFoundation framework provides the AVMutableVideoComposition class (the mutable variant of AVVideoComposition). It looks like you can render CoreAnimations directly to an instance of this class to create a video but I don't know how to save the composition to a file or how to work with it at all, really. The following code called from a UIViewController appears to work to create the composition and the animation but, then, well, I'm stumped as to how to work with the composition. Any help or guidance is greatly appreciated.

static AVMutableVideoComposition *videoComposition = nil;
- (void)animationDidStop:(CAAnimation *)animation finished:(BOOL)flag {
 //Do something with videoComposition here... how to save it to a file?
 NSLog(@"videoComposition: %@", videoComposition);
 [videoComposition release]; videoComposition = nil;
}

- (IBAction)createVideoComposition:(id)sender {
 AVVideoCompositionCoreAnimationTool *videoCompositionCoreAnimationTool = [AVVideoCompositionCoreAnimationTool videoCompositionCoreAnimationToolWithPostProcessingAsVideoLayer:self.view.layer inLayer:self.view.layer];
 videoComposition = [[AVMutableVideoComposition videoComposition] retain];
 [videoComposition setRenderSize:CGSizeMake(320.0, 480.0)];
 [videoComposition setRenderScale:1.0];
 [videoComposition setFrameDuration:CMTimeMake(1, 10)];
 [videoComposition setAnimationTool:videoCompositionCoreAnimationTool];
 //add a basic animation to shake the controller's view
 CAKeyframeAnimation *shakeAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
 shakeAnimation.delegate = self;
 shakeAnimation.removedOnCompletion = YES;
 shakeAnimation.duration = 0.5;
 CGMutablePathRef path = CGPathCreateMutable();
 CGFloat midX = self.view.center.x;
 CGFloat midY = self.view.center.y;
 CGPathMoveToPoint(path, nil, midX, midY);
 CGPathAddLineToPoint(path, nil, midX + 10.0, midY);
 CGPathAddLineToPoint(path, nil, midX - 20.0, midY);
 CGPathAddLineToPoint(path, nil, midX + 15.0, midY);
 CGPathAddLineToPoint(path, nil, midX - 5.0, midY);
 CGPathAddLineToPoint(path, nil, midX, midY);
 shakeAnimation.path = path;
 CFRelease(path);
 [self.view.layer addAnimation:shakeAnimation forKey:@"shakeAnimation"];
}

Thanks, Jon

+1  A: 

Not sure if it helps.

    AVAssetExportSession *session = [[AVAssetExportSession alloc] initWithAsset:composition presetName:AVAssetExportPresetHighestQuality];
    session.videoComposition = videoComposition;
    session.outputURL = outputURL;
    session.outputFileType = AVFileTypeQuickTimeMovie;
    [session exportAsynchronouslyWithCompletionHandler:
         ^(void )
{
        NSLog(@"TADA!")
    }];
Steve
Thank you. This is getting me closer but still not quite there. What is the asset "composition"? If I use:`AVMutableComposition *composition = [AVMutableComposition composition];`then the compiler complains that videoComposition doesn't contain any instructions. I don't know what instructions to add. Sorry for being thick.
Jon
Did you checked AVEditDemo project from WWDC 2010? You can find it on apple's developer page, or download it from here: http://rghost.net/2649255 In that project you can find an example of using AVVideoCompositionCoreAnimationTool with export feature. Hope it helps.
Steve