17

I'm trying to record a video (without displaying the camera) and save it. But the video being saved is not saving in the right orientation. I've tried forcing the UIViewController to be a certain orientation but that didn't help. All videos are being recorded in portrait. My code is below:

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];
NSLog(@"%@", movieFileOutput.connections);

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

NSLog(@"%@", videoConnection);

[session addOutput:movieFileOutput];
[movieFileOutput startRecordingToOutputFileURL:outputURL recordingDelegate:self];

4 Answers 4

22

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];
0
11

**

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!
0
2

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

1
  • I've tried that, but the movieFileOutput connections isn't returning anything so I couldn't set the connections. Mar 22, 2013 at 22:03
0

Thank you Destiny Dawn, incase anyone is looking for the code for Xamarin ios:

AVCaptureConnection videoConnection = null;

foreach(AVCaptureConnection connection in _videoOutput.Connections)
{
    foreach ( AVCaptureInputPort port in connection.InputPorts)
    {
        if (port.MediaType == AVMediaType.Video)
        {
            videoConnection = connection;
            break;
        }
    }
}

if (videoConnection != null) {
    if (videoConnection.SupportsVideoOrientation) {
        videoConnection.VideoOrientation = AVCaptureVideoOrientation.LandscapeRight;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.