openharmony 鸿蒙 arkts-apis-media-AVImageGenerator

2026-08-25 浏览 (1)

Interface (AVImageGenerator)

AVImageGenerator is a class for video thumbnail retrieval. It provides APIs to obtain a thumbnail from a video. Before calling any API in AVImageGenerator, you must use createAVImageGenerator() to create an AVImageGenerator instance.

For details about the demo for obtaining video thumbnails, see Obtaining Video Thumbnails.

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 12.

Modules to Import

import { media } from '@kit.MediaKit';

Properties

System capability: SystemCapability.Multimedia.Media.AVImageGenerator

NameTypeRead-OnlyOptionalDescription
fdSrc12+AVFileDescriptorNoYesMedia 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 AVImageGenerator use the same resource handle to read and write files at the same time, resulting in errors in obtaining data.

fetchFrameByTime12+

fetchFrameByTime(timeUs: number, options: AVImageQueryOptions, param: PixelMapParams, callback: AsyncCallback<image.PixelMap>): void

Obtains a video thumbnail. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Multimedia.Media.AVImageGenerator

Parameters

NameTypeMandatoryDescription
timeUsnumberYesTime of the video for which a thumbnail is to be obtained, in μs.
optionsAVImageQueryOptionsYesRelationship between the thumbnail timestamp in and the video frame.
paramPixelMapParamsYesFormat parameters of the thumbnail to be obtained.
callbackAsyncCallback<image.PixelMap>YesCallback used to return the result. If the operation is successful, err is undefined and data is the PixelMap instance obtained; otherwise, err is an error object.

Error codes

For details about the error codes, see Media Error Codes.

IDError Message
5400102Operation not allowed. Returned by callback.
5400106Unsupported format. Returned by callback.

Example

import { BusinessError } from '@kit.BasicServicesKit';
import { image } from '@kit.ImageKit';
import { media } from '@kit.MediaKit';

let avImageGenerator: media.AVImageGenerator|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_NEXT_SYNC;

let param: media.PixelMapParams = {
  width: 300,
  height: 300,
};

// Obtain the thumbnail.
media.createAVImageGenerator((err: BusinessError, generator: media.AVImageGenerator) => {
  if (generator) {
    avImageGenerator = generator;
    console.info(`Succeeded in creating AVImageGenerator`);
    avImageGenerator.fetchFrameByTime(timeUs, queryOption, param, (error: BusinessError, pixelMap) => {
      if (error) {
        console.error(`Failed to fetch FrameByTime, err = ${JSON.stringify(error)}`);
        return;
      }
      pixel_map = pixelMap;
    });
  } else {
    console.error(`Failed to create AVImageGenerator, error message:${err.message}`);
  }
});

fetchFrameByTime12+

fetchFrameByTime(timeUs: number, options: AVImageQueryOptions, param: PixelMapParams): Promise<image.PixelMap>

Obtains a video thumbnail. This API uses a promise to return the result.

System capability: SystemCapability.Multimedia.Media.AVImageGenerator

Parameters

NameTypeMandatoryDescription
timeUsnumberYesTime of the video for which a thumbnail is to be obtained, in μs.
optionsAVImageQueryOptionsYesRelationship between the thumbnail timestamp in and the video frame.
paramPixelMapParamsYesFormat parameters of the thumbnail to be obtained.

Return value

TypeDescription
Promise<image.PixelMap>Promise used to return the video thumbnail.

Error codes

For details about the error codes, see Media Error Codes.

IDError Message
5400102Operation not allowed. Returned by promise.
5400106Unsupported format. Returned by promise.

Example

import { BusinessError } from '@kit.BasicServicesKit';
import { image } from '@kit.ImageKit';
import { media } from '@kit.MediaKit';

let avImageGenerator: media.AVImageGenerator|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_NEXT_SYNC;

let param: media.PixelMapParams = {
  width: 300,
  height: 300,
};

// Obtain the thumbnail.
media.createAVImageGenerator((err: BusinessError, generator: media.AVImageGenerator) => {
  if (generator) {
    avImageGenerator = generator;
    console.info(`Succeeded in creating AVImageGenerator`);
    avImageGenerator.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 AVImageGenerator, error message:${err.message}`);
  }
});

fetchScaledFrameByTime20+

fetchScaledFrameByTime(timeUs: number, queryMode: AVImageQueryOptions, outputSize?: OutputSize):Promise<image.PixelMap>

Fetches a scaled thumbnail from the video at a particular timestamp. This API uses a promise to return the result.

System capability: SystemCapability.Multimedia.Media.AVImageGenerator

Parameters

NameTypeMandatoryDescription
timeUsnumberYesTimestamp, in microseconds (μs), at which the thumbnail is to be fetched from the video.
queryModeAVImageQueryOptionsYesRelationship between the thumbnail timestamp in and the video frame.
outputSizeOutputSize NoOutput size of the thumbnail. By default, the original image size is used.

Return value

TypeDescription
Promise<image.PixelMap>Promise used to return the video thumbnail.

Error codes

For details about the error codes, see Media Error Codes.

IDError Message
5400102Operation not allowed. Returned by promise.
5400106Unsupported format. Returned by promise.

Example

import { BusinessError } from '@kit.BasicServicesKit';
import { image } from '@kit.ImageKit';
import { media } from '@kit.MediaKit';

let avImageGenerator: media.AVImageGenerator|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_NEXT_SYNC;
let outputSize: media.OutputSize = {
  width: 300,
  height: 300,
};
// Obtain the thumbnail.
media.createAVImageGenerator((err: BusinessError, generator: media.AVImageGenerator) => {
  if (generator) {
    avImageGenerator = generator;
    console.info(`Succeeded in creating AVImageGenerator`);
    avImageGenerator.fetchScaledFrameByTime(timeUs, queryOption, outputSize).then((pixelMap: image.PixelMap) => {
      pixel_map = pixelMap;
    }).catch((error: BusinessError) => {
      console.error(`Failed to fetch ScaledFrameByTime, error message:${error.message}`);
    });
  } else {
    console.error(`Failed to create AVImageGenerator, error message:${err.message}`);
  }
});

release12+

release(callback: AsyncCallback<void>): void

Releases this AVImageGenerator instance. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Multimedia.Media.AVImageGenerator

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<void>YesCallback 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.

IDError Message
5400102Operation not allowed. Returned by callback.

Example

import { BusinessError } from '@kit.BasicServicesKit';
import { media } from '@kit.MediaKit';

let avImageGenerator: media.AVImageGenerator|undefined = undefined;

// Release the resources.
media.createAVImageGenerator((err: BusinessError, generator: media.AVImageGenerator) => {
  if (generator) {
    avImageGenerator = generator;
    console.info(`Succeeded in creating AVImageGenerator`);
    avImageGenerator.release((error: BusinessError) => {
      if (error) {
        console.error(`Failed to release, err = ${JSON.stringify(error)}`);
        return;
      }
      console.info(`Succeeded in releasing`);
    });
  } else {
    console.error(`Failed to create AVImageGenerator, error message:${err.message}`);
  }
});

release12+

release(): Promise<void>

Releases this AVImageGenerator instance. This API uses a promise to return the result.

System capability: SystemCapability.Multimedia.Media.AVImageGenerator

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

For details about the error codes, see Media Error Codes.

IDError Message
5400102Operation not allowed. Returned by promise.

Example

import { BusinessError } from '@kit.BasicServicesKit';
import { media } from '@kit.MediaKit';

let avImageGenerator: media.AVImageGenerator|undefined = undefined;

// Release the resources.
media.createAVImageGenerator((err: BusinessError, generator: media.AVImageGenerator) => {
  if (generator) {
    avImageGenerator = generator;
    console.info(`Succeeded in creating AVImageGenerator`);
    avImageGenerator.release().then(() => {
      console.info(`Succeeded in releasing.`);
    }).catch((error: BusinessError) => {
      console.error(`Failed to release, error message:${error.message}`);
    });
  } else {
    console.error(`Failed to create AVImageGenerator, error message:${err.message}`);
  }
});

你可能感兴趣的鸿蒙文章

openharmony 鸿蒙 capi-avrecorder-oh-avrecorder-range

openharmony 鸿蒙 errorcode-media

openharmony 鸿蒙 capi-avplayer

openharmony 鸿蒙 capi-avplayer-base-h

openharmony 鸿蒙 capi-avimage-generator-h

openharmony 鸿蒙 capi-avscreencapture-oh-rect

openharmony 鸿蒙 capi-videoprocessing-videoprocessing-callback

openharmony 鸿蒙 capi-avsinkbase

openharmony 鸿蒙 capi-avmetadataextractor

openharmony 鸿蒙 capi-avscreencapture-oh-multidisplaycapability

  • 所属分类: 后端技术
  • 本文标签: 鸿蒙 软件
  • 版权声明: 本文链接 https://seaxiang.com/blog/NDe5QM8E