Getting wrong video dimensions from AVAsset

71 Views Asked by At

For editing purposes I want to use the dimensions of a video stored on an iOS device. When I use a video that has dimensions 768x768 (I recorded it that way and verified the dimensions in the iOS Media Library), I'm getting 480x480.

Here is the code I used:

func getVideoResolution(url: URL) async throws -> CGSize? {
    guard let track = try await AVURLAsset(url: url).loadTracks(withMediaType: AVMediaType.video).first else { return nil }
    let size = try await track.load(.naturalSize).applying(track.load(.preferredTransform))
    return size
}

I also tried a different approach using:

func checkVideoDim(videoURL: URL) -> CGSize {
    let videoAsset = AVAsset.init(url: videoURL)

    let generator = AVAssetImageGenerator.init(asset: videoAsset)
    let imageRef: CGImage!

    do {
        try imageRef = generator.copyCGImage(at: CMTime.zero, actualTime: nil)

        let thumbn = UIImage.init(cgImage: imageRef)
        
        return thumbn.size
    } catch let error as NSError {
        print("Failed to check video")
        print(error.localizedDescription)
    }
    
    return CGSize(width: 0, height: 0)
}

Both functions give me 480x480 where they should give 768x768. A user of my app told me about the problem using a totally different video file, so I assume this is not file specific. What am I missing?

0

There are 0 best solutions below