我最近发现在使用AVMutableComposition时出现了一个问题,希望能得到一些见解。
我想能够以两种方向录制视频-横向左和右。 当我在横向右(Home键在右侧)录制视频时,它们被添加到合成中并以正确的方向播放。 但是,如果我在横向左(Home键在左侧)录制,则这些剪辑会倒置播放。
然而,只有当它们被插入到合成中时,它们才会倒置播放,否则它们将以正确的方向播放。为什么合成会翻转以横向左拍摄的剪辑的旋转? 我应该怎么修复这个问题? 非常感谢任何帮助!
我最近发现在使用AVMutableComposition时出现了一个问题,希望能得到一些见解。
我想能够以两种方向录制视频-横向左和右。 当我在横向右(Home键在右侧)录制视频时,它们被添加到合成中并以正确的方向播放。 但是,如果我在横向左(Home键在左侧)录制,则这些剪辑会倒置播放。
然而,只有当它们被插入到合成中时,它们才会倒置播放,否则它们将以正确的方向播放。为什么合成会翻转以横向左拍摄的剪辑的旋转? 我应该怎么修复这个问题? 非常感谢任何帮助!
如果你只是想保持原有的旋转方式,这里有一个稍微简单一点的方法。
// Grab the source track from AVURLAsset for example.
AVAssetTrack *assetVideoTrack = [asset tracksWithMediaType:AVMediaTypeVideo].lastObject;
// Grab the composition video track from AVMutableComposition you already made.
AVMutableCompositionTrack *compositionVideoTrack = [composition tracksWithMediaType:AVMediaTypeVideo].lastObject;
// Apply the original transform.
if (assetVideoTrack && compositionVideoTrack) {
[compositionVideoTrack setPreferredTransform:assetVideoTrack.preferredTransform];
}
// Export...
问题解决了。终于能够旋转轨道并将其转换为框架。非常好用。
//setting up the first video based on previous recording
CMTimeRange videoDuration = CMTimeRangeMake(kCMTimeZero, [self.previousRecording duration]);
AVAssetTrack *clipVideoTrack = [[self.previousRecording tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
AVAssetTrack *clipAudioTrack = [[self.previousRecording tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
[compositionVideoTrack insertTimeRange:videoDuration ofTrack:clipVideoTrack atTime:nextClipStartTime error:nil];
[compositionAudioTrack insertTimeRange:videoDuration ofTrack:clipAudioTrack atTime:nextClipStartTime error:nil];
//our first track instruction - set up the instruction layer, then check the orientation of the track
//if the track is in landscape-left mode, it needs to be rotated 180 degrees (PI)
AVMutableVideoCompositionLayerInstruction *firstTrackInstruction =
[AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:clipVideoTrack];
if([self orientationForTrack:clipVideoTrack] == UIDeviceOrientationLandscapeLeft) {
CGAffineTransform rotation = CGAffineTransformMakeRotation(M_PI);
CGAffineTransform translateToCenter = CGAffineTransformMakeTranslation(640, 480);
CGAffineTransform mixedTransform = CGAffineTransformConcat(rotation, translateToCenter);
[firstTrackInstruction setTransform:mixedTransform atTime:kCMTimeZero];
}
[self orientationForTrack:]
是什么?它的实现是什么? - Julian F. Weinert我认为答案肯定是最好的选择,但它只是部分正确。实际上,为了使其正常工作,我们还必须调整导出的渲染大小,翻转纵向轨道的自然大小的高度和宽度。
我刚刚测试了一下,并引用了AVFoundation编程指南-编辑部分,建议实现@dizy答案中实际建议的内容,但加上了提到的内容:
所有AVAssetTrack对象都有一个preferredTransform属性,其中包含该资产轨迹的方向信息。每当资产轨迹在屏幕上显示时,都会应用此变换。在先前的代码中,图层指令的变换设置为资产轨迹的变换,以便新组合中的视频正常显示 一旦您调整了其渲染大小。
那么代码应该像这样(只需添加两行):
// Grab the source track from AVURLAsset for example.
AVAssetTrack *assetVideoTrack = [asset tracksWithMediaType:AVMediaTypeVideo].lastObject;
// Grab the composition video track from AVMutableComposition you already made.
AVMutableCompositionTrack *compositionVideoTrack = [composition tracksWithMediaType:AVMediaTypeVideo].lastObject;
// Apply the original transform.
if (assetVideoTrack && compositionVideoTrack) {
[compositionVideoTrack setPreferredTransform:assetVideoTrack.preferredTransform];
}
flippedSize = CGSize(compositionVideoTrack.naturalSize.height, compositionVideoTrack.naturalSize.width);
composition.renderSize = flippedSize;
// Export..