How to extract metadata from audio files on iOS

2020-05-29 05:36发布

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

This is the test code:

+ (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);
}

This is the ouput:

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

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

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.

1条回答
一夜七次
2楼-- · 2020-05-29 05:56

AVFoundation API is quite confusing when dealing with metadata.

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.

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.

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

  • 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

Hope this helps!

查看更多
登录 后发表回答