1

I’m trying to attach some XMP metadata to a QuickTime video I'm exporting using AVAssetExportSession.

AVFoundation does support writing metadata (AVMetadataItem) and I’ve managed to export simple values which can subsequently be examined using exiftool:

AVMutableMetadataItem *item = [AVMutableMetadataItem metadataItem];
item.identifier = [AVMetadataItem identifierForKey:AVMetadataCommonKeyTitle keySpace:AVMetadataKeySpaceCommon];
item.value = @"My Title";

exportSession.metadata = @[item];

But I’m having trouble configuring my AVMetadataItem’s to correctly encode XMP. According to the Adobe XMP spec, XMP in QuickTime videos should be under the moov / udta / XMP_ atoms but I can’t see a way to make hierarchical metadata using the AVFoundation API, or any key space that corresponds to this part of the metadata.

I also need to write XMP metadata to images, and Image I/O does have direct support for this (CGImageMetadataCreateFromXMPData), but I can't find anything equivalent in AVFoundation.

If it's not possible using AVFoundation (or similar), I'll probably look at integrating XMP-Toolkit-SDK but this feels like a clunky solution when AVFoundation almost seems to do what I need.

Dan
  • 81
  • 6

1 Answers1

4

I finally managed to figure this after trying lots of variations of keys/key spaces and other attributes of AVMetadataItem:

  • Use a custom XMP_ key in the AVMetadataKeySpaceQuickTimeUserData key space
  • Set the value not as an NSString but as an NSData containing UTF-8 data for the payload
  • Set the dataType to raw data

This results in XMP attributes that can be read by exiftool as expected.

NSString *payload =
  @"<x:xmpmeta xmlns:x=\"adobe:ns:meta/\" x:xmptk=\"MyAppXMPLibrary\">"
    "<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">"
      "<rdf:Description rdf:about=\"\" xmlns:xmp=\"http://ns.adobe.com/xap/1.0/\">"
        "<xmp:CreatorTool>My App</xmp:CreatorTool>"
      "</rdf:Description>"
    "</rdf:RDF>"
  "</x:xmpmeta>";

NSData *data = [payload dataUsingEncoding:kCFStringEncodingUTF8];

AVMutableMetadataItem *item = [AVMutableMetadataItem metadataItem];
item.identifier = [AVMetadataItem identifierForKey:@"XMP_"
                                          keySpace:AVMetadataKeySpaceQuickTimeUserData];
item.dataType = (NSString *)kCMMetadataBaseDataType_RawData;
item.value = data;

exportSession.metadata = @[item];
Dan
  • 81
  • 6