Swift - How to get video size?

I am working on a video editing application where each video is split into a square so that no part of the video is cropped. For this, in the case of portrait video, it contains a black part on the left and right, and for landscape video, it contains a black part on top and bottom of the video. The black parts are part of the video, they are not for AVPlayerViewController. Here is an example S0NBQ.png

I need to cover these black parts with some CALayers.

What will be the frame ( CGRect ) CALayer?

I get the size of the video using a property naturalSizethat includes black parts.

? ( ) CGRect ?

+4
1

:

var mediaAspectRatio: Double! // <- here will be set aspect ratio for video with url

private func initAspectRatioOfVideo(with fileURL: URL) {
  let resolution = resolutionForLocalVideo(url: fileURL)

  let width = resolution?.width
  let height = resolution?.height

  mediaAspectRatio = Double(height! / width!)
}

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))
} 

,

+6

Source: https://habr.com/ru/post/1680611/


All Articles