I have a requirement to record a video in mp4 format and send to AWS server. Below is the code used for the same.
lazy var frontCameraDevice: AVCaptureDevice? = {
let devices = AVCaptureDevice.DiscoverySession(deviceTypes: [AVCaptureDevice.DeviceType.builtInDuoCamera, AVCaptureDevice.DeviceType.builtInWideAngleCamera, AVCaptureDevice.DeviceType.builtInTelephotoCamera], mediaType: AVMediaTypeVideo, position: AVCaptureDevice.Position.front).devices
return devices?.filter{$0.position == .front}.first
}()
lazy var micDevice: AVCaptureDevice? = {
return AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeAudio)
}()
var movieOutput = AVCaptureMovieFileOutput()
func record() {
captureSession.beginConfiguration()
captureSession.sessionPreset = AVCaptureSessionPresetHigh
// add device inputs (front camera and mic)
captureSession.addInput(deviceInputFromDevice(device: frontCameraDevice))
captureSession.addInput(deviceInputFromDevice(device: micDevice))
// add output movieFileOutput
movieOutput.movieFragmentInterval = kCMTimeInvalid
captureSession.addOutput(movieOutput)
// start session
captureSession.commitConfiguration()
recordVideoView.backgroundColor = UIColor.clear
videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
videoPreviewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
videoPreviewLayer?.connection.videoOrientation = AVCaptureVideoOrientation.landscapeRight
videoPreviewLayer?.frame = view.layer.bounds
cameraView.layer.addSublayer(videoPreviewLayer!)
captureSession.startRunning()
}
func capture(_ output: AVCaptureFileOutput!, didFinishRecordingToOutputFileAt outputFileURL: URL!, fromConnections connections: [Any]!, error: Error!) {
if (error == nil)
{
transferDetail.videoUrl = outputFileURL
transferDetail.videoTiming = NSNumber(integerLiteral: (15 - countdownValue == 0) ? 1 : 15 - countdownValue)
}
}
The video is recorded in Landscape mode and is saved to a temporary folder. The video is saved in landscape mode and is played in Portrait mode. I want to rotate the video by 90 degree before sending to server. Please provide a swift code to rotate the video.
I have found similar post with Objective C code. Any swift code is highly appreciable.