0
votes

My setup is fairly straight forward. I have a composition with a video and audio track. Videos are captured in either left or right landscape orientations. Video frame buffers are captured with an AVCaptureVideoDataOutput object and written with AVAssetWriter/AVAssetWriterInput objects. These videos are then displayed in an AVPlayerLayer.

I am setting the writer input's transform based on the device orientation.

if UIDevice.currentDevice.orientation == UIDeviceOrientation.LandscapeRight {
  videoWriterVideoInput.transform = CGAffineTransformMakeRotation(CGFloat(M_PI))
}

Reviewing the output video in Quicktime looks like this: enter image description here

Next, this video is displayed in an AVPlayerLayer:

enter image description here

Exporting the AVMutableComposition with AVAssetExportSession also results in the video flipped 180 degrees.

I can't set the preferredTransform on the composition's video track because videos will be recorded in either landscape left or right orientation.

2

2 Answers

2
votes

I believe you need to set the orientation of the connection, so that you'll have a proper orientation on the EXIF data. If it's the EXIF (metadata) to blame, that would explain why it's displaying correctly in QuickTime but not in iOS.

Something like this:

self.captureConnection.setVideoOrientation(UIDevice.currentDevice.orientation)

1
votes

You are missing a few details but I will walk you through a basic process that worked for me:

Just before I begin my video capture I call a delegate method that sets up the AVAssetWriter and a few other things. In this method I:

AVCaptureConnection *videoConnection = [_myCaptureVideoDataOutput connectionWithMediaType:AVMediaTypeVideo];

// you can use the devices orientation or force one here with a switch statement or something
[videoConnection setVideoOrientation:UIDevice.currentDevice.orientation];

This code (with all the setup obviously) and simply calling _assetWriterVideoInput.transform = CGAffineTransformIdentity; instead worked perfectly fine for me. There are a lot of places where this can go wrong and it's hard to tell without more information but hope this helps. I'm worried you're applying an additional transform when it might automatically use the one set to the AVVideoConnection that is outputting the sample buffers.