当我通过 AVAssetExportSession 导出视频 Assets 时,结果文件处于 landspace 模式。 (通过 itune->apps->file sharing->my app 获取文件)。 如何以纵向模式导出视频资源(旋转)?
最佳答案
无论设备在捕获时的方向如何,来自 iPhone 捕获设备的视频始终是横向的。
如果您想要旋转视频,“简单”的解决方案是为导出 session 的视频轨道指定一个变换。
在您的 AVComposition 对象中创建 2 个可变轨道:
AVMutableCompositionTrack *videoTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
AVMutableCompositionTrack *audioTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
将您的媒体轨道添加到您的作品轨道中:
...
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];
...
添加所有轨道后,将变换应用到合成的视频轨道:
CGAffineTransform rotationTransform = CGAffineTransformMakeRotation(M_PI_2);
// CGAffineTransform rotateTranslate = CGAffineTransformTranslate(rotationTransform,360,0);
videoTrack.preferredTransform = rotationTransform;
(注意变换是以左上角为原点,所以旋转后需要平移,但在iPhone 4S,iOS 5.1上测试,现在似乎是围绕中心旋转。)
关于iphone - 如何在纵向模式下通过 AVAssetExportSession 导出视频资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10034337/