Forked from mooshee/AVAsset Video Orientation and Camera Position.swift
Created
December 10, 2020 09:18
-
-
Save kimdwkimdw/bdc68dcbbd7b6ca994a99c90dd24a7cb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import PhotosUI | |
extension AVAsset { | |
func videoOrientation() -> (orientation: UIInterfaceOrientation, device: AVCaptureDevice.Position) { | |
var orientation: UIInterfaceOrientation = .unknown | |
var device: AVCaptureDevice.Position = .unspecified | |
let tracks :[AVAssetTrack] = self.tracks(withMediaType: AVMediaType.video) | |
if let videoTrack = tracks.first { | |
let t = videoTrack.preferredTransform | |
if (t.a == 0 && t.b == 1.0 && t.d == 0) { | |
orientation = .portrait | |
if t.c == 1.0 { | |
device = .front | |
} else if t.c == -1.0 { | |
device = .back | |
} | |
} | |
else if (t.a == 0 && t.b == -1.0 && t.d == 0) { | |
orientation = .portraitUpsideDown | |
if t.c == -1.0 { | |
device = .front | |
} else if t.c == 1.0 { | |
device = .back | |
} | |
} | |
else if (t.a == 1.0 && t.b == 0 && t.c == 0) { | |
orientation = .landscapeRight | |
if t.d == -1.0 { | |
device = .front | |
} else if t.d == 1.0 { | |
device = .back | |
} | |
} | |
else if (t.a == -1.0 && t.b == 0 && t.c == 0) { | |
orientation = .landscapeLeft | |
if t.d == 1.0 { | |
device = .front | |
} else if t.d == -1.0 { | |
device = .back | |
} | |
} | |
} | |
return (orientation, device) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment