We use essential cookies for the website to function, as well as analytics cookies for analyzing and creating statistics of the website performance. To agree to the use of analytics cookies, click "Accept All". You can manage your preferences at any time by clicking "Cookie Settings" on the footer. More Information.

Only Essential Cookies
Accept All

Using AVImageGenerator to Extract Video Images at a Specified Time (ArkTS)

You can use the AVImageGenerator to obtain the thumbnail of a video at the specified time from the raw media asset.

The full process of obtaining the video thumbnail includes creating an AVImageGenerator instance, setting resources, obtaining the thumbnail, and releasing the instance.

How to Develop

For details about the APIs, see AVImageGenerator.

  1. Use createAVImageGenerator() to create an instance.

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. import { media } from '@kit.MediaKit';
    2. let avImageGenerator: media.AVImageGenerator = await media.createAVImageGenerator();
  2. Set resources. Specifically, set the fdSrc property (indicating the file descriptor).

    NOTE

    Verify resource validity and set fdSrc based on the actual situation:

    • You can use ResourceManager.getRawFd to open a HAP resource file descriptor. For usage, refer to getRawFd in the ResourceManager API.

    • You can also use the app sandbox path to access the corresponding resource (ensure that the resource file is available). For details, see Obtaining Application File Paths. For an introduction to the app sandbox and how to push files to it, see Application Sandbox.

    • If different AVImageGenerator or AVMetadataExtractor instances need to operate on the same resource, you must open the file descriptor multiple times. Do not share the same file descriptor.

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. import { common } from '@kit.AbilityKit';
    2. // Obtain the context of the ability to which the current component belongs and obtain the application file path through the context.
    3. let context = this.getUIContext().getHostContext() as common.UIAbilityContext;
    4. // Set fdSrc. H264_AAC.mp4 is a preset resource in the rawfile directory. Replace it with the actual one.
    5. avImageGenerator.fdSrc = await context.resourceManager.getRawFd('H264_AAC.mp4');
  3. Obtain the thumbnail at the specified time. Specifically, call fetchFrameByTime() to obtain a PixelMap object, which can be used for image display.

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. import { image } from '@kit.ImageKit';
    2. // Declare a pixelMap object, which is used for image display.
    3. @State pixelMap: image.PixelMap | undefined = undefined;
    4. // Initialize input parameters.
    5. let timeUs = 0; // Timestamp in the video where the thumbnail should be captured.
    6. let queryOption = media.AVImageQueryOptions.AV_IMAGE_QUERY_NEXT_SYNC; // AV_IMAGE_QUERY_NEXT_SYNC means that the key frame at or after the specified timestamp is selected.
    7. // Parameters for the output thumbnail format.
    8. let param: media.PixelMapParams = {
    9. width : 300, // Width of the output thumbnail.
    10. height : 300 // Height of the output thumbnail.
    11. };
    12. // Obtain the thumbnail (promise mode).
    13. this.pixelMap = await avImageGenerator.fetchFrameByTime(timeUs, queryOption, param);
  4. Call release() to release the AVImageGenerator instance.

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. // Release the instance (promise mode).
    2. await avImageGenerator.release().catch((err: BusinessError) => {
    3. console.error(`release failed, error code: ${err.code}, error message: ${err.message}`);
    4. });

Running the Sample Project

Refer to the sample project to obtain the video thumbnail at a specified time.

  1. Create a project, download the sample project, and copy its resources to the corresponding directories.

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. AVImageGeneratorArkTS
    2. entry/src/main/ets/
    3. └── pages
    4. └── Index.ets (Thumbnail retrieval UI)
    5. entry/src/main/resources/
    6. ├── base
    7. │ ├── element
    8. │ │ ├── color.json
    9. │ │ ├── float.json
    10. │ │ └── string.json
    11. │ └── media
    12. │
    13. └── rawfile
    14. └── H264_AAC.mp4 (Video resource)
  2. Compile and run the project.

Search in Guides
Enter a keyword.