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

时间:2019-04-03 13:43:26

标签: ios swift rotation avfoundation 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()

        }

例如,这就是我横向记录的方式。

landscape record

我得到以下结果。尽管结果应该相反。

wrong portret wrong landscape

在服务器上,它也以错误的方向存储。

wrong on server

我看了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 =可选(__C.CGAffineTransform(a:0.0,b:1.0,c:-1.0,d:0.0,tx:1080.0,ty:0.0))

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

1 个答案:

答案 0 :(得分:0)

显然,起初我在this answer中做错了事。
我只是将def get_sorted_files(src_dir, regex_ext='*', sort_reverse=False): files_to_evaluate = [os.path.join(src_dir, f) for f in os.listdir(src_dir) if re.search(r'.*\.({})$'.format(regex_ext), f)] files_to_evaluate.sort(key=os.path.getmtime, reverse=sort_reverse) return files_to_evaluate connection作为整个班级的全局字段取出,并针对它们进行了解决。
现在,我添加了以下代码,这解决了我的问题。

orientation
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!