【问题标题】:Mirroring a front-camera video in iOS在 iOS 中镜像前置摄像头视频
【发布时间】:2018-12-16 10:03:33
【问题描述】:

我正在使用 AVFoundation 将视频录制添加到应用程序中。我设法录制视频然后显示它,但后来我意识到(与预览不同)前置摄像头视频没有沿垂直轴镜像。这似乎是标准行为,但我希望视频看起来像预览。我相信CGAffineTransform 可以做到这一点,但我不确定如何将其应用到视频中。

这是我目前所拥有的:

extension CameraViewController: AVCaptureFileOutputRecordingDelegate {
    func fileOutput(_ output: AVCaptureFileOutput, didFinishRecordingTo outputFileURL: URL, from connections: [AVCaptureConnection], error: Error?) {
        guard error != nil else {
            print("Error recording movie: \(error!.localizedDescription)")
            return
        }

        if self.currentCameraPosition == .front {
            mirrorVideo(outputFileURL)
        }
        performSegue(withIdentifier: "ShowVideo", sender: outputFileURL)
    }

    func mirrorVideo(_ outputFileURL: URL){
        var transform: CGAffineTransform = CGAffineTransform(scaleX: -1.0, y: 1.0)
        transform = transform.rotated(by: CGFloat(Double.pi/2))
        // Apply transform
    }
}

【问题讨论】:

  • 看看接受的答案,它的目标C,如果你不能转换并申请,告诉我我会尽力为你写:stackoverflow.com/questions/12136841/…
  • 太好了,有足够的信息让我得到答案!

标签: ios swift avfoundation cgaffinetransform


【解决方案1】:

在您的 AVCaptureSession

添加输入和输出后使用此方法
    private func adjustVideoMirror(){

        guard let currentCameraInput: AVCaptureDeviceInput = captureSession.inputs.first as? AVCaptureDeviceInput else {
            return
        }

        if let conn = movieOutput.connection(with: .video){
            conn.isVideoMirrored = currentCameraInput.device.position == .front
        }

    }

关键是isVideoMirrored属性

【讨论】:

    【解决方案2】:

    根据我得到的回复和一些玩弄我得出了一个答案:

    extension CameraViewController: AVCaptureFileOutputRecordingDelegate {
        func fileOutput(_ output: AVCaptureFileOutput, didFinishRecordingTo outputFileURL: URL, from connections: [AVCaptureConnection], error: Error?) {
            if error != nil {
                print("Error recording movie: \(error!.localizedDescription)")
            } else {
                processMovie()
            }
        }
    
        func processMovie() {
            let asset = AVAsset(url: CameraViewController.movieURL)
            let composition = AVMutableComposition()
            let assetVideoTrack = asset.tracks(withMediaType: .video).last!
            let compositionVideoTrack = composition.addMutableTrack(withMediaType: AVMediaType.video,
                                                                    preferredTrackID: CMPersistentTrackID(kCMPersistentTrackID_Invalid))
            try? compositionVideoTrack?.insertTimeRange(CMTimeRangeMake(start: CMTime.zero, duration: asset.duration),
                                                        of: assetVideoTrack,
                                                        at: CMTime.zero)
            if self.currentCameraPosition == .rear {
                compositionVideoTrack?.preferredTransform = assetVideoTrack.preferredTransform
            }
            if self.currentCameraPosition == .front {
                compositionVideoTrack?.preferredTransform = CGAffineTransform(scaleX: -1.0, y: 1.0).rotated(by: CGFloat(Double.pi/2))
            }
    
            if let exporter = AVAssetExportSession(asset: composition,
                                                   presetName: AVAssetExportPresetHighestQuality) {
                exporter.outputURL = CameraViewController.exportMovieURL
                exporter.outputFileType = AVFileType.mov
                exporter.shouldOptimizeForNetworkUse = true
                exporter.exportAsynchronously() {
                    DispatchQueue.main.async {
                        self.performSegue(withIdentifier: "ShowVideo", sender: nil)
                    }
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多