Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Landscape Videos Rotated #161

Closed
Appswage opened this issue Jun 15, 2018 · 8 comments
Closed

Landscape Videos Rotated #161

Appswage opened this issue Jun 15, 2018 · 8 comments

Comments

@Appswage
Copy link

It seems all videos that are taken "live" from the library come out in portrait orientation. If a video is taken landscape, it is rotated and sideways for editing and output.

@Appswage
Copy link
Author

I've solved this issue by adding the following in YPVideoHelper

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

Add the connection bit.

public func startRecording() {
        let outputPath = "\(NSTemporaryDirectory())output.mov"
        let outputURL = URL(fileURLWithPath: outputPath)
        let fileManager = FileManager.default
        if fileManager.fileExists(atPath: outputPath) {
            do {
                try fileManager.removeItem(atPath: outputPath)
            } catch {
                return
            }
        }
        
        let connection = videoOutput.connection(with: AVMediaType.video)
        if (connection?.isVideoOrientationSupported)! {
            connection?.videoOrientation = currentVideoOrientation()
        }
        
        videoOutput.startRecording(to: outputURL, recordingDelegate: self)
    }
    
    public func stopRecording() {
        videoOutput.stopRecording()
    }
    

@s4cha
Copy link
Member

s4cha commented Jun 19, 2018

@Appswage Your fix is in the pipe here: #163

I want to say your help as been very precious lately.
And as a maintainer, you gotta love the issues that come with a working fix 👍
Feel free to create Pull requests in the future 🎉
Have an awesome day !

@s4cha
Copy link
Member

s4cha commented Jun 25, 2018

@Appswage This is live as of 3.3.0 :) https:/Yummypets/YPImagePicker/releases/tag/3.3.0

@Appswage
Copy link
Author

@s4cha - Awesome, thanks for being so responsive!

@Appswage
Copy link
Author

@s4cha - I need to make an update to the above code. I found that if the phone is portrait locked from settings, the video would not be properly oriented if taken in landscape mode. I'd suggest replacing my orientation function with this one which can still detect orientation when portrait locked. This uses CoreMotion which of course needs to be imported at the top of class. Also add a property called motionManager of type CMMotionManager.

private func checkOrientation(completion: @escaping(_ orientation:AVCaptureVideoOrientation?)->()) 
 {
     self.motionManager = CMMotionManager()
     self.motionManager.accelerometerUpdateInterval = 0.2
     
     self.motionManager.startAccelerometerUpdates( to: OperationQueue() ) { data, _ in
         guard data != nil else {completion(nil);return}
         let data = data!

         let orientation = abs(data.acceleration.y) < abs(data.acceleration.x)
             ?   data.acceleration.x > 0 ? AVCaptureVideoOrientation.landscapeLeft      :  AVCaptureVideoOrientation.landscapeRight
             :   data.acceleration.y > 0 ? AVCaptureVideoOrientation.portraitUpsideDown  :   AVCaptureVideoOrientation.portrait
         
         completion(orientation)
     }
 }
public func startRecording() {
        let outputPath = "\(NSTemporaryDirectory())output.mov"
        let outputURL = URL(fileURLWithPath: outputPath)
        let fileManager = FileManager.default
        if fileManager.fileExists(atPath: outputPath) {
            do {
                try fileManager.removeItem(atPath: outputPath)
            } catch {
                return
            }
        }
        
        checkOrientation { (orientation) in
            self.motionManager.stopAccelerometerUpdates()
            guard orientation != nil else {print("Orientation Error");return}
            let connection = self.videoOutput.connection(with: AVMediaType.video) 
            if (connection?.isVideoOrientationSupported)! {
                connection?.videoOrientation = orientation!
                self.videoOutput.startRecording(to: outputURL, recordingDelegate: self)
            }
        }
    }


@Appswage Appswage reopened this Jul 11, 2018
@s4cha
Copy link
Member

s4cha commented Jul 20, 2018

@Appswage Your update is on point 👌. This looks like a micro case, but still, nice catch!

I implemented it here with few amends and it works perfectly :
bd28c7c
This will probably be live later today.

PS: Excuse the delayed response, we won the soccer World Cup so I was away for a while 🇫🇷 🍻 :)

Totally unrelated, I think I saw your profile on the top iOS dev in upwork, congrats!! 👏 Have you used the picker on clients' apps? If so would you be willing to communicate their name in the App Store so we can build a list of Apps that use it ?

Cheers!

@s4cha
Copy link
Member

s4cha commented Jul 20, 2018

This should be ok in 3.4.0 https:/Yummypets/YPImagePicker/releases/tag/3.4.0 :)

@s4cha
Copy link
Member

s4cha commented Jul 24, 2018

Closing this for now, don't hesitate to reopen if needed :)

@s4cha s4cha closed this as completed Jul 24, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants