0

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.

landscape record

And I get the following result. Although the result should look the other way around.

wrong portret wrong landscape

On the server, it is also stored in the wrong orientation.

wrong on server

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?

Alex Kirov
  • 29
  • 1
  • 8

1 Answers1

0

Apparently at first I did the wrong thing in this answer.
I just took out the connection and orientation as global fields of the whole class and addressed them.
Now I have added the following code and this solves my problem.

var currentOrientation: AVCaptureVideoOrientation?
var videoConnection: AVCaptureConnection?
captureSession.addOutput(videoOutput!)

            videoConnection = videoOutput!.connection(with: .video)

            if (videoConnection!.isVideoOrientationSupported) {
                Logger.Log("connection!.isVideoOrientationSupported")
                if let capOr = AVCaptureVideoOrientation(rawValue: UIDevice.current.orientation.rawValue) {
                    Logger.Log("connection!.videoOrientation = \(capOr.rawValue)")
                    currentOrientation = capOr
                    videoConnection!.videoOrientation = currentOrientation!
                } else {
                    currentOrientation = .portrait
                    videoConnection!.videoOrientation = currentOrientation!
                }
            }
cameraPreviewLayer?.connection?.videoOrientation = currentOrientation! 
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 = \(videoOrientation.rawValue)")

            currentOrientation = videoOrientation
            videoConnection?.videoOrientation = currentOrientation!
            cameraPreviewLayer?.connection?.videoOrientation = currentOrientation!
        }

        cameraPreviewLayer?.frame.size = size
    }
Alex Kirov
  • 29
  • 1
  • 8