如何在纵向模式下通过 AVAssetExportSession 导出视频资产 [英] how to export video asset via AVAssetExportSession in portrait mode

查看:23
本文介绍了如何在纵向模式下通过 AVAssetExportSession 导出视频资产的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我通过 AVAssetExportSession 导出视频资产时,结果文件处于陆地空间模式.(通过 iTunes 抓取的文件-> 应用程序-> 文件共享-> 我的应用程序).如何以纵向模式导出视频资产(旋转它)?

when i export a video asset via AVAssetExportSession the result file is in landspace mode. (file grabbed via itune->apps->file sharing->my app). how can i export the video asset in portrait mode (rotate it)?

推荐答案

来自 iPhone 捕获设备的视频始终为横向,无论捕获时设备方向如何.

The video coming from the iPhone capture device are always landscape orientated whatever the device orientation is when capturing.

如果您想旋转视频,简单"的解决方案是为导出会话的视频轨道分配一个变换.

If you want to rotate your video, the 'simple' solution is to assign a transform to the video track of the exported session.

在您的 AVComposition 对象中创建 2 个可变轨道:

Create 2 mutable tracks in your AVComposition object :

AVMutableCompositionTrack *videoTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
AVMutableCompositionTrack *audioTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];

将您的媒体曲目添加到您的作品曲目中:

Add your medias tracks to your composition's tracks :

...        
BOOL videoResult = [videoTrack insertTimeRange:sourceCMTime 
                                       ofTrack:[tracks objectAtIndex:0] 
                                        atTime:currentTime 
                                         error:&error];

BOOL audioResult = [audioTrack insertTimeRange:sourceCMTime 
                                       ofTrack:[tracks objectAtIndex:0] 
                                        atTime:currentTime 
                                         error:&error];
...

添加完所有轨道后,将变换应用到作品的视频轨道:

After you added all your tracks, apply your transform to the video track of your composition :

    CGAffineTransform rotationTransform = CGAffineTransformMakeRotation(M_PI_2);
//    CGAffineTransform rotateTranslate = CGAffineTransformTranslate(rotationTransform,360,0);
    videoTrack.preferredTransform = rotationTransform;

(注意变换以左上角为原点,所以旋转后需要平移,但在iPhone 4S,iOS 5.1上测试,现在旋转似乎是围绕中心进行的.)

(be carful that the transform had the upper left corner as origin, so the translation was needed after rotation, but tested on iPhone 4S, iOS 5.1, it seems that the rotation is now made around the center.)

这篇关于如何在纵向模式下通过 AVAssetExportSession 导出视频资产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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