Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mooshee/6b9e35c53047373568f5 to your computer and use it in GitHub Desktop.
Save mooshee/6b9e35c53047373568f5 to your computer and use it in GitHub Desktop.
extension AVAsset {
func videoOrientation() -> (orientation: UIInterfaceOrientation, device: AVCaptureDevicePosition) {
var orientation: UIInterfaceOrientation = .Unknown
var device: AVCaptureDevicePosition = .Unspecified
let tracks :[AVAssetTrack] = self.tracksWithMediaType(AVMediaTypeVideo)
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)
}
}
@wdcurry
Copy link

wdcurry commented Feb 12, 2018

I updated this for current Swift (as at feb12-2017) and added a simple isPortrait bool to simply coding at my end.

https://gist.github.com/wdcurry/433dd1db8f315cf6edd9ff8d2ee6f411

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment