基于系统能力获取视频缩略图
=============

#### 概述

视频缩略图是视频的静态预览图像，是从视频中截取的某一帧画面，经常用作视频的封面。在视频浏览、分享和管理等场景中使用可以帮助用户快速浏览和选择想要的内容，提高用户的使用体验。HarmonyOS提供了对应的模块能力，帮助开发者获取视频文件的缩略图。根据应用获取缩略图策略的不同，可以分为以下两种场景：

*   [获取视频默认缩略图](#section14470163720352)
*   [选取视频帧作为缩略图](#section1848255103812)

#### 获取视频默认缩略图

#### \[h2\]实现原理

视频的默认缩略图一般为视频的第一帧，可以通过[PhotoAsset](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/arkts-apis-photoaccesshelper-photoasset)类的[getThumbnail()](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/arkts-apis-photoaccesshelper-photoasset#getthumbnail-2)方法获取。这里以获取图库视频缩略图场景为例。

#### \[h2\]开发步骤

1.  通过相册管理模块[@ohos.file.photoAccessHelper](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-photoaccesshelper)的[PhotoViewPicker](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/arkts-apis-photoaccesshelper-photoviewpicker)选取图库视频，获得视频的URL。
    
    /\*\*
     \* Pull up the gallery picker and select a video.
     \* @returns The url of the selected video.
     \*/
    async selectVideo(): Promise<string> {
      try {
        let photoViewPicker = new photoAccessHelper.PhotoViewPicker();
        return photoViewPicker.select({
          MIMEType: photoAccessHelper.PhotoViewMIMETypes.VIDEO\_TYPE,
          maxSelectNumber: 1
        }).then((photoSelectResult: photoAccessHelper.PhotoSelectResult): string => {
          if (photoSelectResult.photoUris.length <= 0) {
            return '';
          }
          return photoSelectResult.photoUris\[0\];
        })
      } catch (error) {
        hilog.error(0x0000, TAG, \`selectVideo catch error, code: ${error.code}, message: ${error.message}\`);
        return '';
      }
    }
    
2.  使用[getAssets()](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/arkts-apis-photoaccesshelper-photoaccesshelper#getassets-1)方法，通过选择的图库视频的URL获取视频资源。
    
    // Obtain video resources.
    let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
    predicates.equalTo('uri', videoUrl);
    let videoFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> =
      await this.phAccessHelper.getAssets({
        fetchColumns: \['width', 'height', 'orientation'\],
        predicates: predicates
      });
    let photoAsset: photoAccessHelper.PhotoAsset = await videoFetchResult.getFirstObject();
    
3.  根据视频资源的属性配置缩略图的尺寸信息，调用getThumbnail()方法获取PixelMap格式的图片。
    
    // Configure thumbnail parameters.
    let thumbnailSize: Size = { width: 0, height: 0 };
    if (photoAsset.get(photoAccessHelper.PhotoKeys.ORIENTATION) === 90 ||
      photoAsset.get(photoAccessHelper.PhotoKeys.ORIENTATION) === 270) {
      thumbnailSize.width = photoAsset.get(photoAccessHelper.PhotoKeys.HEIGHT) as number;
      thumbnailSize.height = photoAsset.get(photoAccessHelper.PhotoKeys.WIDTH) as number;
    } else {
      thumbnailSize.width = photoAsset.get(photoAccessHelper.PhotoKeys.WIDTH) as number;
      thumbnailSize.height = photoAsset.get(photoAccessHelper.PhotoKeys.HEIGHT) as number;
    }
    return photoAsset.getThumbnail(thumbnailSize);
    

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/d/v3/oz0kUNLCQXeAri-8c2MzKA/note_3.0-zh-cn.png?HW-CC-KV=V1&HW-CC-Date=20260612T074606Z&HW-CC-Expire=86400&HW-CC-Sign=F580B7556AC389AD7DC5F2D27801DB93807999111FF89205186FD71CBC20C4FD)

使用getAsset()和getThumbnail()方法需要申请受限开放权限'[ohos.permission.READ\_IMAGEVIDEO'](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/restricted-permissions#ohospermissionread_imagevideo)，对于需要克隆、备份或同步图片/视频类文件的应用可申请获取该权限，并通过[getAlbums()](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/arkts-apis-photoaccesshelper-photoaccesshelper#getalbums)方法获取相册资源再调用这两个方法获取缩略图。或者通过picker的方式可以在不获取权限的情况下，使用这两个方法来访问用户指定的图库资源获取缩略图。本文中的示例使用的是第二种picker的方式。

#### \[h2\]实现效果

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/17/v3/HnnR3K75SSGmOMI844EKYw/zh-cn_image_0000002229450885.gif?HW-CC-KV=V1&HW-CC-Date=20260612T074606Z&HW-CC-Expire=86400&HW-CC-Sign=69FD066948469F2DD5FCD392F9E1E2A414906A53868CFBF8D89B7B6B89AEEF97 "点击放大")

#### 选取视频帧作为缩略图

#### \[h2\]实现原理

HarmonyOS提供视频缩略图获取类[AVImageGenerator](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/arkts-apis-media-avimagegenerator)用于选取视频指定时间的帧作为缩略图，这里以选取图库视频缩略图场景为例。

#### \[h2\]开发步骤

1.  拉起图库picker，选取视频获取文件资源描述符。
    
    /\*\*
     \* Obtain video resources through AVImageGenerator.
     \*/
    async imageGeneratorGetThumbnail() {
      this.photoUtils.selectVideo().then(async (result: string) => {
        // ...
        this.fileAlbum = fileIo.openSync(result, fileIo.OpenMode.READ\_ONLY);
        this.avFileDescriptor = { fd: this.fileAlbum.fd };
        // ...
      }).catch((error: BusinessError) => {
        hilog.error(0x0000, TAG,
          \`Invoke imageGeneratorGetThumbnail failed!, error code: ${error.code}, message: ${error.message}\`);
      })
    }
    
2.  根据文件资源描述符获取视频元数据信息。
    
    /\*\*
     \* Get video infos through video file descriptor.
     \* @param avFileDescriptor AVFileDescriptor of video.
     \* @returns the size infos of video.
     \*/
    async getVideoData(avFileDescriptor: media.AVFileDescriptor): Promise<VideoSizeData> {
      let videoSize: VideoSizeData = new VideoSizeData();
      try {
        let avMetaDataExtractor: media.AVMetadataExtractor = await media.createAVMetadataExtractor();
        avMetaDataExtractor.fdSrc = avFileDescriptor;
        let metadata = await avMetaDataExtractor.fetchMetadata();
        videoSize.photoSize.width = parseInt(metadata.videoWidth as string);
        videoSize.photoSize.height = parseInt(metadata.videoHeight as string);
        if (metadata.duration) {
          videoSize.totalTime = parseInt(metadata.duration);
        }
        avMetaDataExtractor.release();
      } catch (error) {
        hilog.error(0x0000, TAG, \`getVideoData catch error, code: ${error.code}, message: ${error.message}\`);
      }
      return videoSize;
    }
    
3.  创建AVImageGenerator。
    
    this.avImageGenerator = await media.createAVImageGenerator();
    if (this.avImageGenerator) {
      this.avImageGenerator.fdSrc = this.avFileDescriptor;
    } else {
      hilog.error(0X0000, TAG, 'Create AVImageGenerator failed!');
      return;
    }
    
4.  使用[fetchFrameByTime()](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/arkts-apis-media-avimagegenerator#fetchframebytime12-1)方法来获取指定时间的缩略图，返回PixelMap格式的图片。
    
    /\*\*
     \* Obtain a frame at a certain point in time of the video.
     \* @param time A certain point in time of the video.
     \*/
    async fetchFrameByTime(time: number) {
      this.pixelMap = await this.avImageGenerator?.fetchFrameByTime(time,
        media.AVImageQueryOptions.AV\_IMAGE\_QUERY\_CLOSEST\_SYNC, this.videoSize.photoSize).catch((error: BusinessError)=>{
        hilog.error(0x0000, TAG, \`release catch error, code: ${error.code}, message: ${error.message}\`);
        return undefined;
      });
    }
    

#### \[h2\]实现效果

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/0e/v3/jsWbiqdoQT6yAzL8lXdCFw/zh-cn_image_0000002194010608.gif?HW-CC-KV=V1&HW-CC-Date=20260612T074606Z&HW-CC-Expire=86400&HW-CC-Sign=BDAEAF1386D051F82A588A678C869071D6B74BDBB24E341C4A5797C8B6320EAF "点击放大")

#### 常见问题

#### \[h2\]如何获取网络视频的缩略图

推荐使用系统在API20上的新增接口[setUrlSource()](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/arkts-apis-media-avmetadataextractor#seturlsource20)，在[AVMetadataExtractor](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/arkts-apis-media-avmetadataextractor)实例中调用该接口获取网络视频url地址对应的数据源，再调用[fetchFrameByTime()](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/arkts-apis-media-avmetadataextractor#fetchframebytime20)接口即可获取网络视频的缩略图。

#### 示例代码

*   [基于系统能力获取视频缩略图](https://gitcode.com/harmonyos_samples/VideoThumbnail)