Skip to content

Instantly share code, notes, and snippets.

@Denismih
Created February 4, 2020 08:10
Show Gist options
  • Save Denismih/a1a193528241d0771cbfa1ea0e663645 to your computer and use it in GitHub Desktop.
Save Denismih/a1a193528241d0771cbfa1ea0e663645 to your computer and use it in GitHub Desktop.
getVideoResolution
var mediaAspectRatio: Double! // <- here will be set aspect ratio for video with url
func initAspectRatioOfVideo(with fileURL: URL) {
let resolution = resolutionForLocalVideo(url: fileURL)
guard let width = resolution?.width, let height = resolution?.height else {
return
}
mediaAspectRatio = Double(height / width)
}
private func resolutionForLocalVideo(url: URL) -> CGSize? {
guard let track = AVURLAsset(url: url).tracks(withMediaType: AVMediaTypeVideo).first else { return nil }
let size = track.naturalSize.applying(track.preferredTransform)
return CGSize(width: fabs(size.width), height: fabs(size.height))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment