簡體   English   中英

在Mac OS X 10.7 Lion上使用Core Animation進行AVFoundation問題

[英]AVFoundation Issue With Core Animation on Mac OS X 10.7 Lion

在Mac OS X 10.7中,Apple推出了AVFoundation,我正在嘗試生成一個包含動畫形狀的簡單快速時間電影。 問題是核心動畫沒有渲染,我最終只有一個空白的“黑色”視頻。 下面是我使用的代碼。 我嘗試了很多變化但是唉它們都沒有用。 導出狀態始終為“已完成”。

奇怪的是,UI視圖包含另一個層設置,但具有相同的方式,但添加到AVSynchronizedLayer,它顯示得很好,我能夠在動畫中來回擦洗。

// NOTE: composition is an AVMutableComposition containing a single video 
//       track (30s of black in 1280 x 720).

// Create the animated layer
CALayer *renderAnimLayer = [CALayer layer];
renderAnimLayer.frame = CGRectMake(0, 0, 1280, 720);

// -- EDIT: Added animated square (now animates)
renderAnimLayer.backgroundColor = CGColorCreateGenericRGB(0.3, 0.0, 0.0, 0.5);

// -- Removed
// [self setupAnimationOnLayer:renderAnimLayer];

CALayer *square = [CALayer layer];
square.backgroundColor = CGColorCreateGenericRGB(0, 0, 1, 0.8);
square.frame = CGRectMake(100, 100, 100, 100);

[CATransaction begin];
[CATransaction setDisableActions:YES];
[CATransaction setAnimationDuration:30.0];

CABasicAnimation *animation = [CABasicAnimation animation];
animation.fromValue = [NSValue valueWithPoint:square.position];
animation.toValue = [NSValue valueWithPoint:CGPointOffset(square.position, 800, 400)];
animation.removedOnCompletion = NO;
animation.beginTime = AVCoreAnimationBeginTimeAtZero;
animation.duration = 30.0;

[CATransaction commit];

[square addAnimation:animation forKey:@"position"];
[renderAnimLayer addSublayer:square];
// -- End of Edit

// Create a composition
AVMutableVideoCompositionLayerInstruction *layerInstr1 = 
    [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstruction];
layerInstr1.trackID = 2;

AVMutableVideoCompositionInstruction *instr = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
instr.timeRange = CMTimeRangeMake(kCMTimeZero, composition.duration);
instr.layerInstructions = [NSArray arrayWithObject:layerInstr1];

AVMutableVideoComposition *renderComp = [AVMutableVideoComposition videoComposition];
renderComp.renderSize    = renderAnimLayer.frame.size;
renderComp.frameDuration = CMTimeMake(1, 30); // Normally 1,30
renderComp.instructions  = [NSArray arrayWithObject:instr];
renderComp.animationTool = [AVVideoCompositionCoreAnimationTool videoCompositionCoreAnimationToolWithAdditionalLayer:renderAnimLayer asTrackID:2];


// Create an export session and export
AVAssetExportSession *exportSession = [AVAssetExportSession exportSessionWithAsset:composition presetName:@"AVAssetExportPreset1280x720"];
exportSession.outputURL = [NSURL URLWithString:@"file:///Users/eric/Desktop/toto.mov"];
exportSession.timeRange = CMTimeRangeMake(kCMTimeZero, CMTimeMake(30, 1));
exportSession.shouldOptimizeForNetworkUse = YES;    
exportSession.videoComposition = renderComp;

// Just see how things have finished.
[exportSession exportAsynchronouslyWithCompletionHandler:^() {
    NSLog(@"Export completed with status: %ld", exportSession.status);
}];

// TODO: remove once everything works and objects have been retained.
while (exportSession.progress < 1.0)
    usleep(200000);

找到問題的原因

感謝@ChristianK的指針。 添加藍色方形動畫並看到它工作后,我不得不像ChritianK一樣得出結論,我的setupAnimationOnLayer就是問題所在。 起初我認為這是我設置關鍵幀動畫的方式,但也有效。 事實證明我正在使用CAShapeLayers並且根據這個問題 ,CAShapeLayers在調用renderInContext時不會渲染,這就是AVExportSession在后台嘗試做的事情。 這也可以解釋為什么在查看我的圖層備份視圖設置時,我沒有遇到這個問題,同時調用setupAnimationOnLayer:

感謝你們兩位的幫助。

啊,你需要使用NSView作為動畫層的基礎。 CALayers是非常古怪的野獸,關於它們的一個鮮為人知的事實是它們有渲染問題,有時需要將Quicktime組件拖到瀏覽器框架之外才能啟動。

你將不得不做一些重構,但NSView絕對是一個開始的地方。 祝好運!

部分解決方案

上面的代碼現在可以導出簡單的圖層動畫,但它不會做的是導出自定義CALayers,其內容由drawInContext:或其委托版本drawLayer:inContext:確定。 CAShapeLayer不會導出其內容,也不會導出MyCustomLayer(據我所知)。

下一步:了解如何在AVExportSession中導出自定義圖層。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM