当我通过AVAssetExportSession导出视频资产时,结果文件是横向模式(通过iTunes->应用程序->文件共享->我的应用抓取文件)。 如何将视频资产导出为纵向模式(旋转它)?
当我通过AVAssetExportSession导出视频资产时,结果文件是横向模式(通过iTunes->应用程序->文件共享->我的应用抓取文件)。 如何将视频资产导出为纵向模式(旋转它)?
来自iPhone捕获设备的视频始终以横向方式呈现,无论在拍摄时设备方向如何。
如果您想旋转视频,则“简单”的解决方案是为导出会话的视频轨道分配一个变换。
在您的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上测试,似乎现在的旋转是围绕中心进行的。
当您同时转换轨道时,应设置组合渲染大小,因为它可能超出帧或出现黑块:
self.mutableVideoComposition.renderSize = CGSizeMake(assetVideoTrack.naturalSize.height,assetVideoTrack.naturalSize.width);