本文介绍了如何从iOS上的音频文件中提取元数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用AVFoundation框架从mp3和m4a文件中提取元数据。

I'm trying to extract metadata from mp3 and m4a files using the AVFoundation framework.

这是测试代码:

+ (void)printMetadataForFileAtPath:(NSString *)path
{
    NSURL *url = [NSURL fileURLWithPath:path];
    AVAsset *asset = [AVURLAsset assetWithURL:url];
    NSArray *availableFormats = [asset availableMetadataFormats];
    NSLog(@"Available formats: %@", availableFormats);
    NSArray *iTunesMetadata = [asset metadataForFormat:AVMetadataFormatiTunesMetadata];
    for (AVMetadataItem *item in iTunesMetadata)
    {
        NSLog(@"%@ %x", item.key, [(NSNumber *)item.key integerValue]);

        if ([item.key isEqual:AVMetadataiTunesMetadataKeySongName])
        {
            NSLog(@"FOUND song name: %@", item.stringValue);
        }
    }
    NSLog(@"====================");
    NSLog(@"%@ %@ %@", AVMetadataiTunesMetadataKeySongName, AVMetadataiTunesMetadataKeyAlbum, AVMetadataiTunesMetadataKeyArtist);
}

这是输出:

Available formats: (
    "com.apple.itunes",
    "com.apple.quicktime.udta"
)
-1452383891 a96e616d
-1455336876 a9415254
1631670868 61415254
-1451789708 a9777274
-1453233054 a9616c62
-1452838288 a9677270
-1452841618 a967656e
1953655662 74726b6e
1684632427 6469736b
-1453039239 a9646179
-1453101708 a9636d74
1668311404 6370696c
1885823344 70676170
1953329263 746d706f
-1451987089 a9746f6f
com.apple.iTunes.iTun
com.apple.iTunes.Enco
1668249202 636f7672
-1452508814 a96c7972
====================
@nam @alb @ART

当解释为4个ASCII字符时:

When interpreted as 4 ASCII chars:

© n a m
© A R T
a A R T
© w r t
© a l b
© g r p
© g e n
t r k n
d i s k
© d a y
© c m t
c p i l
p g a p
t m p o
© t o o

所以似乎 item.key NSNumber 对象,但以 AVMetadataiTunesMetadataKey ... 开头的常量是 NSString 对象。获取元数据的正确方法是什么?当我使用 [AVAsset commonMetadata] 时,键也是 NSString 对象以及与的比较AVMetadataCommonKey ... 常量按预期工作。

So it seems that item.key is a NSNumber object but the constants beginning with AVMetadataiTunesMetadataKey... are NSString objects. What's the right way to get the metadata? When i use [AVAsset commonMetadata] the keys are NSString objects too and the comparison with the AVMetadataCommonKey... constants works as expected.

推荐答案

AVFoundation API在处理元数据时非常混乱。

AVFoundation API is quite confusing when dealing with metadata.

AVMetadataiTunesMetadataKey * 定义的键与AVMetadataItem的键属性值不同。 AVMetadataiTunesMetadataKey *键将与 [AVMetadataItem metadataItemsFromArray:withKey:keySpace:AVMetadataKeySpaceiTunes] API一起使用,以使用特定的iTunes密钥过滤掉元数据项。

The keys defined by AVMetadataiTunesMetadataKey* values are not the same ones as AVMetadataItem's key property values. AVMetadataiTunesMetadataKey* keys are to be used with the [AVMetadataItem metadataItemsFromArray:withKey:keySpace:AVMetadataKeySpaceiTunes] API to filter out metadata items with the specific iTunes keys.

AVMetadataItem的键和值取决于资源文件的确切格式。除了使用上述函数过滤掉键外,我建议使用item的 commonKey 属性,该属性提供比其他属性更通用的key属性。

The keys and values for AVMetadataItem depend on the exact format of the resource file. In addition to filtering out the keys with the function above, I suggest using item's commonKey property, which provides more generic "key" property than the others.

更改示例代码以打印 commonKey 以及项目的
以下是一些示例:

Change your sample code to print out commonKey along with the item's value.Here are some examples:


  • commonKey 是title - value 是一首带有歌曲标题的NSString

  • commonKey 是'艺术家' - 是一个NSString,其艺术家是歌曲

  • commonKey 是'albumName' - 是一张带有专辑名称的NSString

  • commonKey is "title" - value is a NSString with the title of the song
  • commonKey is 'artist' - value is a NSString with the artist of the song
  • commonKey is 'albumName' - value is a NSString with the name of the album

希望这会有所帮助!

这篇关于如何从iOS上的音频文件中提取元数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 08:21