AVMutableComposition旋转视频

时间:2012-05-29 19:25:47

标签: objective-c ios avfoundation avmutablecomposition

我最近发现了一个使用AVMutableComposition的问题,而我正在寻找对此的一些见解。

我希望能够以两个方向录制视频 - 左右横向。当我以横向录制视频时(主页按钮在右侧),它们将被添加到合成中并以正确的方向播放。但是,如果我将其记录在左侧方向(左侧的主页按钮),则这些剪辑会上下颠倒。

但是,如果将它们插入构图中,它们只会被颠倒。否则他们会以正确的方向进行游戏。为什么构图会逆转横向拍摄的剪辑的旋转?我怎样才能解决这个问题?任何帮助表示赞赏!

3 个答案:

答案 0 :(得分:28)

如果您只是想保持原始旋转,这是一种稍微简单的方法。

// 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...

答案 1 :(得分:9)

解决了我的问题。终于能够旋转轨道并将其转换为框架。像魅力一样。

    //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];
    }

答案 2 :(得分:0)

我认为答案肯定是最好的选择,但它只是部分正确。事实上,为了使其工作,我们还必须调整导出的渲染大小,翻转纵向轨道自然大小的高度和宽度。

我刚刚测试了它,我还引用了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..