Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kimdwkimdw/bdc68dcbbd7b6ca994a99c90dd24a7cb to your computer and use it in GitHub Desktop.
Save kimdwkimdw/bdc68dcbbd7b6ca994a99c90dd24a7cb to your computer and use it in GitHub Desktop.
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