Article

Finding Metadata Values

Retrieve and filter all of the metadata associated with an asset.

Overview

Media container formats store descriptive metadata about their media. As a developer, it’s often challenging to work with metadata, because each container format has its own unique metadata format. You typically need a low-level understanding of the format to read and write a container’s metadata, but AVFoundation simplifies working with metadata through the use of its AVMetadataItem class.

In its most basic form, an instance of AVMetadataItem is a key-value pair representing a single metadata value such as a movie’s title or an album’s artwork. In the same way that AVAsset provides a normalized view of your media, AVMetadataItem provides a normalized view of its associated metadata.

Load the Asset's Metadata

You determine what metadata formats an asset contains by calling its availableMetadataFormats property. This property returns an array of string identifiers for each metadata format it contains. You then use its metadataForFormat: method to retrieve format-specific metadata values by passing it an appropriate format identifier, as shown below:

let url = Bundle.main.url(forResource: "audio", withExtension: "m4a")!
let asset = AVAsset(url: url)
let formatsKey = "availableMetadataFormats"

asset.loadValuesAsynchronously(forKeys: [formatsKey]) {
    var error: NSError? = nil
    let status = asset.statusOfValue(forKey: formatsKey, error: &error)
    if status == .loaded {
        for format in asset.availableMetadataFormats {
            let metadata = asset.metadata(forFormat: format)
            // process format-specific metadata collection
        }
    }
}

Filter the Metadata Collection

After you’ve retrieved a collection of metadata, the next step is to find the specific values of interest within it. You use the various class methods of AVMetadataItem to filter metadata collections down to a discrete set of values.

The easiest way to find specific metadata items is to filter by identifier, which groups a key space and key into a single unit. The following example shows how to retrieve the title item from the common key space:

let metadata = asset.commonMetadata
let titleID = AVMetadataCommonIdentifierTitl
let titleItems = AVMetadataItem.metadataItems(from: metadata, filteredByIdentifier: titleID)

if let item = titleItems.first {
    // process title item
}

Find Specific Values

After you’ve retrieved a specific metadata item, the next step is to call its value property. The value returned is an object type adopting the NSObject and NSCopying protocols. You can manually cast the value to the appropriate type, but it’s safer and easier to use the metadata item’s type coercion properties. You can use its stringValue, numberValue, dateValue, and dataValue properties to easily coerce the value to the appropriate type. For instance, the following example shows how you retrieve the artwork associated with an iTunes music track:

// Collection of "common" metadata
let metadata = asset.commonMetadata

// Filter metadata to find the asset's artwork
let artworkItems = AVMetadataItem.metadataItems(from: metadata, filteredByIdentifier: AVMetadataCommonIdentifierArtwork)

if let artworkItem = artworkItems.first {
    // Coerce the value to an NSData using its dataValue property
    if let imageData = artworkItem.dataValue {
        let image = UIImage(data: imageData)
        // process image
    } else {
        // No image data found
    }
}

See Also

Metadata Manipulation

JavaScript Object Notation (JSON) Chapter Format for HTTP Live Streaming

Understand the JSON chapter formatting required for HTTP Live Streaming.

AVMetadataItem

A metadata item associated with an audiovisual asset or one of its tracks.

AVMetadataGroup

A collection of metadata items associated with a timeline segment.

AVMetadataItemFilter

An object that filters selected information from a metadata item.

AVMetadataItemValueRequest

An object used to respond to a request to load the value of a metadata item.

AVMutableMetadataItem

A mutable metadata item associated with an audiovisual asset or with one of its tracks.

AVDateRangeMetadataGroup

A collection of metadata items that are valid for use within a specific date range.

AVMutableDateRangeMetadataGroup

A mutable collection of metadata items that are valid for use within a specific range of dates.

AVTimedMetadataGroup

A collection of metadata items that are valid for use during a specific time range.

AVMutableTimedMetadataGroup

A mutable collection of metadata items that are valid for use during a specific time range.

AVMetadataFormat

A value that defines a metadata format.