如何控制使用AVMutableComposition组装的视频的方向 [英] how to control orientation of video assembled with AVMutableComposition

查看:1091
本文介绍了如何控制使用AVMutableComposition组装的视频的方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在纵向模式下组装了一堆在iPhone上拍摄的视频剪辑。为了组装它们,我采取以下直接的方法:



AVURLAsset以获取不同的视频,然后将这些放入AVMutableCompositionTrack,然后将其放入AVMutableComposition, m导出到文件与AVAssetExportSession



我的问题是,当我来显示视频在UIWebView,它是以横向模式出现。但是,如果我查看任何组件视图,它们将显示在纵向视图中。有谁知道如何整理方向。我试图搞砸与AVMutableComposition naturalSize改变宽度和高度,但只是使我的人看起来短而胖!



Tudor

$

b $ b

解决方案

设置将处理rotate + scale的视频组合:

 code> AVMutableVideoComposition * videoComposition = [[AVMutableVideoComposition videoComposition] retain]; 
videoComposition.renderSize = CGSizeMake(320,240);
videoComposition.frameDuration = CMTimeMake(1,30);

AVMutableVideoCompositionInstruction * instruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
instruction.timeRange = CMTimeRangeMake(kCMTimeZero,CMTimeMakeWithSeconds(60,30));

AVMutableVideoCompositionLayerInstruction * rotator = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:[[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]];
CGAffineTransform translateToCenter = CGAffineTransformMakeTranslation(0,-320);
CGAffineTransform rotateBy90Degrees = CGAffineTransformMakeRotation(M_PI_2);
CGAffineTransform shrinkWidth = CGAffineTransformMakeScale(0.66,1); //需要因为苹果在默认情况下伸展 - 真的,我们应该找到和撤消苹果的弹力 - 我怀疑它会是一个CALayer defaultTransform或UIView属性导致这
CGAffineTransform finalTransform = CGAffineTransformConcat(shrinkWidth,CGAffineTransformConcat (translateToCenter,rotateBy90Degrees));
[rotator setTransform:finalTransform atTime:kCMTimeZero];

instruction.layerInstructions = [NSArray arrayWithObject:rotator];
videoComposition.instructions = [NSArray arrayWithObject:instruction];


I am assembling a bunch of video clips filmed on the iPhone in portrait mode. To assemble them I am taking straightforward approach as follows:

AVURLAsset to get hold of the different videos then shoving these into an AVMutableCompositionTrack and then putting this into an AVMutableComposition which I'm exporting to file with AVAssetExportSession

My problem is that when I come to display the video in a UIWebView, it is appearing in landscape mode. However, if I view any of the component views they appear in Portrait view. Does anyone know how to sort out the orientation. I tried messing around with the AVMutableComposition naturalSize changing the width and height around but that just made my people look short and fat! (whilst on their side)

Thanks in advance for any thoughts / suggestions

Tudor

解决方案

Setup a video composition that will handle rotate + scale:

AVMutableVideoComposition* videoComposition = [[AVMutableVideoComposition videoComposition]retain];
videoComposition.renderSize = CGSizeMake(320, 240);
videoComposition.frameDuration = CMTimeMake(1, 30);

AVMutableVideoCompositionInstruction *instruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
instruction.timeRange = CMTimeRangeMake(kCMTimeZero, CMTimeMakeWithSeconds(60, 30) );

AVMutableVideoCompositionLayerInstruction* rotator = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:[[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]];
CGAffineTransform translateToCenter = CGAffineTransformMakeTranslation( 0,-320);    
CGAffineTransform rotateBy90Degrees = CGAffineTransformMakeRotation( M_PI_2);
CGAffineTransform shrinkWidth = CGAffineTransformMakeScale(0.66, 1); // needed because Apple does a "stretch" by default - really, we should find and undo apple's stretch - I suspect it'll be a CALayer defaultTransform, or UIView property causing this
CGAffineTransform finalTransform = CGAffineTransformConcat( shrinkWidth, CGAffineTransformConcat(translateToCenter, rotateBy90Degrees) );
[rotator setTransform:finalTransform atTime:kCMTimeZero];

instruction.layerInstructions = [NSArray arrayWithObject: rotator];
videoComposition.instructions = [NSArray arrayWithObject: instruction];

这篇关于如何控制使用AVMutableComposition组装的视频的方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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