如何以正确的方向保存视频?

How do I save the video in the correct orientation?

我的应用程序中有一个可以改变方向的摄像头。
最初,相机方向是根据当前设备方向设置的。

captureSession.addOutput(videoOutput!)
cameraPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
cameraPreviewLayer?.connection?.videoOrientation = AVCaptureVideoOrientation(rawValue: UIDevice.current.orientation.rawValue) ?? .portrait

旋转时,我改变了相机方向。

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
    }

录制完成后,我将视频URL传输到AVPlayer。而且你可以看到播放器播放视频的方向不对。

if let _ = link {
            let player = AVPlayer(url: link!)
            self.player = player
            self.player?.play()

        }

比如我是这样横屏录制的。

我得到以下结果。虽然结果应该是相反的。

在服务器上,也是存储方向错误。

我看了this question。但好像我在录制时已经指定了方向。

我也看了this question。我试着看看播放器播放的视频是否在不同方向上有所不同。但我总是得到相同的结果。

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))

请帮帮我,我应该怎么做才能得到正确的输出结果?

显然一开始我在 this answer 中做错了事。
我只是把 connectionorientation 作为整个 class 的全局字段取出来并解决了它们。
现在我添加了以下代码,这解决了我的问题。

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
    }