如何在swift中更改AVCaptureMovieFileOutput的方向

时间:2016-01-03 09:08:48

标签: swift swift2 avfoundation orientation avcapturemoviefileoutput

我尝试了几种不同的方法,但他们没有帮助我。我想在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
            }
        }
    }

1 个答案:

答案 0 :(得分:3)

我改变了我的代码,它适用于我

    @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
}