Video Saving in the wrong orientation AVCaptureSession

**

Try changing its orientation just before starting recording.

**

    if let videoConnection = fileOutput.connection(with: .video) {

    let newOrientation: AVCaptureVideoOrientation
    switch UIDevice.current.orientation {
    case .portrait:
        newOrientation = .portrait
    case .portraitUpsideDown:
        newOrientation = .portraitUpsideDown
    case .landscapeLeft:
        newOrientation = .landscapeRight
    case .landscapeRight:
        newOrientation = .landscapeLeft
    default :
        newOrientation = .portrait
    }
    videoConnection.videoOrientation = newOrientation
    self.fileOutput.startRecording(to: URL(fileURLWithPath: filePath), recordingDelegate: self)
}

where,

var fileOutput : AVCaptureMovieFileOutput!

It turns out you have to add the connections' orientation to the AVCaptureMovieFileOutput after it is added to the session.

session = [[AVCaptureSession alloc] init];
[session beginConfiguration];
session.sessionPreset = AVCaptureSessionPresetHigh;


AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
for (AVCaptureDevice *cam in [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo])
{
    if (cam.position == AVCaptureDevicePositionFront)
        device = cam;
}

NSError *error = nil;
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
if (!input) {
    // Handle the error appropriately.
    NSLog(@"ERROR: trying to open camera: %@", error);
}

AVCaptureDevice *audioDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
AVCaptureDeviceInput * audioInput = [AVCaptureDeviceInput deviceInputWithDevice:audioDevice error:nil];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];


NSString *outputpathofmovie = [[documentsDirectoryPath stringByAppendingPathComponent:@"RecordedVideo"] stringByAppendingString:@".mp4"];
outputURL = [[NSURL alloc] initFileURLWithPath:outputpathofmovie];

[self deleteTempVideos];

[session addInput:input];
[session addInput:audioInput];
[session commitConfiguration];
[session startRunning];

movieFileOutput = [[AVCaptureMovieFileOutput alloc] init];
[session addOutput:movieFileOutput];

AVCaptureConnection *videoConnection = nil;

for ( AVCaptureConnection *connection in [movieFileOutput connections] )
{
    NSLog(@"%@", connection);
    for ( AVCaptureInputPort *port in [connection inputPorts] )
    {
        NSLog(@"%@", port);
        if ( [[port mediaType] isEqual:AVMediaTypeVideo] )
        {
            videoConnection = connection;
        }
    }
}

if([videoConnection isVideoOrientationSupported]) // **Here it is, its always false**
{
    [videoConnection setVideoOrientation:[[UIDevice currentDevice] orientation]];
}

[movieFileOutput startRecordingToOutputFileURL:outputURL recordingDelegate:self];

Try something like this:

#import <AVFoundation/AVFoundation.h>

AVCaptureConnection *captureConnection = <#A capture connection#>;
if ([captureConnection isVideoOrientationSupported])
{
    AVCaptureVideoOrientation orientation = AVCaptureVideoOrientationLandscapeLeft;
    [captureConnection setVideoOrientation:orientation];
}

http://developer.apple.com/library/ios/#qa/qa1744/_index.html#//apple_ref/doc/uid/DTS40011134