Alexsander Alexsander - 3 months ago 29
Swift Question

how to change orientation for AVCaptureMovieFileOutput in swift

I tried several different methods but they didn't help me. I want to change video orientation in AVFoundation. How can I make it?

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


I tried it that I can to find outputs but I don't know how change orientation

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

Answer

I change my code and it works for me

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