AVMutableComposition旋转视频

ano*_*234 11 objective-c avfoundation ios avmutablecomposition

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

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

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

diz*_*izy 29

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

// 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...
Run Code Online (Sandbox Code Playgroud)

  • 如果合成中有多个轨道具有不同的变换,您不能简单地设置每个轨道的变换,您需要进行不同的补偿。 (2认同)
  • @bgoers也许,答案变得无效,对我来说它现在对我不起作用,因为我查了一下PreferredTransform属性是readOnly.我们不能影响这个属性 (2认同)

ano*_*234 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];
    }
Run Code Online (Sandbox Code Playgroud)