# Interface (AVMetadataExtractor) > **NOTE** > > - The initial APIs of this module are supported since API version 6. Newly added APIs will be marked with a superscript to indicate their earliest API version. > - The initial APIs of this interface are supported since API version 11. AVMetadataExtractor is a class for metadata retrieval. It provides APIs to obtain metadata and thumbnails from media assets. Before calling any API of AVMetadataExtractor, you must use [media.createAVMetadataExtractor](arkts-apis-media-f.md#mediacreateavmetadataextractor11) to create an AVMetadataExtractor instance. For details about the demo of obtaining audio or video metadata and video thumbnails, see [Using AVMetadataExtractor to Extract Audio and Video Metadata (ArkTS)](../../media/media/avmetadataextractor.md). ## Modules to Import ```ts import { media } from '@kit.MediaKit'; ``` ## Properties **System capability**: SystemCapability.Multimedia.Media.AVMetadataExtractor | Name | Type | Read-Only| Optional | Description | | --------------------------------------------------- | ------------------------------------------------------------ | ---- | ---- | ------------------------------------------------------------ | | fdSrc12+ | [AVFileDescriptor](arkts-apis-media-i.md#avfiledescriptor9) | No | Yes | Media file descriptor, which specifies the data source.
**Example:**
There is a media file that stores continuous assets, the address offset is 0, and the byte length is 100. Its file descriptor is **AVFileDescriptor { fd = resourceHandle; offset = 0; length = 100; }**.
**NOTE**
After the resource handle (FD) is transferred to an AVImageGenerator instance, do not use the resource handle to perform other read and write operations, including but not limited to transferring this handle to other AVPlayer, AVMetadataExtractor, AVImageGenerator, or AVTranscoder instance. Competition occurs when multiple AVMetadataExtractor use the same resource handle to read and write files at the same time, resulting in errors in obtaining video thumbnail data.| | dataSrc11+ | [AVDataSrcDescriptor](arkts-apis-media-i.md#avdatasrcdescriptor10) | Yes | Yes | Streaming media resource descriptor, which specifies the data source. Before obtaining metadata, you must set the data source through either **fdSrc** or **dataSrc**.
When an application obtains a media file from the remote, you can set **dataSrc** to obtain the metadata before the application finishes the downloading.| ## setUrlSource20+ setUrlSource(url: string, headers?: Record\): void Sets the data source for a network on-demand resource. Only network metadata ([fetchMetadata](#fetchmetadata11)) and thumbnails ([fetchFrameByTime](#fetchframebytime20)) can be obtained. The media resource URL must be set before the retrieval. **System capability**: SystemCapability.Multimedia.Media.AVMetadataExtractor **Parameters** | Name | Type | Mandatory| Description | | -------- | -------------------------------------------- | ---- | ----------------------------------- | | url | string | Yes | URL of the media resource.
1. The video formats MP4, MPEG-TS, and MKV are supported.
2. The audio formats M4A, AAC, MP3, OGG, WAV, FLAC, and AMR are supported.
**Example of supported URLs**:
1. HTTP: http\://xx
2. HTTPS: https\://xx
Note: HLS/DASH and live streaming resources cannot be set.| | headers | Record\ | No | Custom HTTP headers for accessing the network resource. The default value is empty.| **Example** ```ts import { BusinessError } from '@kit.BasicServicesKit'; import { media } from '@kit.MediaKit'; let avMetadataExtractor: media.AVMetadataExtractor | undefined = undefined; media.createAVMetadataExtractor(async (error: BusinessError, extractor: media.AVMetadataExtractor) => { if (extractor != null) { avMetadataExtractor = extractor; console.info('Succeeded in creating AVMetadataExtractor'); let url = "http://xx"; let headers: Record = { "User-Agent" : "User-Agent-Value" }; avMetadataExtractor.setUrlSource(url, headers); } else { console.error(`Failed to create AVMetadataExtractor, error message:${error.message}`); } }); ``` ## fetchFrameByTime20+ fetchFrameByTime(timeUs: number, options: AVImageQueryOptions, param: PixelMapParams): Promise\ Obtains a video thumbnail. This API uses a promise to return the result. **System capability**: SystemCapability.Multimedia.Media.AVMetadataExtractor **Parameters** | Name | Type | Mandatory| Description | | -------- | -------------------------------------------- | ---- | ----------------------------------- | | timeUs | number | Yes | Time of the video for which a thumbnail is to be obtained, in us.| | options | [AVImageQueryOptions](arkts-apis-media-e.md#avimagequeryoptions12) | Yes | Relationship between the time passed in and the video frame.| | param | [PixelMapParams](arkts-apis-media-i.md#pixelmapparams12) | Yes | Format parameters of the thumbnail to be obtained.| **Return value** | Type | Description | | -------------- | ---------------------------------------- | | Promise\<[image.PixelMap](../apis-image-kit/arkts-apis-image-PixelMap.md)> | Promise used to return the video thumbnail.| **Error codes** For details about the error codes, see [Media Error Codes](errorcode-media.md). | ID| Error Message | | -------- | ----------------------------------------- | | 5400102 | Operation not allowed. Returned by promise. | | 5400106 | Unsupported format. Returned by promise. | | 5400108 | Parameter check failed. Returned by promise. | **Example** ```ts import { BusinessError } from '@kit.BasicServicesKit'; import { image } from '@kit.ImageKit'; import { media } from '@kit.MediaKit'; let avMetadataExtractor: media.AVMetadataExtractor | undefined = undefined; let pixel_map : image.PixelMap | undefined = undefined; // Initialize input parameters. let timeUs: number = 0; let queryOption: media.AVImageQueryOptions = media.AVImageQueryOptions.AV_IMAGE_QUERY_PREVIOUS_SYNC; let param: media.PixelMapParams = { width : 300, height : 300 }; // Obtain the thumbnail. media.createAVMetadataExtractor((error: BusinessError, extractor: media.AVMetadataExtractor) => { if (extractor != null) { avMetadataExtractor = extractor; console.info('Succeeded in creating AVMetadataExtractor'); avMetadataExtractor.fetchFrameByTime(timeUs, queryOption, param).then((pixelMap: image.PixelMap) => { pixel_map = pixelMap; }).catch((error: BusinessError) => { console.error(`Failed to fetch FrameByTime, error message:${error.message}`); }); } else { console.error(`Failed to create AVMetadataExtractor, error message:${error.message}`); } }); ``` ## fetchMetadata11+ fetchMetadata(callback: AsyncCallback\): void Obtains media metadata. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.Multimedia.Media.AVMetadataExtractor **Parameters** | Name | Type | Mandatory| Description | | -------- | -------------------------------------------- | ---- | ----------------------------------- | | callback | AsyncCallback\<[AVMetadata](arkts-apis-media-i.md#avmetadata11)> | Yes | Callback used to return the result, which is an AVMetadata instance.| **Error codes** For details about the error codes, see [Media Error Codes](errorcode-media.md). | ID| Error Message | | -------- | ------------------------------------------ | | 5400102 | Operation not allowed. Returned by callback. | | 5400106 | Unsupported format. Returned by callback. | **Example** ```ts import { BusinessError } from '@kit.BasicServicesKit'; import { media } from '@kit.MediaKit'; async function test() { // Create an AVMetadataExtractor instance. let avMetadataExtractor: media.AVMetadataExtractor = await media.createAVMetadataExtractor(); avMetadataExtractor.fetchMetadata((error: BusinessError, metadata: media.AVMetadata) => { if (error) { console.error(`Failed to fetch Metadata, err = ${JSON.stringify(error)}`); return; } console.info(`Succeeded in fetching Metadata, genre: ${metadata.genre}`); }); } ``` ## fetchMetadata11+ fetchMetadata(): Promise\ Obtains media metadata. This API uses a promise to return the result. **System capability**: SystemCapability.Multimedia.Media.AVMetadataExtractor **Return value** | Type | Description | | -------------- | ---------------------------------------- | | Promise\<[AVMetadata](arkts-apis-media-i.md#avmetadata11)> | Promise used to return the result. which is an AVMetadata instance.| **Error codes** For details about the error codes, see [Media Error Codes](errorcode-media.md). | ID| Error Message | | -------- | ----------------------------------------- | | 5400102 | Operation not allowed. Returned by promise. | | 5400106 | Unsupported format. Returned by promise. | **Example** ```ts import { BusinessError } from '@kit.BasicServicesKit'; import { media } from '@kit.MediaKit'; async function test() { // Create an AVMetadataExtractor instance. let avMetadataExtractor: media.AVMetadataExtractor = await media.createAVMetadataExtractor(); avMetadataExtractor.fetchMetadata().then((metadata: media.AVMetadata) => { console.info(`Succeeded in fetching Metadata, genre: ${metadata.genre}`); }).catch((error: BusinessError) => { console.error(`Failed to fetch Metadata, error message:${error.message}`); }); } ``` ## fetchAlbumCover11+ fetchAlbumCover(callback: AsyncCallback\): void Obtains the cover of the audio album. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.Multimedia.Media.AVMetadataExtractor **Parameters** | Name | Type | Mandatory| Description | | -------- | -------------------------------------------- | ---- | ----------------------------------- | | callback | AsyncCallback\<[image.PixelMap](../apis-image-kit/arkts-apis-image-PixelMap.md)> | Yes | Callback used to return the album cover.| **Error codes** For details about the error codes, see [Media Error Codes](errorcode-media.md). | ID| Error Message | | -------- | ------------------------------------------ | | 5400102 | Operation not allowed. Return by callback. | | 5400106 | Unsupported format. Returned by callback. | **Example** ```ts import { BusinessError } from '@kit.BasicServicesKit'; import { image } from '@kit.ImageKit'; import { media } from '@kit.MediaKit'; async function test() { // Create an AVMetadataExtractor instance. let avMetadataExtractor: media.AVMetadataExtractor = await media.createAVMetadataExtractor(); let pixel_map : image.PixelMap | undefined = undefined; avMetadataExtractor.fetchAlbumCover((error: BusinessError, pixelMap: image.PixelMap) => { if (error) { console.error(`Failed to fetch AlbumCover, error = ${JSON.stringify(error)}`); return; } pixel_map = pixelMap; }); } ``` ## fetchAlbumCover11+ fetchAlbumCover(): Promise\ Obtains the cover of the audio album. This API uses a promise to return the result. **System capability**: SystemCapability.Multimedia.Media.AVMetadataExtractor **Return value** | Type | Description | | -------------- | ---------------------------------------- | | Promise\<[image.PixelMap](../apis-image-kit/arkts-apis-image-PixelMap.md)> | Promise used to return the album cover.| **Error codes** For details about the error codes, see [Media Error Codes](errorcode-media.md). | ID| Error Message | | -------- | ----------------------------------------- | | 5400102 | Operation not allowed. Returned by promise. | | 5400106 | Unsupported format. Returned by promise. | **Example** ```ts import { BusinessError } from '@kit.BasicServicesKit'; import { image } from '@kit.ImageKit'; import { media } from '@kit.MediaKit'; async function test() { // Create an AVMetadataExtractor instance. let avMetadataExtractor: media.AVMetadataExtractor = await media.createAVMetadataExtractor(); let pixel_map : image.PixelMap | undefined = undefined; avMetadataExtractor.fetchAlbumCover().then((pixelMap: image.PixelMap) => { pixel_map = pixelMap; }).catch((error: BusinessError) => { console.error(`Failed to fetch AlbumCover, error message:${error.message}`); }); } ``` ## release11+ release(callback: AsyncCallback\): void Releases this AVMetadataExtractor instance. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.Multimedia.Media.AVMetadataExtractor **Parameters** | Name | Type | Mandatory| Description | | -------- | -------------------------------------------- | ---- | ----------------------------------- | | callback | AsyncCallback\ | Yes |Callback used to return the result. If the operation is successful, **err** is **undefined**; otherwise, **err** is an error object.| **Error codes** For details about the error codes, see [Media Error Codes](errorcode-media.md). | ID| Error Message | | -------- | ------------------------------------------ | | 5400102 | Operation not allowed. Returned by callback. | **Example** ```ts import { BusinessError } from '@kit.BasicServicesKit'; import { media } from '@kit.MediaKit'; async function test() { // Create an AVMetadataExtractor instance. let avMetadataExtractor: media.AVMetadataExtractor = await media.createAVMetadataExtractor(); avMetadataExtractor.release((error: BusinessError) => { if (error) { console.error(`Failed to release, err = ${JSON.stringify(error)}`); return; } console.info(`Succeeded in releasing.`); }); } ``` ## release11+ release(): Promise\ Releases this AVMetadataExtractor instance. This API uses a promise to return the result. **System capability**: SystemCapability.Multimedia.Media.AVMetadataExtractor **Return value** | Type | Description | | -------------- | ---------------------------------------- | | Promise\ | Promise that returns no value.| **Error codes** For details about the error codes, see [Media Error Codes](errorcode-media.md). | ID| Error Message | | -------- | ----------------------------------------- | | 5400102 | Operation not allowed. Returned by promise. | **Example** ```ts import { BusinessError } from '@kit.BasicServicesKit'; import { media } from '@kit.MediaKit'; async function test() { // Create an AVMetadataExtractor instance. let avMetadataExtractor: media.AVMetadataExtractor = await media.createAVMetadataExtractor(); avMetadataExtractor.release().then(() => { console.info(`Succeeded in releasing.`); }).catch((error: BusinessError) => { console.error(`Failed to release, error message:${error.message}`); }); } ```