如何在 swift 中更改 AVCaptureMovieFileOutput 的方向

how to change orientation for AVCaptureMovieFileOutput in swift

我尝试了几种不同的方法,但它们对我没有帮助。我想更改 AVFoundation 中的视频方向。我怎样才能做到?

    override func viewDidLoad() {
    super.viewDidLoad()
    self.definesPresentationContext = true

    // device capture for audio and video
    let captureVideo = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
    let captureAudio = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeAudio)

    // input
    let audioInput = try! AVCaptureDeviceInput(device: captureAudio)
    let videoInput = try! AVCaptureDeviceInput(device: captureVideo)

    let capturePreview = AVCaptureVideoPreviewLayer(session: captureSession)
    capturePreview.frame = self.view.frame

    capturePreview.videoGravity = AVLayerVideoGravityResizeAspect
    self.view.layer.addSublayer(capturePreview)

    // setting of session

    captureSession.beginConfiguration()

    if captureSession.canAddInput(audioInput) {
        captureSession.addInput(audioInput)
    }
    if captureSession.canAddInput(videoInput) {
        captureSession.addInput(videoInput)
    }

    // output
    movieOutput.movieFragmentInterval = kCMTimeInvalid
    if captureSession.canAddOutput(movieOutput) {
        captureSession.addOutput(movieOutput)
        print("added moview")
    }

    captureSession.sessionPreset = AVCaptureSessionPresetHigh

    captureSession.commitConfiguration()

    captureSession.startRunning()
}


 @IBAction func startStopSession(sender: UIBarButtonItem) {
    if movieOutput.recording {
        movieOutput.stopRecording()
    } else {
        print("start recording")
        captureSession.beginConfiguration()

        for connection in movieOutput.connections as! [AVCaptureConnection] {
            for port in connection.inputPorts as! [AVCaptureInputPort] {
                print(port)
                if port.mediaType == AVMediaTypeVideo {
                    print(port)
                    self.captureConnection = AVCaptureConnection(inputPorts: [port], output: movieOutput)
                    if self.captureConnection.supportsVideoOrientation {
                        self.captureConnection.videoOrientation = AVCaptureVideoOrientation.LandscapeRight
                        print("video orientation right")
                    }
                }
            }
        }

        if self.captureConnection.supportsVideoStabilization {
            captureConnection.preferredVideoStabilizationMode = .Cinematic
            print("true video stabilization")
        }

        let digit = returnFileDigit()
        let path = fileManager.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).last!.path!.stringByAppendingString("/movie-\(digit).mp4")

        captureSession.commitConfiguration()
        let url = NSURL(fileURLWithPath: path)
        print(movieOutput.connections)
        movieOutput.startRecordingToOutputFileURL(url, recordingDelegate: self)
    }
}

我试过了,我可以找到输出,但我不知道如何改变方向

  for output in captureSession.outputs as! [AVCaptureOutput] {
        output.connections.first?.mediaType
        for var connection in output.connections  {
            if connection.mediaType == AVMediaTypeVideo {
                print(connection.mediaType)
                connection.videoOrientation = .LandscapeRight
            }
        }
    }

我更改了我的代码,它对我有用

    @IBAction func startStopSession(sender: UIBarButtonItem) {
    if movieOutput.recording {
        movieOutput.stopRecording()
    } else {
        print("start recording")
        movieOutput.connectionWithMediaType(AVMediaTypeVideo).videoOrientation = returnedOrientation()

        if movieOutput.connectionWithMediaType(AVMediaTypeVideo).supportsVideoStabilization {
            movieOutput.connectionWithMediaType(AVMediaTypeVideo).preferredVideoStabilizationMode = .Cinematic
        }

        let digit = returnFileDigit()
        let path = fileManager.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).last!.path!.stringByAppendingString("/movie-\(digit).mp4")

        let url = NSURL(fileURLWithPath: path)
        movieOutput.startRecordingToOutputFileURL(url, recordingDelegate: self)
    }
}
func returnedOrientation() -> AVCaptureVideoOrientation {
    var videoOrientation: AVCaptureVideoOrientation!
    let orientation = UIDevice.currentDevice().orientation

    switch orientation {
    case .Portrait:
        videoOrientation = .Portrait
        userDefault.setInteger(0, forKey: "CaptureVideoOrientation")
    case .PortraitUpsideDown:
        videoOrientation = .PortraitUpsideDown
        userDefault.setInteger(1, forKey: "CaptureVideoOrientation")
    case .LandscapeLeft:
        videoOrientation = .LandscapeRight
        userDefault.setInteger(2, forKey: "CaptureVideoOrientation")
    case .LandscapeRight:
        videoOrientation = .LandscapeLeft
        userDefault.setInteger(3, forKey: "CaptureVideoOrientation")
    case .FaceDown, .FaceUp, .Unknown:
        let digit = userDefault.integerForKey("CaptureVideoOrientation")
        videoOrientation = AVCaptureVideoOrientation.init(rawValue: digit)
    }
    return videoOrientation
}