cómo cambiar la orientación de AVCaptureMovieFileOutput en swift
swift2 avfoundation (1)
Intenté varios métodos diferentes pero no me ayudaron. Quiero cambiar la orientación del video en AVFoundation. ¿Cómo puedo hacerlo?
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)
}
}
Lo intenté para encontrar resultados, pero no sé cómo cambiar la orientación
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
}
}
}
Cambio mi código y funciona para mí
@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
}