xcode AVURLAsset 获取视频大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10433774/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 04:24:32  来源:igfitidea点击:

AVURLAsset getting video size

xcodeios5avurlasset

提问by Swissdude

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

这很令人沮丧。我正在尝试获取 AVURLasset 的大小,但尽量避免,naturalSize因为 Xcode 告诉我,这在 iOS5 中已弃用。

But: What's the replacement?

但是:什么是替代品?

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

我找不到任何关于如何在不使用 ?naturalsize? 的情况下获取视频尺寸的线索...

回答by Avt

Resolution in Swift 3:

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:

对于 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 preferredTransformdo not return correct values for some videos on the latest devices!

没有的解决方案不会preferredTransform为最新设备上的某些视频返回正确的值!

回答by C4 - Travis

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

我刚刚在网上查看了文档,该naturalSize方法已不推荐用于 AVAsset 对象。但是,应该始终有一个 AVAssetTrack 引用 AVAsset,并且 AVAssetTrack 有一个naturalSize您可以调用的方法。

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

自然尺寸

轨道引用的媒体数据的自然尺寸。(只读)

@property(nonatomic, readonly) CGSize naturalSize

可用性

在 iOS 4.0 及更高版本中可用。在 AVAssetTrack.h 中声明

Via: AVAssetTrack Reference for iOS

通过:适用于 iOS 的 AVAssetTrack 参考

回答by Dave Burt

The deprecation warning on the official documentationsuggests, "Use the naturalSizeand preferredTransform, as appropriate, of the asset's video tracks instead (see also tracksWithMediaType:)."

官方文件弃用警告提示,“使用naturalSize,并preferredTransform酌情资产的视频轨道,而不是(也见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.

它现在不那么漂亮和安全了,但是当他们放弃那个方法时不会坏。

回答by dloomb

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 naturalSizeand preferredTransform. This can be accessed with the following:

因此,我们需要一个AVAssetTrack,我们希望它naturalSizepreferredTransform。这可以通过以下方式访问:

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

assetis obviously your AVAsset.

资产显然是您的AVAsset

回答by CodeBender

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

这是一个相当简单的扩展,用于AVAssetSwift 4中获取视频的大小(如果可用):

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

回答by David H

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

要导出 an 的维度AVAsset,您应该计算所有视觉轨迹矩形的并集(在应用它们相应的首选变换之后):

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 CGSizeApplyAffineTransformfail 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).

依赖方法CGSizeApplyAffineTransform失败时,您的资产包含了不平凡的仿射变换轨道(例如,45个旋转),或者如果您的资产包含不同来源(例如,两个曲目演奏并排侧与第二轨道的原点通过增加轨道第一条轨道的宽度)。

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

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