omnypro / perpetual

Repeat one, done right.
http://playperpetually.com
25 stars 5 forks source link

Metadata #5

Closed bryanveloso closed 12 years ago

bryanveloso commented 12 years ago

This adds metadata support for MP3s and AAC (.mp4) files.

kallepersson commented 12 years ago

Sweet! If AAC metadata works fine, feel free to merge. :) I haven't been able to test that yet.

bryanveloso commented 12 years ago

It hasn't worked for me yet. :( Gonna keep trying.

kallepersson commented 12 years ago

Hm, I should try to fix it before we merge. It's weird though, QuickTime and iTunes seem to read the artist and title tags just fine and we are after all using QTKit.

bryanveloso commented 12 years ago

Odd. I set an NSLog to output the contents of mdArray... if I open an .mp4 I get the following:

2012-02-16 01:26:42.007 Perpetual[2897:707] (null)

Opening an .mp3 returns this:

2012-02-16 01:27:42.936 Perpetual[2897:707] (
    "<QTMetadataItem: 0x10a55f060 key = artist, keySpace = comn, locale = (null), time = 0:00:00:00.00/1, value = Hiroki Kikuta, extraAttributes = {\n    dataType = 2;\n}>",
    "<QTMetadataItem: 0x10060ef60 key = albumName, keySpace = comn, locale = (null), time = 0:00:00:00.00/1, value = Seiken Densetsu Music Complete Book, extraAttributes = {\n    dataType = 2;\n}>",
    "<QTMetadataItem: 0x10a541cf0 key = comment, keySpace = comn, locale = en, time = 0:00:00:00.00/1, value = SQEX-10253, extraAttributes = {\n    dataType = 2;\n}>",
    "<QTMetadataItem: 0x10a5043e0 key = title, keySpace = comn, locale = en, time = 0:00:00:00.00/1, value = Meridian Dance, extraAttributes = {\n    dataType = 2;\n}>",
    "<QTMetadataItem: 0x10a55bca0 key = copyrights, keySpace = comn, locale = en, time = 0:00:00:00.00/1, value = Square Enix Co., Ltd., extraAttributes = {\n    dataType = 2;\n}>"
)
kallepersson commented 12 years ago

Hm, the only official docs mentioning QTMovie metadata are these: https://developer.apple.com/library/mac/#releasenotes/General/MacOSXLionAPIDiffs/QTKit.html

Also, this blog posts outlines some of the new Metadata-related methods. http://oleb.net/blog/2011/08/whats-new-for-developers-in-lion-part-3/

Seems like all of this is new in Lion, so that is probably why it's not documented/discussed anywhere else. One would think that the official QTKit docs would mention it though.

Perhaps I need to do a [QTMovie availableMetadataFormats] and then do [QTMovie metadataForFormat:] instead of the current [QTMovie commonMetadata].

kallepersson commented 12 years ago

Seems like it!

//Opening an Mp3
2012-02-16 10:44:05.411 Perpetual[24920:707] (
    "com.apple.quicktime.mdta",
    "com.apple.quicktime.udta"
)

//Opening an Mp4
2012-02-16 10:44:17.010 Perpetual[24920:707] (
    "com.apple.itunes",
    "com.apple.quicktime.udta"
)
kallepersson commented 12 years ago

Great success!

NSArray * mdFormatsArray = [music availableMetadataFormats];
for(int i=0;i<[mdFormatsArray count];i++) {
    NSLog(@"%@",[music metadataForFormat:[mdFormatsArray objectAtIndex:i]]);
}

Output:

2012-02-16 10:48:15.057 Perpetual[25086:707] (
"<QTMetadataItem: 0x107634190 key = \U00a9nam, keySpace = itsk, locale = (null), time = 0:00:00:00.00/1, value =    awesome, extraAttributes = {\n    dataType = 1;\n}>",
"<QTMetadataItem: 0x107626a10 key = \U00a9ART, keySpace = itsk, locale = (null), time = 0:00:00:00.00/1, value = Kp, extraAttributes = {\n    dataType = 1;\n}>"
)

"kp" and "awesome" are two metadata tags I added manually to an mp4 file I had lying around, so it works. However, the keys look really weird - so it might be hard to get the actual values.

bryanveloso commented 12 years ago

Yeah, I'm finding the same thing myself...

kallepersson commented 12 years ago

Too bad it's badly documented. I guess there must be a way to get title and artist in an uniform way though.

kallepersson commented 12 years ago

Ok, this is a bit weird. But it works :) By using "©nam" for title and "©ART", it correctly gets the tags for both mp3 and mp4 files. Why they would name the keys that way is beyond me, so I'll investigate this further :)

NSArray * mdFormatsArray = [music availableMetadataFormats];
for(int i=0;i<[mdFormatsArray count];i++) {
    NSArray * mdArray = [music metadataForFormat:[mdFormatsArray objectAtIndex:i]];   
    NSArray * titleMetadataItems = [QTMetadataItem metadataItemsFromArray:mdArray withKey:@"©nam" keySpace:nil];
    if([titleMetadataItems count] > 0) {
        trackTitle = [[titleMetadataItems objectAtIndex:0] stringValue];
        NSLog(@"%@",trackTitle);
    }
    NSArray * artistMetadataItems = [QTMetadataItem metadataItemsFromArray:mdArray withKey:@"©ART" keySpace:nil];
    if([artistMetadataItems count] > 0) {
        trackArtist = [[artistMetadataItems objectAtIndex:0] stringValue];
        NSLog(@"%@",trackArtist);
    }
}
bryanveloso commented 12 years ago

I've done some investigation on its newer AVMetadataItem sibling and it seems to return the same thing, so I think we're good here.

kallepersson commented 12 years ago

Oh, I see. Should we switch to AV instead of QT then? Or just keep our current solution?

kallepersson commented 12 years ago

Oh, sorry. Seems like AVMetadataItem and related classes are only available in iOS. Sticking with the current solution it is then.I'll see if I can find any constant representing "©nam" and "©ART". Probably one of the constants in QTMetadataItem.h:

https://developer.apple.com/library/mac/#releasenotes/General/MacOSXLionAPIDiffs/QTKit.html

bryanveloso commented 12 years ago

Keep it for now since it works. I need to do a bit more experimentation with the AV flavor, a plus being that it exists on both iOS and OSX.

kallepersson commented 12 years ago

Sweet. I found the contstants: QTMetadataiTunesMetadataKeySongName and QTMetadataiTunesMetadataKeyArtist For some reason they are prefixed with @ instead of ©, so as a temporary workaround I am now replacing them.

I'll take a look at AV too, didn't know it existed on OSX too.

Pushed the changes.

bryanveloso commented 12 years ago

Oh, sorry. Seems like AVMetadataItem and related classes are only available in iOS.

They're available in OSX: http://cl.ly/EGzS

This looks good though. If you're happy with it, feel free to merge. :)