AVFoundation是一个对多媒体操作的库。多媒体一般以文件或者流的形式存在,显而易见,直接对多媒体进行操作并不是一件愉快的事,这需要我们了解很多底层多媒体方面的知识。AVFoundation为我们提供了一个多媒体的载体类:AVAsset,在AVAsset中有着统一并且友好的接口,我们不需要了解太多多媒体的知识(当然还是需要了解一些的),就能对其进行操作。
基本属性
我们将描述视频基本信息的属性称为基本属性。AVAsset的属性从根本上来说是多媒体文件(如视频文件)的属性,我们先来看看多媒体文件中有哪些属性。用十六进制编辑器打开一个视频文件是最完整的查看视频中信息的方法,不过这样并不利于我们的阅读,因为数据太多了。apple提供了一个很好的查看视频信息的工具Atom Inspector,它会将十六进制的数据归类,并提取出其中有用的信息,即有利于查阅信息,也可以很方便的查看视频完整的16进制,了解视频的结构。
用Atom Inspector打开一个视频文件。
我们可以看到在moov的目录下有一个mvhd,mvhd也称为movie header,它是整个视频的描述部分,里面包含着视频的基本信息,如时长,创建时间等。这些信息就是视频文件的基本属性,他们对应到AVAsset中有:
// Indicates the duration of the asset. If @"providesPreciseDurationAndTiming" is NO, a best-available estimate of the duration is returned. The degree of precision preferred for timing-related properties can be set at initialization time for assets initialized with URLs. See AVURLAssetPreferPreciseDurationAndTimingKey for AVURLAsset below.
@property (nonatomic, readonly) CMTime duration;
// indicates the natural rate at which the asset is to be played; often but not always 1.0
@property (nonatomic, readonly) float preferredRate;
// indicates the preferred volume at which the audible media of an asset is to be played; often but not always 1.0
@property (nonatomic, readonly) float preferredVolume;
// Indicates the creation date of the asset as an AVMetadataItem. May be nil. If a creation date has been stored by the asset in a form that can be converted to an NSDate, the dateValue property of the AVMetadataItem will provide an instance of NSDate. Otherwise the creation date is available only as a string value, via -[AVMetadataItem stringValue].
@property (nonatomic, readonly, nullable) AVMetadataItem *creationDate NS_AVAILABLE(10_8, 5_0);
首先duration
属性是CMTime类型,CMTime是一个结构体
typedef struct
{
CMTimeValue value; // @field value The value of the CMTime. value/timescale = seconds.
CMTimeScale timescale; // @field timescale The timescale of the CMTime. value/timescale = seconds.
CMTimeFlags flags; // @field flags The flags, eg. kCMTimeFlags_Valid, kCMTimeFlags_PositiveInfinity, etc.
CMTimeEpoch epoch; // @field epoch Differentiates between equal timestamps that are actually different because of looping, multi-item sequencing, etc. Will be used during comparison: greater epochs happen after lesser ones. Additions/subtraction is only possible within a single epoch, however, since epoch length may be unk