如何在纵向模式下通过AVAssetExportSession导出视频资源 [英] how to export video asset via AVAssetExportSession in portrait mode
问题描述
当我通过AVAssetExportSession导出视频资源时,结果文件处于LANDpace模式。
(通过itune抓取文件 - > apps->文件共享 - >我的应用程序)。
如何以纵向模式导出视频资源(旋转它)?
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屋!