【问题标题】:How do I save the video in the correct orientation?如何以正确的方向保存视频?
【发布时间】:2019-04-03 13:43:25
【问题描述】:

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

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

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

【问题讨论】:

    标签: ios swift rotation avfoundation orientation


    【解决方案1】:

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

    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
        }
    

    【讨论】:

    • 不适合我。当我在它自己旋转的播放器上播放视频时,我设置的一切仍然相同
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-27
    • 1970-01-01
    • 1970-01-01
    • 2015-07-07
    • 1970-01-01
    • 2015-05-04
    • 1970-01-01
    相关资源
    最近更新 更多