             }
         }
         
-        let connection = videoOutput.connection(with: .video)
-        if (connection?.isVideoOrientationSupported)! {
-            connection?.videoOrientation = currentVideoOrientation()
-        }
-        
-        videoOutput.startRecording(to: outputURL, recordingDelegate: self)
+        checkOrientation { [weak self] orientation in
+            guard let strongSelf = self else {
+                return
+            }
+            if let connection = strongSelf.videoOutput.connection(with: .video) {
+                if let orientation = orientation, connection.isVideoOrientationSupported {
+                    connection.videoOrientation = orientation
+                }
+                strongSelf.videoOutput.startRecording(to: outputURL, recordingDelegate: strongSelf)
+            }
+        }
     }
     
     public func stopRecording() {

    }
    
    // MARK: - Orientation
    
    func currentVideoOrientation() -> AVCaptureVideoOrientation {
        var orientation: AVCaptureVideoOrientation
        switch UIDevice.current.orientation {
        case .portrait:
            orientation = .portrait
        case .landscapeRight:
            orientation = .landscapeLeft
        case .portraitUpsideDown:
            orientation = .portraitUpsideDown
        default:
            orientation = .landscapeRight
        }
        return orientation
    }

    // MARK: - Preview


import UIKit
import AVFoundation
import CoreMotion

/// Abstracts Low Level AVFoudation details.
class YPVideoHelper: NSObject {
