37

This is pretty frustrating. I'm trying to get the size of an AVURLasset, but try to avoid naturalSize since Xcode tells me, this is deprecated in iOS5.

But: What's the replacement?

I can't find any clue on how to get the video-dimensions without using «naturalsize»...

kelin
  • 11,323
  • 6
  • 67
  • 104
Swissdude
  • 3,486
  • 3
  • 35
  • 68

9 Answers9

53

Resolution in Swift 3:

func resolutionSizeForLocalVideo(url:NSURL) -> CGSize? {
    guard let track = AVAsset(URL: url).tracksWithMediaType(AVMediaTypeVideo).first else { return nil }
    let size = CGSizeApplyAffineTransform(track.naturalSize, track.preferredTransform)
    return CGSize(width: fabs(size.width), height: fabs(size.height))
}

For Swift 4:

func resolutionSizeForLocalVideo(url:NSURL) -> CGSize? {
    guard let track = AVAsset(url: url as URL).tracks(withMediaType: AVMediaType.video).first else { return nil }
    let size = track.naturalSize.applying(track.preferredTransform)
    return CGSize(width: fabs(size.width), height: fabs(size.height))
}

Solutions without preferredTransform do not return correct values for some videos on the latest devices!

Scott McKenzie
  • 16,052
  • 8
  • 45
  • 70
Avt
  • 16,927
  • 4
  • 52
  • 72
34

I just checked the documentation online, and the naturalSize method is deprecated for the AVAsset object. However, there should always be an AVAssetTrack which refers to the AVAsset, and the AVAssetTrack has a naturalSize method that you can call.

naturalSize

The natural dimensions of the media data referenced by the track. (read-only)

@property(nonatomic, readonly) CGSize naturalSize

Availability

Available in iOS 4.0 and later. Declared In AVAssetTrack.h

Via: AVAssetTrack Reference for iOS

C4 - Travis
  • 4,502
  • 4
  • 31
  • 56
  • `nautralSize` is deprecated in iOS 5 – Raptor Oct 15 '14 at 02:59
  • just checked the docs in the link in the answer, and it's not deprecated, it's available in both ObjC and Swift – C4 - Travis Oct 16 '14 at 01:37
  • 15
    there are 2 `naturalSize` in the documentations. `AVURLAsset.naturalSize` is deprecated, but `[[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] naturalSize]` is not – Raptor Oct 16 '14 at 03:04
  • My answer specifies the `AVAssetTrack` approach, which isn't deprecated and which is what Dave specifies in his second line of code below... It appears I just didn't add the line of code to my answer. – C4 - Travis Oct 16 '14 at 17:48
  • Sooo everyone switches to doing basically the same thing as what the deprecated method did. Great. – sudo Aug 20 '16 at 17:14
  • Do you have any idea to change the resolution of selected video? Should I need to use AVAssetExportPreset1280x720? – user2786 Nov 21 '18 at 06:24
26

The deprecation warning on the official documentation suggests, "Use the naturalSize and preferredTransform, as appropriate, of the asset’s video tracks instead (see also tracksWithMediaType:)."

I changed my code from:

CGSize size = [movieAsset naturalSize];

to

CGSize size = [[[movieAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] naturalSize];

It's less pretty and less safe now but won't break when they drop that method.

Dave Burt
  • 1,838
  • 19
  • 20
17

The deprecation warning says:

Use the naturalSize and preferredTransform, as appropriate, 
of the asset’s video tracks instead (see also tracksWithMediaType:).

So we need an AVAssetTrack, and we want its naturalSize and preferredTransform. This can be accessed with the following:

AVAssetTrack *track = [[asset tracksWithMediaType:AVMediaTypeVideo] firstObject];
CGSize dimensions = CGSizeApplyAffineTransform(track.naturalSize, track.preferredTransform);

asset is obviously your AVAsset.

dloomb
  • 1,952
  • 22
  • 26
5

This is a fairly simple extension for AVAsset in Swift 4 to get the size of the video, if available:

extension AVAsset {
    var screenSize: CGSize? {
        if let track = tracks(withMediaType: .video).first {
            let size = __CGSizeApplyAffineTransform(track.naturalSize, track.preferredTransform)
            return CGSize(width: fabs(size.width), height: fabs(size.height))
        } 
        return nil
    }
}
CodeBender
  • 35,668
  • 12
  • 125
  • 132
  • Thanks - the app is written in Objective-C, though (I have yet to rewrite it). I'll check it out whether I can adapt your extension... – Swissdude Jan 21 '18 at 02:20
  • Hi I'm facing the same problem did you find a solution ?? because in my case using the natural size + preferredTransform return an obsolete value :( thank you – sarra.srairi Sep 20 '18 at 10:32
3

To derive the dimension of an AVAsset, you should calculate the union of all the visual track rects (after applying their corresponding preferred transformation):

CGRect unionRect = CGRectZero;
for (AVAssetTrack *track in [asset tracksWithMediaCharacteristic:AVMediaCharacteristicVisual]) {
    CGRect trackRect = CGRectApplyAffineTransform(CGRectMake(0.f,
                                                             0.f,
                                                             track.naturalSize.width,
                                                             track.naturalSize.height),
                                                  track.preferredTransform);
    unionRect = CGRectUnion(unionRect, trackRect);
}
CGSize naturalSize = unionRect.size;

Methods that rely on CGSizeApplyAffineTransform fail when your asset contains tracks with non-trivial affine transformation (e.g., 45 degree rotations) or if your asset contains tracks with different origins (e.g., two tracks playing side-by-side with the second track's origin augmented by the width of the first track).

See: MediaPlayerPrivateAVFoundationCF::sizeChanged()at https://opensource.apple.com/source/WebCore/WebCore-7536.30.2/platform/graphics/avfoundation/cf/MediaPlayerPrivateAVFoundationCF.cpp

David H
  • 2,901
  • 1
  • 26
  • 21
  • Altho David, for AVAsset that contains one Video Track, @Avt answer works properly. Right? – Roi Mulia Aug 31 '17 at 05:06
  • 1
    @RoiMulia: I think it's uncommon for assets to have a single video track with non-zero offset. For that, you may unlikely ever see the bug when using that code. Also, I've seen players that ignore the offset when the asset only contains one track. If you care to about handling these edge cases, I recommend experimenting yourself to verify the actual behavior. – David H Sep 01 '17 at 23:34
3

For Swift 5

let assetSize =  asset.tracks(withMediaType: .video)[0].naturalSize
Olcay Ertaş
  • 5,987
  • 8
  • 76
  • 112
Hope
  • 2,096
  • 3
  • 23
  • 40
2

For iOS versions 15.0 and above,

extension AVAsset {
    func naturalSize() async -> CGSize? {
        guard let tracks = try? await loadTracks(withMediaType: .video) else { return nil }
        guard let track = tracks.first else { return nil }
        guard let size = try? await track.load(.naturalSize) else { return nil }
        return size
    }
}
deeJ
  • 361
  • 4
  • 13
1

Swift version of @David_H answer.

extension AVAsset {
    func resolutionSizeForLocalVideo() -> CGSize {
        var unionRect = CGRect.zero;
        for track in self.tracks(withMediaCharacteristic: .visual) {
            let trackRect = CGRect(x: 0, y: 0, width: 
            track.naturalSize.width, height: 
            track.naturalSize.height).applying(track.preferredTransform)
            unionRect = unionRect.union(trackRect)
            
        }
        return unionRect.size
    }
}

sagarthecoder
  • 137
  • 1
  • 9