Search code examples
iosavfoundationavassetexportsessionavmutablecompositionavasset

How to combine portrait and landscape assets in AVMutableComposition for preview


I have AVAssets that are portrait and landscape orientation. When I play them one at a time with the AVPlayer they play with the correct orientation. So the AVPlayer is reading each video's AVAssetTrack preferredTransform and adjusting accordingly.

On the other hand, when I add a portrait asset to a AVMutableComposition it shows up sideways. Presumably this is because the AVMutableCompositionTrack just has a default preferredTransform of CGAffineTransformIdentity.

What I want is for my AVMutableComposition to honor the preferredTransform of assets I add to it so everything shows up correctly.

Part 2

I know I can perform a video rotation by supplying compositing instructions (AVMutableVideoCompositionLayerInstructions) to an AVAssetExportSession, for example as explained here. But I don't want to export the video yet.

Is AVAssetExportSession the only class that takes AVMutableVideoComposition as an input?

Part 3

I'm trying to play a preview of the video I'm about to export, and the only compositing operation is a transform to either portrait or landscape. So I think there should be a lighter-weight way to do this, without running an AVAssetExportSession every time the user changes the mutable composition. Is this right?

Aside

As an aside, I guess I'm not understanding why AVMutableVideoComposition is not itself a descendent of AVAsset like AVMutableComposition is. Then you could just play the compostion directly.


Solution

  •    AVPlayerItem * item item = [[AVPlayerItem alloc] initWithAsset:[collageComposition mutableCopy]];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:item];
    item.videoComposition = [collageVideoComposition mutableCopy];
    [item setSeekingWaitsForVideoCompositionRendering:NO];
    item.audioMix = audioMix;
    self.previewPlayer = [AVPlayer playerWithPlayerItem:item];
    

    that's all you need for a preview. If you use animationTool in your AVMutableVideoComposition AVPlayer don't suport it. Use AVSynchronizedLayer for this purpose.