Swift-如何获取视频尺寸? [英] Swift - How to get video dimension?

查看:691
本文介绍了Swift-如何获取视频尺寸?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个视频编辑应用程序,其中每个视频的平方都不会导致视频的任何部分被裁剪.为此,在纵向视频的情况下,左侧和黑色部分均包含黑色部分.右侧,对于风景视频,它在顶部和顶部包含黑色部分视频底部.黑色部分是视频的一部分,不适用于AVPlayerViewController.这是示例

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,

我需要用一些CALayers覆盖这些黑色部分.

I need to cover these black portions with some CALayers.

CALayer的框架( CGRect )是什么?

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

我正在使用naturalSize属性获得视频尺寸,其中包括黑色部分.

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

有没有办法获得没有黑色部分的视频尺寸?(我是指实际视频内容的尺寸)或 有什么方法可以获取视频黑色区域的CGRect?

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?

推荐答案

尝试一下:

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

希望有帮助

这篇关于Swift-如何获取视频尺寸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆