Adding metadata to a movie

I am using AVAssetWriter to create MP4 files ( AVFileTypeMPEG4 ) and this bit works fine, however I am having trouble writing some of the metadata.


These keys work fine:

* AVMetadataCommonKeyAuthor

* AVMetadataCommonKeyDescription

* AVMetadataCommonKeyCopyrights


But the following don't, they log an entry to the console when used (about being invalid).

* AVMetadataCommonKeyModel

* AVMetadataCommonKeyMake

* AVMetadataCommonKeySoftware


I've also tried the QuickTime and iTunes variations, which don't log errors but don't add the meta data to the file either (and I specified the matching key spaces also).

* AVMetadataQuickTimeMetadataKeyMake

* AVMetadataQuickTimeMetadataKeyModel

* AVMetadataQuickTimeMetadataKeyKeywords

* AVMetadataQuickTimeMetadataKeySoftware

* AVMetadataiTunesMetadataKeyEncodedBy


Should I file this as a bug or there is some magic trick to make these work?

Accepted Reply

* AVMetadataCommonKeyModel

* AVMetadataCommonKeyMake

* AVMetadataCommonKeySoftware


MP4 files support the ISO userdata keyspace and the iTunes metadata keyspace. The above keys don't have an ISO userdata mapping, so that's why they're not working. Likewise, you can't put AVMetadataQuickTimeMetadataKey* in an MP4 file because it doesn't support that keyspace.


You can of course define your own private keys within the ISO userdata space and store those in the file (ISO userdata keys are 4 character codes). Example:


myMetadataItem.keySpace = AVMetadataKeySpaceISOUserData;

myMetadataItem.key = @"a4cc"; // or some other 4cc that's meaningful to you

myMetadataItem.value = // whatever

Replies

* AVMetadataCommonKeyModel

* AVMetadataCommonKeyMake

* AVMetadataCommonKeySoftware


MP4 files support the ISO userdata keyspace and the iTunes metadata keyspace. The above keys don't have an ISO userdata mapping, so that's why they're not working. Likewise, you can't put AVMetadataQuickTimeMetadataKey* in an MP4 file because it doesn't support that keyspace.


You can of course define your own private keys within the ISO userdata space and store those in the file (ISO userdata keys are 4 character codes). Example:


myMetadataItem.keySpace = AVMetadataKeySpaceISOUserData;

myMetadataItem.key = @"a4cc"; // or some other 4cc that's meaningful to you

myMetadataItem.value = // whatever

Thanks, after working in imaging for the last decade, video is really lackluster for meta data.

I tried exporting the video as AVFileTypeQuicktime as almost all the meta data appeared, except for the keywords. Which is okay I guess; as it seems I can set the 'TagNames' on the NSSavePanel to be the user specified keywords (within the meta data section of the app) and then attach them to the NSURL once export is complete.

>video is really lackluster for meta data.


I've hidden metadate in caption space instead, in the past, especially when SEO was a factor.

And here's code that actually compiles and runs (adding a private UserData key):



int8_t metadataValue[] = {0x01, 0x02, 0x03, 0x04};


NSData *metadataCFData = [NSData dataWithBytes:metadataValue length:sizeof(metadataValue)];

AVMutableMetadataItem *metadataItem = [AVMutableMetadataItem metadataItem];

NSMutableArray *metadataItems = [NSMutableArray array];


[metadataItem setKeySpace:AVMetadataKeySpaceISOUserData];


[metadataItem setKey:@"demo"];

[metadataItem setDataType:(NSString *)kCMMetadataBaseDataType_RawData];

[metadataItem setValue:metadataCFData]; // The value must be binary data: CFData for private key.

[metadataItems addObject:metadataItem];

[assetWriter setMetadata:metadataItems];

Thanks for this information.


I tested out the idea of injecting the keywords (that the user specifies in the meta data section of my app) into the tagNames of the NSSavePanel. When the user clicks "Save" I then update the internal meta data, and upon AVAssetWriter firing the completion block I then use [NSURL setResourceValues:] and append the NSArray to the 'tagNames' of the NSURL.


To my surprise it worked and is searchable via Spotlight, which is what I wanted. A little sad that it's not x-plat, but without a defined standard, there's not much I can do (at least IMHO).