AVMutableComposition Rotates Videos

anon_dev1234 picture anon_dev1234 · May 29, 2012 · Viewed 9.3k times · Source

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!

Answer

dizy picture dizy · Feb 24, 2014

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