Reputation: 1513
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?
Upvotes: 17
Views: 19224
Reputation: 3272
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))
}
For visionOS:
guard let track = (try? await AVURLAsset(url: url).loadTracks(withMediaType: AVMediaType.video))?.first,
let (naturalSize, preferredTransform) = try? await videoTrack.load(.naturalSize, .preferredTransform) else { return nil }
let size = naturalSize.applying(preferredTransform)
...
Upvotes: 58
Reputation: 29
~New
func getVideoResolution(url: String) async throws -> CGSize? {
guard let track = try await AVURLAsset(url: URL(string: url)!).loadTracks(withMediaType: AVMediaType.video).first else { return nil }
let size = try await track.load(.naturalSize).applying(track.load(.preferredTransform))
return size
}
Upvotes: 1
Reputation: 947
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)
}
Upvotes: 7