I have a camera in my app that can change the orientation.
Initially, the camera orientation is set from the current device orientation.
captureSession.addOutput(videoOutput!)
cameraPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
cameraPreviewLayer?.connection?.videoOrientation = AVCaptureVideoOrientation(rawValue: UIDevice.current.orientation.rawValue) ?? .portrait
When rotating, I change the camera orientation.
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
coordinator.animate(alongsideTransition: nil) { _ in UIView.setAnimationsEnabled(true) }
UIView.setAnimationsEnabled(false)
super.viewWillTransition(to: size, with: coordinator)
Logger.Log("VIEW WILL TRANSITION")
if let videoOrientation = AVCaptureVideoOrientation(rawValue: UIDevice.current.orientation.rawValue) {
Logger.Log("videoOrientation updated")
cameraPreviewLayer?.connection?.videoOrientation = videoOrientation
}
cameraPreviewLayer?.frame.size = size
}
After the recording is completed, I transfer the video URL to the AVPlayer. And you can see that the player plays the video in the wrong orientation.
if let _ = link {
let player = AVPlayer(url: link!)
self.player = player
self.player?.play()
}
For example, this is how I record in landscape orientation.
And I get the following result. Although the result should look the other way around.
On the server, it is also stored in the wrong orientation.
I watched this question. But it seems I already assign the orientation when recording.
I also watched this question. I tried to see if the video that the player plays is different in different orientations. But I always get the same result.
func playLink(){
if let _ = link {
let player = AVPlayer(url: link!)
self.player = player
self.player?.play()
}
let videoAssetTrack = self.player?.currentItem?.asset.tracks(withMediaType: .video).first
let videoTransform = videoAssetTrack?.preferredTransform
Logger.Log("videoTransform = \(videoTransform)")
}
videoTransform = Optional(__C.CGAffineTransform(a: 0.0, b: 1.0, c: -1.0, d: 0.0, tx: 1080.0, ty: 0.0))
Help me please, what should I do to get the correct result on the output?