使用AVMutableVideoCompositionLayerInstruction旋转视频 [英] Rotating Video with AVMutableVideoCompositionLayerInstruction

查看:1078
本文介绍了使用AVMutableVideoCompositionLayerInstruction旋转视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用前置摄像头在iPhone 4上拍摄视频,并将视频与其他媒体资产相结合。我希望这个视频是纵向的 - 所有视频的默认方向都是横向的,在某些情况下,你必须手动管理。

I'm shooting video on an iPhone 4 with the front camera and combining the video with some other media assets. I'd like for this video to be portrait orientation - the default orientation for all video is landscape and in some circumstances, you have to manage this manually.

我是使用AVFoundation,特别是AVAssetExportSession和AVMutableVideoComposition。基于WWDC视频,当我将视频组合成新的构图时,我必须自己处理修正方向。

I'm using AVFoundation and specifically AVAssetExportSession with a AVMutableVideoComposition. Based on the WWDC videos, it's clear that I have to handle 'fixing' the orientation myself when I'm combining videos into a new composition.

所以,我已经创建了一个附加到我的AVMutableVideoCompositionInstruction的AVMutableVideoCompositionLayerInstruction,我使用setTransform:atTime:方法设置一个用于旋转视频的转换:

So, I've created an AVMutableVideoCompositionLayerInstruction attached to my AVMutableVideoCompositionInstruction and I'm using the setTransform:atTime: method to set a transform designed to rotate the video:

    AVMutableVideoCompositionLayerInstruction *passThroughLayer = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:videoTrack];
CGAffineTransform portraitRotationTransform = CGAffineTransformMakeRotation(degreesToRadians(90.0));
[passThroughLayer setTransform:portraitRotationTransform atTime:kCMTimeZero];

问题在于,当我查看导出的视频时,实际内容都没有出现在屏幕。如果我将旋转角度减小到45度,我可以在屏幕上看到部分视频 - 它几乎就像它不在中心点旋转一样。我将下面的图片包括在内,以便更清楚地说明我在说什么。

The problem is that when I view the video that is exported, none of the actual contents are on the screen. If I reduce the rotation angle to something like 45 degrees, I can see part of the video on the screen - it's almost as if it's not rotating at the center point. I'm including images below to make it more clear what I'm talking about.

视频的自然尺寸将恢复为480x360。我已经尝试将其更改为360x480并且它不会影响核心问题。

The natural size of the video is coming back as 480x360. I've tried changing that to 360x480 and it doesn't impact the core issue.

0度轮换:

45度旋转:

90度旋转只是绿色。

A 90 degree rotation is just all green.

无论如何,我希望之前做过此事的人可以指点我在正确的方向。我找不到关于AVFoundation合成和导出中某些更高级主题的任何文档。

Anyway, I'm hoping someone who has done this before can point me in the right direction. I can't find any docs on some of the more advanced topics in AVFoundation compositions and exports.

推荐答案

试试这个:

AVMutableVideoCompositionLayerInstruction *passThroughLayer = AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:videoTrack];
CGAffineTransform rotationTransform = CGAffineTransformMakeRotation(degreesToRadians(90.0));
CGAffineTransform rotateTranslate = CGAffineTransformTranslate(rotateTransform,320,0);
[passThroughLayer setTransform:rotateTranslate atTime:kCMTimeZero];

基本上,我们的想法是创建一个旋转和平移矩阵。您将其旋转到正确的方向,然后将其转换为视图。当我浏览API时,我没有看到任何指定中心点的方法。

Essentially the idea is to create a rotation and translation matrix. You rotate it to the proper orientation and then translate it into the view. I did not see any way to specify a center point while I was glancing through the API.

这篇关于使用AVMutableVideoCompositionLayerInstruction旋转视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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