Video Saving in the wrong orientation AVCaptureSession

后端 未结 4 991
情书的邮戳
情书的邮戳 2020-12-31 07:13

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 UIViewCo

相关标签:
4条回答
  • 2020-12-31 07:43

    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;
        }
    }
    
    0 讨论(0)
  • 2020-12-31 07:55

    **

    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 讨论(0)
  • 2020-12-31 07:56

    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

    0 讨论(0)
  • 2020-12-31 07:57

    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 讨论(0)
提交回复
热议问题