AVMetadataItem其實就是一個模型類, 主要是用來保存音視頻等多媒體資料的各種附加信息。稱之為元數(shù)據(jù)。
例如:作者, 標題, 創(chuàng)建時間, 封面等描述關(guān)于這個多媒體的一些常見描述信息。
在分析這個之前,大家可以下載一個元數(shù)據(jù)分析工具,對一個多媒體文件進行詳細的分析, 有助于我們學習AVFoundation, 大家可以去蘋果工具下載中心 搜索Atom Inspector 進行下載
大家也可以到AVFoundation(二):核心AVAsset先去了解一下AVFoundation核心類,詳細介紹了Atom Inspector的用法 這里主要介紹AVMetadataItem
- AVMetadataItem獲取
AVMetadataItem還有一個子類AVMutableMetadataItem, 他們兩主要的區(qū)別就是一個不可變, 一個可變的, AVMetadataItem的屬性基本都是只讀(readonly)的, 而AVMutableMetadataItem的屬性是可讀可寫(readwrite)。
AVMutableMetadataItem 除了常規(guī)的init的方法創(chuàng)建還可以通過+ (AVMutableMetadataItem *)metadataItem; 這個類方法創(chuàng)建。
AVMetadataItem: 這個一般不自己創(chuàng)建而是從AVAssetTrack 和 AVAsset獲取到AVMetadataItem數(shù)組, 一個AVMetadataItem對象存放著一個信息。
NSURL *assetUrl = [[NSBundle mainBundle] URLForResource:@"Hubblecast" withExtension:@"mov"];
AVAsset *videoAsset = [AVAsset assetWithURL:assetUrl];
NSArray *keys = @[@"tracks", @"availableMetadataFormats"];
[self obtainAVMetadataItem:videoAsset];
[videoAsset loadValuesAsynchronouslyForKeys:keys completionHandler:^{
NSError *error = nil;
//獲取加載tracks的狀態(tài), 作出相應的操作
AVKeyValueStatus status = [videoAsset statusOfValueForKey:@"tracks" error:&error];
switch (status) {
case AVKeyValueStatusUnknown:
NSLog(@"AVKeyValueStatusUnknown");
//加載tracks未知錯誤
break;
case AVKeyValueStatusFailed:
NSLog(@"AVKeyValueStatusFailed");
//加載tracks失敗
break;
case AVKeyValueStatusLoading:
NSLog(@"AVKeyValueStatusLoading");
//加載tracks中
break;
case AVKeyValueStatusLoaded:
NSLog(@"AVKeyValueStatusLoaded");
//加載tracks完畢
break;
case AVKeyValueStatusCancelled:
NSLog(@"AVKeyValueStatusCancelled");
//取消加載tracks
break;
}
status = [videoAsset statusOfValueForKey:@"availableMetadataFormats" error:&error];
switch (status) {
case AVKeyValueStatusUnknown:
NSLog(@"AVKeyValueStatusUnknown");
//加載AVKeyValueStatusUnknown未知錯誤
break;
case AVKeyValueStatusFailed:
NSLog(@"AVKeyValueStatusFailed");
//加載AVKeyValueStatusUnknown失敗
break;
case AVKeyValueStatusLoading:
NSLog(@"AVKeyValueStatusLoading");
//加載AVKeyValueStatusUnknown中
break;
case AVKeyValueStatusLoaded:
{
NSLog(@"AVKeyValueStatusLoaded");
//加載AVKeyValueStatusUnknown完畢
//獲取videoAsset里面的元數(shù)據(jù)
NSMutableArray *metadata = [NSMutableArray array];
for (NSString *format in videoAsset.availableMetadataFormats) {
NSLog(@"%@", format);
NSLog(@"%@", [videoAsset metadataForFormat:format]);
[metadata addObject:[videoAsset metadataForFormat:format]];
}
for (AVMetadataItem *item in metadata.firstObject) {
NSLog(@"%@--%@\n",item.key, item.value);
}
[self keySpac:metadata];
break;
}
case AVKeyValueStatusCancelled:
NSLog(@"AVKeyValueStatusCancelled");
//取消加載AVKeyValueStatusUnknown
break;
}
}];
這里要注意的是AVFoundation里面很多類的屬性都是用到時才加載,對于一些加載過程中比較耗時的屬性,遵守了AVAsynchronousKeyValueLoading這個協(xié)議,使用異步加載的方式,防止界面卡頓
loadValuesAsynchronouslyForKeys 和 statusOfValueForKey
使用注意點
loadValuesAsynchronouslyForKeys 中的不管在keys中設置了多少個屬性值, completionHandler只會調(diào)用一次,
所以在 completionHandler中獲取 AVKeyValueStatus狀態(tài)在keys有多個是不準確的, 得每個屬性分開, 一個key對應一個statusOfValueForKey中獲取加載狀態(tài)
- AVMetadataItem分析
我們打印這個視頻的創(chuàng)建時間AVMetadataItem出來看看:
"<AVMetadataItem: 0x6100000139f0, identifier=mdta/com.apple.quicktime.year, keySpace=mdta, key class = __NSCFString, key=com.apple.quicktime.year, commonKey=(null), extendedLanguageTag=en-US, dataType=com.apple.metadata.datatype.UTF-8, time={INVALID}, duration={INVALID}, startDate=(null), extras={\n dataType = 1;\n dataTypeNamespace = \"com.apple.quicktime.mdta\";\n}, value=2013>"
再結(jié)合他的屬性:
/* Indicates the identifier of the metadata item. Publicly defined identifiers are declared in AVMetadataIdentifiers.h. */
@property (nonatomic, readonly, copy, nullable) NSString *identifier NS_AVAILABLE(10_10, 8_0);
/* indicates the IETF BCP 47 (RFC 4646) language identifier of the metadata item; may be nil if no language tag information is available */
@property (nonatomic, readonly, copy, nullable) NSString *extendedLanguageTag NS_AVAILABLE(10_10, 8_0);
/* indicates the locale of the metadata item; may be nil if no locale information is available for the metadata item */
@property (nonatomic, readonly, copy, nullable) NSLocale *locale;
/* indicates the timestamp of the metadata item. */
@property (nonatomic, readonly) CMTime time;
/* indicates the duration of the metadata item */
@property (nonatomic, readonly) CMTime duration NS_AVAILABLE(10_7, 4_2);
/* indicates the data type of the metadata item's value. Publicly defined data types are declared in <CoreMedia/CMMetadata.h> */
@property (nonatomic, readonly, copy, nullable) NSString *dataType NS_AVAILABLE(10_10, 8_0);
/* provides the value of the metadata item */
@property (nonatomic, readonly, copy, nullable) id<NSObject, NSCopying> value;
/* provides a dictionary of the additional attributes */
@property (nonatomic, readonly, copy, nullable) NSDictionary<NSString *, id> *extraAttributes;
可以看出, 每個屬性相對應的值。所以我們可以很直觀的把AVMetadataItem就是我們平時開發(fā)中用到的模型。里面都是鍵值對的存在 。
- 篩選AVMetadataItem
/*!
@method metadataItemsFromArray:withLocale:
@discussion Instead, use metadataItemsFromArray:filteredAndSortedAccordingToPreferredLanguages:.
*/
+ (NSArray<AVMetadataItem *> *)metadataItemsFromArray:(NSArray<AVMetadataItem *> *)metadataItems withLocale:(NSLocale *)locale;
/*!
@method metadataItemsFromArray:withKey:keySpace:
@discussion Instead, use metadataItemsFromArray:filteredByIdentifier:.
*/
+ (NSArray<AVMetadataItem *> *)metadataItemsFromArray:(NSArray<AVMetadataItem *> *)metadataItems withKey:(nullable id)key keySpace:(nullable NSString *)keySpace;
/*!
@method metadataItemsFromArray:filteredAndSortedAccordingToPreferredLanguages:
@abstract Filters an array of AVMetadataItems according to whether their locales match any language identifier in the specified array of preferred languages. The returned array is sorted according to the order of preference of the language each matches.
@param metadataItems
An array of AVMetadataItems to be filtered and sorted.
@param preferredLanguages
An array of language identifiers in order of preference, each of which is an IETF BCP 47 (RFC 4646) language identifier. Use +[NSLocale preferredLanguages] to obtain the user's list of preferred languages.
@result An instance of NSArray containing metadata items of the specified NSArray that match a preferred language, sorted according to the order of preference of the language each matches.
*/
+ (NSArray<AVMetadataItem *> *)metadataItemsFromArray:(NSArray<AVMetadataItem *> *)metadataItems filteredAndSortedAccordingToPreferredLanguages:(NSArray<NSString *> *)preferredLanguages NS_AVAILABLE(10_8, 6_0);
/*!
@method metadataItemsFromArray:filteredByIdentifier:
@abstract Filters an array of AVMetadataItems according to identifier.
@param metadataItems
An array of AVMetadataItems to be filtered by identifier.
@param identifier
The identifier that must be matched for a metadata item to be copied to the output array. Items are considered a match not only when their identifiers are equal to the specified identifier, and also when their identifiers conform to the specified identifier.
@result An instance of NSArray containing the metadata items of the target NSArray that match the specified identifier.
*/
+ (NSArray<AVMetadataItem *> *)metadataItemsFromArray:(NSArray<AVMetadataItem *> *)metadataItems filteredByIdentifier:(NSString *)identifier NS_AVAILABLE(10_10, 8_0);
/*!
@method metadataItemsFromArray:filteredByMetadataItemFilter:
@abstract Filters an array of AVMetadataItems using the supplied AVMetadataItemFilter.
@param metadataItems
An array of AVMetadataItems to be filtered.
@param metadataItemFilter
The AVMetadataItemFilter object for filtering the metadataItems.
@result An instance of NSArray containing the metadata items of the target NSArray that have not been removed by metadataItemFilter.
*/
+ (NSArray<AVMetadataItem *> *)metadataItemsFromArray:(NSArray<AVMetadataItem *> *)metadataItems filteredByMetadataItemFilter:(AVMetadataItemFilter *)metadataItemFilter NS_AVAILABLE(10_9, 7_0);
里面有好幾個方法進行篩選的, 傳進一個篩選前的AVMetadataItem數(shù)組進去,根據(jù)identifier, preferredLanguages進行篩選, 這兩個比較簡單。
而 根據(jù)key和keySpace的話就要注意文件的類型了
這里的keySpace是根據(jù)文件的類型來的
1、 iTunes: iTunes 包括 Audio/Video 文件格式一般有:.mp4, .m4v, .m4a; 對應的 keySpace 是 AVMetadataKeySpaceiTunes
2、 Quicktime Metadata 包括 Quicktime movie 格式有: .mov 對應的 keySpace 是 AVMetadataKeySpaceQuickTimeMetadata
3、 Quicktime User Dara 包括 Quicktime movie 格式有: .mov 對應的 keySpace 是 AVMetadataKeySpaceQuickTimeUserData
4、 ID3 包括 MPEG Layer III 格式有:.mp3 對應的 keySpace 是 AVMetadataKeySpaceID3
5、其中Common類型包括了 iTunes, Quicktime Metadata, Quicktime User Dara, ID3等等 是一個匯總 對應的 keySpace 是AVMetadataKeySpaceCommon
根據(jù)相對應進去點進去 keySpace 底下就是 key, 根據(jù)key獲取作者,標題,子標題, 創(chuàng)建時間, 時長等信息 都在AVMetadataFormat.h 中
所以一般我們用 AVMetadataKeySpaceCommon 和 AVMetadataKeySpaceCommon底下的key能滿足我們的需求了