2012-05-03 200 views
16

这很令人沮丧。我试图获得AVURLasset的大小,但是尽量避免使用naturalSize,因为Xcode告诉我,这在iOS5中不推荐使用。AVURLAsset获取视频尺寸

但是:什么是替代?

我无法找到如何让视频尺寸,而不使用«naturalsize»任何线索......

回答

20

我只是检查了在线文档和naturalSize方法已经过时了AVAsset对象。但是,始终应该有一个指向AVAsset的AVAssetTrack,而AVAssetTrack有一个可以调用的方法。

naturalSize

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

@属性(非原子,只读)CGSize naturalSize

状况

可用在IOS 4.0和更高。宣布AVAssetTrack.h

途经:AVAssetTrack Reference for iOS

+0

'nautralSize'在iOS的弃用5 – Raptor

+0

刚刚检查了答案中链接中的文档,并且它不被弃用,它在ObjC和Swift –

+14

中均可用,文档中有2个'naturalSize'。 'AVURLAsset.naturalSize'已被弃用,但是[[[[[[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] naturalSize]'不是 – Raptor

24

deprecation warning on the official documentation提示, “使用naturalSizepreferredTransform酌情的资产的视频跟踪,而不是(也见tracksWithMediaType:)。”

CGSize size = [movieAsset naturalSize]; 

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

它不太漂亮,不太安全,但现在,当他们放下该方法不会破坏:

我从我的代码更改。

13

弃用警告说:

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

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

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

资产显然是你的AVAsset

+1

嗯,应用你所建议的转换结果会给我一个负宽度:( – Goz

+4

@Goz只需使用'fabsf()'标准化尺寸组件。 –

15

分辨率斯威夫特:

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

解决方案,而preferredTransform没有为最新的设备有些视频返回正确的值!

+0

Hi @Avt可以使用一些关于导出视频的建议,如果你不介意的话。你能帮助这些问题吗?谢谢你的时间!(1)http://stackoverflow.com/questions/34937862/merge-videos-images-in-avmutablecomposition-using-avmutablecompositiontrack(2)http:// stackoverflow。 com/questions/34938904/exported-video-not-at-same-size-as-source-video – Crashalot

1

以得出AVAsset的尺寸,你应该计算所有的视觉轨迹rects联盟(应用其对应的优先改造后):

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; 

依靠CGSizeApplyAffineTransform当你的资产中包含的轨道失败的方法(例如45度旋转),或者如果您的资产包含具有不同原点的曲目(例如,两个曲目并排播放,而第二个曲目的起点由第一个曲目的宽度增大)。

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

+0

Altho David,对于包含一个视频轨道的AVAsset,@Avt答案能正常工作,对吗? –

+1

@RoiMulia:我认为它是不常见的资产有一个单一的视频轨道与非零偏移量,因此,你可能不会看到使用该代码时的错误,而且,我看到球员忽略资产只包含一个轨道的偏移量。你关心手柄在这些边缘案例中,我建议您尝试自己来验证实际行为。 –

0

这是一个相当简单的扩展AVAsset斯威夫特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 
    } 
} 
+0

谢谢 - 应用程序是用Objective-C编写的,尽管(我还没有改写它)。我会检查一下,我是否可以调整你的分机号码...... – Swissdude