Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Video Saving in the wrong orientation AVCaptureSession

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];
like image 521
Destiny Dawn Avatar asked Mar 22 '13 21:03

Destiny Dawn


2 Answers

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];
like image 67
Destiny Dawn Avatar answered Nov 12 '22 02:11

Destiny Dawn


**

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!
like image 11
Abdul Yasin Avatar answered Nov 12 '22 02:11

Abdul Yasin