Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift - How to get video dimension?

I am working on a video editing app where each video gets squared in such a way that no portion of the video gets cropped.For this, in case of portrait video, it contains black portion on left & right and for landscape video, it contains black portion on top & bottom side of the video. Black portions are part of the video, they are not for AVPlayerViewController. Here is the sample, enter image description here

I need to cover these black portions with some CALayers.

What will be the frame(CGRect) of the CALayer?

I am getting the video dimension with naturalSize property which includes the black portions.

Is there any way to get the video dimension without the black portions?(I mean the dimension of actual video content) or is there any way to get the CGRect of black area of the video?

like image 439
pigeon_39 Avatar asked Jul 03 '17 06:07

pigeon_39


2 Answers

func initAspectRatioOfVideo(with fileURL: URL) -> Double {
  let resolution = resolutionForLocalVideo(url: fileURL)
  guard let width = resolution?.width, let height = resolution?.height else {
     return 0
  }
  return Double(height / width)
}

private func resolutionForLocalVideo(url: URL) -> CGSize? {
    guard let track = AVURLAsset(url: url).tracks(withMediaType: AVMediaType.video).first else { return nil }
   let size = track.naturalSize.applying(track.preferredTransform)
   return CGSize(width: abs(size.width), height: abs(size.height))
}
like image 64
Vlad Pulichev Avatar answered Sep 30 '22 02:09

Vlad Pulichev


This is a more concise version of Vlad Pulichev's answer.

var aspectRatio: CGFloat! // use the function to assign your variable

func getVideoResolution(url: String) -> CGFloat? {
    guard let track = AVURLAsset(url: URL(string: url)!).tracks(withMediaType: AVMediaType.video).first else { return nil }
    let size = track.naturalSize.applying(track.preferredTransform)
    return abs(size.height) / abs(size.width)
}
like image 22
Tadreik Avatar answered Sep 30 '22 03:09

Tadreik