AVVideoCompositionCoreAnimationTool和CALayer处于纵向模式? [英] AVVideoCompositionCoreAnimationTool and CALayer in portrait mode?

查看:106
本文介绍了AVVideoCompositionCoreAnimationTool和CALayer处于纵向模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用iOS 4.3上的AVMutableComposition,AVMutableVideoComposition和AVVideoCompositionCoreAnimationTool将CALayer烘焙成纵向模式视频(导出时)。这一切都在景观中起作用。但是,如果我以纵向捕捉视频,AVVideoCompositionCoreAnimationTool将忽略视频轨道上的变换。也就是说,对于纵向模式视频,我将AVMutableCompositionTrack.preferredTransform设置为原始资产视频轨道中的preferredTransform值。只要我不使用AVVideoCompositionCoreAnimationTool,这就可以了,视频以纵向模式显示。然而,只要我添加AVVideoCompositionCoreAnimationTool和CALayer,该文件就会出现。 (CALayer显示正确,但背后的视频位于其侧面,文件的宽高比关闭)。我尝试将变换应用于CALayer,并在ACVideoComposition中设置变换。这些都不会改变生成的文件的方向(它仍然是480x369,而不是360x480)。有没有办法用AVVideoCompositionCoreAnimationTool渲染肖像模式视频?

I'm trying to bake a CALayer into portrait-mode video (on export) using an AVMutableComposition, an AVMutableVideoComposition and a AVVideoCompositionCoreAnimationTool on iOS 4.3. This all works in landscape. If I capture video in portrait, however, the AVVideoCompositionCoreAnimationTool is ignoring the transform on the video track. That is, for portrait-mode video, I am setting AVMutableCompositionTrack.preferredTransform to the preferredTransform value from the original asset video track. As long as I don't use a AVVideoCompositionCoreAnimationTool, this works, and the video comes out in portrait mode. As soon as I add a AVVideoCompositionCoreAnimationTool and CALayer, however, the file comes out in landscape. (The CALayer appears correctly, but the video behind it is on its side, and the aspect ratio of the file is off). I've tried applying the transform to the CALayer, and setting a transform in the ACVideoComposition. Neither of these change the orientation of the file produced (it is still 480x369, not 360x480). Is there a way to render portrait-mode video with AVVideoCompositionCoreAnimationTool?

首先我设置一个AVMutableComposition和AVMutableVideoComposition

First I set up a AVMutableComposition and AVMutableVideoComposition

AVMutableComposition *composition = [AVMutableComposition composition];
AVMutableCompositionTrack *compositionVideoTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
AVURLAsset *videoAsset = [AVURLAsset URLAssetWithURL:url options:nil];
CMTimeRange timeRange = CMTimeRangeMake(kCMTimeZero, [videoAsset duration]);
AVAssetTrack *clipVideoTrack = [[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];

CGSize videoSize = CGSizeApplyAffineTransform(clipVideoTrack.naturalSize, clipVideoTrack.preferredTransform);
videoSize.width = fabs(videoSize.width);
videoSize.height = fabs(videoSize.height);

CMTime titleDuration = CMTimeMakeWithSeconds(5, 600);
CMTimeRange titleRange = CMTimeRangeMake(kCMTimeZero, titleDuration);

[compositionVideoTrack insertTimeRange:titleRange ofTrack:nil atTime:kCMTimeZero error:nil];
[compositionVideoTrack insertTimeRange:timeRange ofTrack:clipVideoTrack atTime:titleDuration error:nil];
compositionVideoTrack.preferredTransform = clipVideoTrack.preferredTransform;

AVMutableVideoComposition *videoComposition = [AVMutableVideoComposition videoComposition];
AVMutableVideoCompositionInstruction *passThroughInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
passThroughInstruction.timeRange = CMTimeRangeMake(kCMTimeZero, [composition duration]);
AVAssetTrack *videoTrack = [[composition tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
AVMutableVideoCompositionLayerInstruction *passThroughLayer = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:videoTrack];    
passThroughInstruction.layerInstructions = [NSArray arrayWithObject:passThroughLayer];
videoComposition.instructions = [NSArray arrayWithObject:passThroughInstruction];       
videoComposition.frameDuration = CMTimeMake(1, 30); 
videoComposition.renderSize = videoSize;
videoComposition.renderScale = 1.0;

带标题的CALayer

And a CALayer with a title

CALayer *animationLayer = [CALayer layer];
animationLayer.bounds = CGRectMake(0, 0, videoSize.width, videoSize.height);

CATextLayer *titleLayer = [CATextLayer layer];
titleLayer.string = [effect valueForKey:@"title"];
titleLayer.font = [effect valueForKey:@"font"];
titleLayer.fontSize = 30;

titleLayer.alignmentMode = kCAAlignmentCenter;
titleLayer.bounds = CGRectMake(0, 0, videoSize.width, videoSize.height / 6);

[animationLayer addSublayer:titleLayer];
titleLayer.anchorPoint =  CGPointMake(0.5, 0.5);
titleLayer.position = CGPointMake(CGRectGetMidX(layer.bounds), CGRectGetMidY(layer.bounds));    

CABasicAnimation *fadeAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
fadeAnimation.fromValue = [NSNumber numberWithFloat:1.0];
fadeAnimation.toValue = [NSNumber numberWithFloat:0.0];
fadeAnimation.additive = NO;
fadeAnimation.removedOnCompletion = NO;
fadeAnimation.beginTime = 3.5;
fadeAnimation.duration = 1.0;
fadeAnimation.fillMode = kCAFillModeBoth;
[titleLayer addAnimation:fadeAnimation forKey:nil];

最后,我将CALayer添加到AVMutableVideoComposition

Finally I add the CALayer to the AVMutableVideoComposition

CALayer *parentLayer = [CALayer layer];
CALayer *videoLayer = [CALayer layer];

parentLayer.bounds = CGRectMake(0, 0, videoSize.width, videoSize.height);
parentLayer.anchorPoint =  CGPointMake(0, 0);
parentLayer.position = CGPointMake(0, 0);

videoLayer.bounds = CGRectMake(0, 0, videoSize.width, videoSize.height);
[parentLayer addSublayer:videoLayer];
videoLayer.anchorPoint =  CGPointMake(0.5, 0.5);
videoLayer.position = CGPointMake(CGRectGetMidX(parentLayer.bounds), CGRectGetMidY(parentLayer.bounds));
[parentLayer addSublayer:layer];    
animationLayer.anchorPoint =  CGPointMake(0.5, 0.5);
animationLayer.position = CGPointMake(CGRectGetMidX(parentLayer.bounds), CGRectGetMidY(parentLayer.bounds));
videoComposition.animationTool = [AVVideoCompositionCoreAnimationTool videoCompositionCoreAnimationToolWithPostProcessingAsVideoLayer:videoLayer inLayer:parentLayer];

并导出!

AVAssetExportSession *exportSession = [[[AVAssetExportSession alloc] initWithAsset:composition presetName:AVAssetExportPresetMediumQuality] autorelease];
exportSession.videoComposition = videoComposition; 

NSURL *segmentFileURL = //some local URL
exportSession.outputFileType = @"com.apple.quicktime-movie";
exportSession.outputURL = segmentFileURL;


[exportSession exportAsynchronouslyWithCompletionHandler:^{
    switch ([exportSession status]) {
        case AVAssetExportSessionStatusFailed:
            Log(@"Export failed: %@", [exportSession error]);
            break;
        case AVAssetExportSessionStatusCancelled:
            Log(@"Export canceled");
            break;
        case AVAssetExportSessionStatusCompleted:
            Log(@"Export done");
            break;
    }
}]; 

此代码在横向模式下工作,如果我删除行 videoComposition.animationTool = [AVVideoCompositionCoreAnimationTool videoCompositionCoreAnimationToolWithPostProcessingAsVideoLayer:videoLayer inLayer:parentLayer];

This code works in landscape mode, and also in portrait if I remove the line videoComposition.animationTool = [AVVideoCompositionCoreAnimationTool videoCompositionCoreAnimationToolWithPostProcessingAsVideoLayer:videoLayer inLayer:parentLayer];

推荐答案

试试这个和添加您的其他说明。

Try this and add your other instructions as well.

///turn video 90 degrees
AVMutableVideoCompositionLayerInstruction *passThroughLayer = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:clipVideoTrack];
CGAffineTransform rotationTransform = CGAffineTransformMakeRotation(degreesToRadians(90.0));
CGAffineTransform rotateTranslate = CGAffineTransformTranslate(rotationTransform,320,0);
[passThroughLayer setTransform:rotateTranslate atTime:kCMTimeZero];

让我知道它是怎么回事!

Let me know how it goes!

MacMike

这篇关于AVVideoCompositionCoreAnimationTool和CALayer处于纵向模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆