2010-08-03 45 views
3

AVFoundation框架提供了AVMutableVideoComposition類(AVVideoComposition的可變變體)。它看起來像你可以直接渲染CoreAnimations到這個類的一個實例來創建一個視頻,但我不知道如何將組合保存到一個文件或如何使用它,真的。從UIViewController調用的以下代碼似乎可以用來創建合成和動畫,但是,那麼,我很難理解如何使用合成。任何幫助或指導,非常感謝。需要使用iPhone SDK的AVMutableVideoComposition的提示

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

感謝, 喬恩

回答

2

不知道是否有幫助。

AVAssetExportSession *session = [[AVAssetExportSession alloc] initWithAsset:composition presetName:AVAssetExportPresetHighestQuality]; 
    session.videoComposition = videoComposition; 
    session.outputURL = outputURL; 
    session.outputFileType = AVFileTypeQuickTimeMovie; 
    [session exportAsynchronouslyWithCompletionHandler: 
     ^(void) 
{ 
     NSLog(@"TADA!") 
    }]; 
+0

謝謝。這讓我更接近,但仍然不夠。什麼是資產「組合」?如果我用: 'AVMutableComposition * composition = [AVmutableComposition composition];' 那麼編譯器會抱怨videoComposition不包含任何指令。我不知道要添加什麼指令。對不起,很厚。 – Jon 2010-09-14 19:51:42

+1

您是否檢查過WWDC 2010的AVEditDemo項目?你可以在蘋果的開發者頁面找到它,或者從這裏下載它:http://rghost.net/2649255在這個項目中你可以找到一個使用AVVideoCompositionCoreAnimationTool和導出功能的例子。希望能幫助到你。 – Steve 2010-09-17 09:20:18