Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract metadata from audio files on iOS

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.

like image 616
Pipo Avatar asked Jul 18 '13 12:07

Pipo


People also ask

Do audio files have metadata?

Music metadata is the information included in audio files used to identify, label and present audio content. Metadata is used by every major device and music streaming application including smartphones, Spotify, Apple Music, YouTube and all other major online music services.

Where would you find metadata in a sound file?

Usually it's embedded into the track when the file is created, but sometimes it can be left out. FLAC, AIFF and MP3 formats take full advantage of metadata whereas WAV files only allow a little metadata input, so not as helpful for browsing your library.

Does MP3 contain metadata?

Metadata has nothing to do with the file name of the mp3, it's information that you need a music player or special editor to view and/or change. Metadata is important for the following reasons: You want your name to show up in the key music databases, Rovi/Allmusic and CDDB/Gracenote as associated with that track.

How is metadata stored in MP3?

The method, now known as ID3v1, quickly became the de facto standard for storing metadata in MP3s. The ID3v1 tag occupies 128 bytes, beginning with the string TAG 128 bytes from the end of the file. The tag was placed at the end of the file to maintain compatibility with older media players.


1 Answers

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!

like image 191
jmkk Avatar answered Oct 16 '22 23:10

jmkk