Swift - How to get video dimension?

懵懂的女人 提交于 2020-08-01 05:08:30

问题


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,

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?


回答1:


Try this:

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

Hope it helps



来源:https://stackoverflow.com/questions/44879386/swift-how-to-get-video-dimension

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!