In my code, the user chooses a video from the camera roll, which I then slice up and write back to the camera roll using AVMutableVideoComposition
and AVAssetExportSession
.
It is working fine for me when the video is in portrait, but not so much when the video was shot in landscape. Specifically, when the video is landscape left
(which I’m calling when the user rotates the phone counter clockwise) it works fine, but when the video is landscape right
(i.e. the user rotates the phone clockwise) the end result is upside down.
Interestingly, the preferredTransform
and the naturalSize
of the videos are always (1, 0, 0, 1)
and 1280 x 720
, regardless of the perceived orientation of the video. So how might I distinguish between the different landscape orientations, so I could optionally rotate the video 180 degrees?
In fact, the only way I know if the video is portrait is using this post:
func isVideoPortrait(theAsset: AVAsset) -> Bool {
guard let videoTrack = theAsset.tracks(withMediaType: AVMediaType.video).first else {
return false
}
let transformedVideoSize = videoTrack.naturalSize.applying(videoTrack.preferredTransform)
return abs(transformedVideoSize.width) < abs(transformedVideoSize.height)
}
There is a lot of code, so if what I’ve shared above is not sufficient, I will add more about how the video is composed and exported.