Search code examples
objective-ciosavfoundationavmutablecomposition

AVMutableComposition Rotates Videos


I've recently discovered an issue using AVMutableComposition and I'm looking for some insight into this.

I want to be able to record video in two orientations - landscape left and right. When I record videos in landscape right (home button is on the right), they are added to the composition and played in the correct orientation. However, if I record it in orientation left (home button on the left), these clips are played upside down.

BUT, they are only played upside-down if they are inserted into the composition. Otherwise they play in the correct orientation. Why is the composition reversing the rotation of clips shot in landscape left? How can I fix this? Any help is appreciated!


Solution

  • Here's a slightly easier way if you simply want to maintain original rotation.

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