Set a video thumbnail

更新时间:
复制 MD 格式

ApsaraVideo for VOD uses an auto-captured snapshot as the default video thumbnail. You can specify a custom thumbnail during or after upload.

Overview

Every video uploaded to ApsaraVideo for VOD has a thumbnail.

If you specify a thumbnail during upload, that image is used. Otherwise, ApsaraVideo for VOD captures one to eight snapshots and selects one from the middle as the default (Video snapshots). You can update the thumbnail after upload.

ApsaraVideo for VOD supports regular and smart thumbnails. Enable the smart thumbnail feature from the Smart Thumbnail product page.

  • Regular thumbnail: A custom image you specify. Sources include local images, video snapshots, or accessible URLs. Supports static and animated formats.

  • Smart thumbnail: The system analyzes video content and visual aesthetics to select the optimal keyframe or segment. Supports static and animated images extracted from the source video.

Use case

Thumbnail type

Method

Documentation

Set a thumbnail during upload

Regular thumbnail

OpenAPI

Set a thumbnail during upload

Update a thumbnail after upload

Regular thumbnail

  • Console

  • OpenAPI

Update a thumbnail after upload

Smart thumbnail

OpenAPI

Prerequisites

Set a thumbnail during upload

You can specify a thumbnail during upload only when using an API or SDK (Upload methods).

Important

The CoverURL parameter accepts any URL. ApsaraVideo for VOD stores this URL and returns it upon request. Ensure that the URL is accessible.

  • When you call the CreateUploadVideo operation, pass the CoverURL parameter to specify the thumbnail URL.

  • When you call the UploadMediaByURL operation, pass the CoverURL parameter within UploadMetadata to specify the thumbnail URL.

Update a thumbnail after upload

After uploading a video, use the following methods to update its thumbnail.

Set a regular thumbnail

Console

Update a video thumbnail in the ApsaraVideo for VOD console using a local image or a video snapshot.

  1. Log on to the ApsaraVideo for VOD console.

  2. In the left-side navigation pane, choose Media Files > Audio/Video.

  3. Find the video that you want to update and click Manage in the Actions column.

  4. On the Basic Information tab, click Editing Video Information .

    • Select one of the automatically captured snapshots to use as the thumbnail.

    • Click Upload to select a local image as the thumbnail.

      Note

      The image must be a JPG or PNG file, 1 MB or smaller, with a maximum resolution of 1920×1080 pixels.

  5. Click Save to complete the configuration.

    Return to the audio/video list to verify the updated thumbnail.

OpenAPI

Call the following API operations to update a video thumbnail. Any accessible URL can serve as the thumbnail source.

Important

The CoverURL parameter accepts any URL. ApsaraVideo for VOD stores this URL and returns it upon request. Ensure that the URL is accessible.

  • Call the UpdateVideoInfo operation and pass CoverURL to specify the thumbnail URL.

  • Call the UpdateVideoInfos operation and pass CoverURL within UpdateContent to specify the thumbnail URL.

  • If your video is in your own OSS bucket registered with ApsaraVideo for VOD, call the RegisterMedia operation and pass CoverURL within RegisterMetadata to specify the thumbnail URL.

Set a smart thumbnail

The smart thumbnail feature analyzes video content, visual aesthetics, and extensive production data to select the optimal keyframe or segment, improving click-through rates and user experience.

Note

ApsaraVideo for VOD supports static and animated thumbnail images. Video thumbnails are not supported.

Example: Set the first frame as thumbnail

Process overview

  1. Create a snapshot template to capture the first frame of a video.

  2. Submit a snapshot job to capture the first frame.

  3. Set the video thumbnail to the captured first-frame snapshot.

Procedure

Console

  1. Create a snapshot template to capture the first frame.

    1. Log on to the ApsaraVideo for VOD console.

    2. In the left-side navigation pane, choose Configuration Management > Media Processing > Snapshot Templates.

    3. Click Create Snapshot Template. Configure the parameters to capture the first frame.

      Set a custom Template Name. Select Normal Snapshot for Snapshot Type. Set Start Time to 00:00:00 and Snapshot Count: to 1. Configure other parameters as needed (Snapshot templates).

    4. Click Save to create the snapshot template.

  2. Submit a snapshot job to capture the first frame.

    Note
    • To submit a snapshot job from the console, create a workflow with a Snapshots node and select the first-frame snapshot template (Manage workflows).

    • Configure a SnapshotComplete event notification to receive the task status and captured image URL when the job completes (Callback settings).

    • Scenario 1: Take a snapshot during video upload

      1. In the ApsaraVideo for VOD console, choose Media Files > Audio/Video.

      2. Click Upload Audio/Video, and then click Add Audio/Video.

      3. Set the upload method and storage location, add the video file, and then select Use Workflow. Choose the workflow you created for capturing the first frame.

      4. Click Upload.

    • Scenario 2: Take a snapshot after video upload

      1. In the ApsaraVideo for VOD console, choose Media Files > Audio/Video.

      2. In the audio/video list, find the target video and click Media Processing in the Actions column.

      3. Set Processing Type to Use Workflow, and set the Workflows parameter to the workflow you created for capturing the first frame.

      4. Click OK.

    • Obtain the snapshot result

      • If you have configured a SnapshotComplete event notification, you can get the task status and the captured image URL from the callback.

      • If you have not configured event notifications, you can periodically call the ListSnapshots operation to poll for the task status and the captured image URL.

  3. Set the video thumbnail to the first-frame snapshot.

    Note

    Save the snapshot from Step 2 to your local computer before uploading it as the thumbnail.

    1. In the ApsaraVideo for VOD console, choose Media Files > Audio/Video.

    2. In the audio/video list, find the target video and click Manage in the Actions column.

    3. On the Basic information tab, click Editing Video Information .

    4. Click Upload, select the first-frame snapshot that you saved locally, and click Open.

    5. Click Save to set the video thumbnail.

      Check the audio/video list to verify the thumbnail update.

OpenAPI

Note

The following examples use the Alibaba Cloud SDK for Java to call ApsaraVideo for VOD API operations.

Procedure

  1. Create a snapshot template to capture the first frame.

    Call the AddVodTemplate operation to create a snapshot template for capturing the first frame. The following code provides an example:

    import com.aliyuncs.DefaultAcsClient;
    import com.aliyuncs.exceptions.ClientException;
    import com.aliyuncs.profile.DefaultProfile;
    import com.aliyuncs.vod.model.v20170321.AddVodTemplateRequest;
    import com.aliyuncs.vod.model.v20170321.AddVodTemplateResponse;
    /**
     * Note:
     * 1. This demo shows how to create a template to capture the first frame as a single snapshot.
     * 2. We recommend using the ApsaraVideo for VOD console to create snapshot templates for a more convenient experience.
     */
    public class AddSnapshotTemplate {
        // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M.
        // Do not hard-code the AccessKey ID and AccessKey Secret in your project. Otherwise, the AccessKey pair may be leaked and the security of all your resources may be compromised.
        // This example shows how to use environment variables to obtain the AccessKey pair for identity verification. Before you run the code, configure the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables.
        public static String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        public static String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        public static void main(String[] args) {
            try{
                DefaultAcsClient vodClient = initVodClient(accessKeyId, accessKeySecret);
                AddVodTemplateResponse response = addSnapshotTemplate(vodClient);
                System.out.println("RequestId is:" + response.getRequestId());
                System.out.println("TemplateId is:" + response.getVodTemplateId());
            }catch (Exception e){
            }
        }
        public static AddVodTemplateResponse addSnapshotTemplate(DefaultAcsClient vodClient) throws ClientException {
            AddVodTemplateRequest request = new AddVodTemplateRequest();
            request.setName("First-frame snapshot template");
            request.setTemplateType("Snapshot");
            request.setTemplateConfig("{\"SnapshotType\":\"NormalSnapshot\",\"SnapshotConfig\":{\"FrameType\":\"normal\",\"Count\":1,\"Interval\":1,\"SpecifiedOffsetTime\":0}}");
            return vodClient.getAcsResponse(request);
        }
        public static DefaultAcsClient initVodClient(String accessKeyId, String accessKeySecret) throws ClientException {
            // The region where ApsaraVideo for VOD is activated.
            String regionId = "cn-shanghai";  
            DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
            DefaultAcsClient client = new DefaultAcsClient(profile);
            return client;
        }
    }
    
  2. Submit a snapshot job to capture the first frame.

    Call the SubmitSnapshotJob operation to submit a snapshot job. The following code provides an example:

    import com.aliyuncs.DefaultAcsClient;
    import com.aliyuncs.exceptions.ClientException;
    import com.aliyuncs.profile.DefaultProfile;
    import com.aliyuncs.vod.model.v20170321.*;
    /**
     * Note:
     * 1. This demo shows how to submit a snapshot job after a first-frame snapshot template is created.
     * 2. For newly uploaded videos, we recommend performing this step after the `VideoAnalysisComplete` event succeeds. For existing videos with a Normal status, you can design your own process.
     * 3. Snapshots are an asynchronous task. We recommend waiting for the `SnapshotComplete` event to succeed before you retrieve the snapshot URL.
     * 4. You can infer the snapshot output URL from `SnapshotRegular` in the event callback. For more information about the URL generation rule, see the `SnapshotComplete` topic.
     * 5. If you do not have a callback service, you can also call the `ListSnapshots` operation to poll for results. This operation returns only the latest snapshot result by default. For more information, see the `ListSnapshots` API reference.
     */
    public class SubmitSnapshotJob {
        // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M.
        // Do not hard-code the AccessKey ID and AccessKey Secret in your project. Otherwise, the AccessKey pair may be leaked and the security of all your resources may be compromised.
        // This example shows how to use environment variables to obtain the AccessKey pair for identity verification. Before you run the code, configure the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables.
        public static String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        public static String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        public static void main(String[] args) {
            try{
                DefaultAcsClient vodClient = initVodClient(accessKeyId, accessKeySecret);
                SubmitSnapshotJobResponse response = submitSnapshotJob(vodClient);
                System.out.println("RequestId is:" + response.getRequestId());
                System.out.println("JobId is:" + response.getSnapshotJob().getJobId());
            }catch (Exception e){
            }
        }
        public static SubmitSnapshotJobResponse submitSnapshotJob(DefaultAcsClient vodClient) throws ClientException {
            SubmitSnapshotJobRequest request = new SubmitSnapshotJobRequest();
            request.setVideoId("a42b**********633b79f0102");
            request.setSnapshotTemplateId("1f27a7**********eba2756");
            // Optional. Custom pass-through parameters that are available in callbacks. Use this to identify the first-frame snapshot.
            request.setUserData("{\"Extend\":{\"SnapshotType\":\"FirstFrame\",\"VideoId\":\"a42bf540********33b79f0102\"}}");
            return vodClient.getAcsResponse(request);
        }
        public static DefaultAcsClient initVodClient(String accessKeyId, String accessKeySecret) throws ClientException {
            // The region where ApsaraVideo for VOD is activated.
            String regionId = "cn-shanghai";  
            DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
            DefaultAcsClient client = new DefaultAcsClient(profile);
            return client;
        }
        // Call ListSnapshots to query snapshots.
        public static ListSnapshotsResponse listSnapshots(DefaultAcsClient vodClient) throws ClientException {
            ListSnapshotsRequest request = new ListSnapshotsRequest();
            request.setVideoId("a42bf540b1b371ed804a6633b79****");
            request.setSnapshotType("NormalSnapshot");
            ListSnapshotsResponse response = vodClient.getAcsResponse(request);
            System.out.println("RequestId is:" + response.getRequestId());
            System.out.println("SnapshotUrl is:" + response.getMediaSnapshot().getSnapshots().get(0).getUrl());
            return vodClient.getAcsResponse(request);
        }
    }
    
  3. Set the video thumbnail to the first-frame snapshot.

    This example modifies a single video's thumbnail after upload. Other methods: Set a thumbnail during video upload and Update a thumbnail after video upload.

    Call the UpdateVideoInfo operation and pass the CoverURL parameter to specify the video thumbnail. The following code provides an example:

    package com.alibaba.bltest.transcode;
    import com.aliyuncs.DefaultAcsClient;
    import com.aliyuncs.exceptions.ClientException;
    import com.aliyuncs.profile.DefaultProfile;
    import com.aliyuncs.vod.model.v20170321.UpdateVideoInfoRequest;
    import com.aliyuncs.vod.model.v20170321.UpdateVideoInfoResponse;
    /**
     * Note:
     * 1. This demo shows how to update the thumbnail of a single video. To modify other video parameters, see the `UpdateVideoInfo` API reference.
     * 2. When updating a thumbnail, ensure that the image URL you provide is valid.
     */
    public class UpdateVideoInfo {
        // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M.
        // Do not hard-code the AccessKey ID and AccessKey Secret in your project. Otherwise, the AccessKey pair may be leaked and the security of all your resources may be compromised.
        // This example shows how to use environment variables to obtain the AccessKey pair for identity verification. Before you run the code, configure the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables.
        public static String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        public static String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        public static void main(String[] args) {
            try{
                DefaultAcsClient vodClient = initVodClient(accessKeyId, accessKeySecret);
                UpdateVideoInfoResponse response = updateVideoInfo(vodClient);
                System.out.println("RequestId is:" + response.getRequestId());
            }catch (Exception e){
            }
        }
        public static UpdateVideoInfoResponse updateVideoInfo(DefaultAcsClient vodClient) throws ClientException {
            UpdateVideoInfoRequest request = new UpdateVideoInfoRequest();
            request.setVideoId("a42b***********33b79f0102");
            // When setting the first-frame thumbnail, set `CoverURL` to the image URL that is output after the first-frame snapshot job succeeds.
            request.setCoverURL("http://demo.aliyuncdn.com/a42bf5******40b1b37/snapshots/normal/41B7AF54-18672BB301D-1748-0984-309-112420****.jpg");
            return vodClient.getAcsResponse(request);
        }
        public static DefaultAcsClient initVodClient(String accessKeyId, String accessKeySecret) throws ClientException {
            // The region where ApsaraVideo for VOD is activated.
            String regionId = "cn-shanghai";  
            DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
            DefaultAcsClient client = new DefaultAcsClient(profile);
            return client;
        }
    }
    

Complete example

package com.alibaba.bltest.transcode;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.*;
import org.apache.commons.lang3.StringUtils;
/**
 * Note:
 * 1. This demo provides the complete logic for taking a snapshot and updating a thumbnail. You must adapt parts of the code to your business logic.
 * 2. This demo cannot be run directly. You must add the required logic.
 * 3. This demo is for reference only and does not represent the only implementation method.
 */
public class SnapshotAndUpdateCover {
    // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M.
    // Do not hard-code the AccessKey ID and AccessKey Secret in your project. Otherwise, the AccessKey pair may be leaked and the security of all your resources may be compromised.
    // This example shows how to use environment variables to obtain the AccessKey pair for identity verification. Before you run the code, configure the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables.
    public static String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
    public static String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
    public static void main(String[] args) {
        try{
            DefaultAcsClient vodClient = initVodClient(accessKeyId, accessKeySecret);
            // Video ID
            String videoId = "a42bf540b1b37*******b79f0102";
            // Scenario 1: You have a callback service or use Message Service (MNS).
            // If you set a thumbnail for a newly uploaded video, you must first receive the video analysis success event.
            // If you set a thumbnail for an existing video, you can directly submit a snapshot job from this step.
            submitSnapshotJob(vodClient,videoId);
            // After you receive the `SnapshotComplete` event callback, determine the snapshot type and retrieve the required image URL.
            JSONObject callBackMessage = new JSONObject();  // Replace this with the message received by the callback service.
            String snapshotType = callBackMessage.getJSONObject("UserData").getJSONObject("Extend").getString("SnapshotType");
            if("FirstFrame".equals(snapshotType)){
                // Replace the snapshot path logic here with your custom logic.
                String coverUrl = callBackMessage.getJSONArray("SnapshotInfos").getJSONObject(0).getString("SnapshotRegular").replace("{SnapshotCount}","00001");
                // Update the video thumbnail.
                updateVideoInfo(vodClient,videoId,coverUrl);
            }
            // Scenario 2: You do not have a callback service or use MNS.
            // If you set a thumbnail for a newly uploaded video, poll the video status after the upload.
            String videoStatus = "";
            while(!"Normal".equals(videoStatus)){
                videoStatus = getVideoInfo(vodClient,videoId);
                Thread.sleep(1000);
            }
            // If you set a thumbnail for an existing video, you can directly submit a snapshot job from this step.
            submitSnapshotJob(vodClient,videoId);
            // Poll for the snapshot result.
            String coverUrl = "";
            while(StringUtils.isBlank(coverUrl)){
                coverUrl = listSnapshots(vodClient,videoId);
                Thread.sleep(1000);
            }
            // Update the video thumbnail.
            updateVideoInfo(vodClient,videoId,coverUrl);
        }catch (Exception e){
        }
    }
    /**
     * Submit a snapshot job.
     */
    public static SubmitSnapshotJobResponse submitSnapshotJob(DefaultAcsClient vodClient, String vid) throws ClientException {
        SubmitSnapshotJobRequest request = new SubmitSnapshotJobRequest();
        request.setVideoId(vid);
        request.setSnapshotTemplateId("1f27a7f*********70eba2756");
        // Optional. Custom pass-through parameters that are available in callbacks. Use this to identify the first-frame snapshot.
        request.setUserData("{\"Extend\":{\"SnapshotType\":\"FirstFrame\",\"VideoId\":\"a42bf540********33b79f0102\"}}");
        return vodClient.getAcsResponse(request);
    }
    /**
     * Update the video thumbnail.
     */
    public static UpdateVideoInfoResponse updateVideoInfo(DefaultAcsClient vodClient, String vid, String coverUrl) throws ClientException {
        UpdateVideoInfoRequest request = new UpdateVideoInfoRequest();
        request.setVideoId(vid);
        // When setting the first-frame thumbnail, set `CoverURL` to the image URL that is output after the first-frame snapshot job succeeds.
        request.setCoverURL(coverUrl);
        return vodClient.getAcsResponse(request);
    }
    /**
     * Initialize the SDK instance.
     */
    public static DefaultAcsClient initVodClient(String accessKeyId, String accessKeySecret) throws ClientException {
        // The region where ApsaraVideo for VOD is activated.
        String regionId = "cn-shanghai";  
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
        DefaultAcsClient client = new DefaultAcsClient(profile);
        return client;
    }
    /**
     * Query snapshots.
     */
    public static String listSnapshots(DefaultAcsClient vodClient, String vid) throws ClientException {
        ListSnapshotsRequest request = new ListSnapshotsRequest();
        request.setVideoId(vid);
        request.setSnapshotType("NormalSnapshot");
        ListSnapshotsResponse response = vodClient.getAcsResponse(request);
        String coverUrl = "";
        System.out.println("RequestId is:" + response.getRequestId());
        try{
            coverUrl = response.getMediaSnapshot().getSnapshots().get(0).getUrl();
            System.out.println("SnapshotUrl is:" + response.getMediaSnapshot().getSnapshots().get(0).getUrl());
        }catch (NullPointerException e){
        }
        return coverUrl;
    }
    /**
     * Query a single video.
     */
    public static String getVideoInfo(DefaultAcsClient vodClient, String vid) throws ClientException {
        GetVideoInfoRequest request = new GetVideoInfoRequest();
        request.setVideoId(vid);
        GetVideoInfoResponse response = vodClient.getAcsResponse(request);
        System.out.println("RequestId is:" + response.getRequestId());
        String videoStatus = "";
        try{
            videoStatus = response.getVideo().getStatus();
            System.out.println("Video Status is:" + response.getVideo().getStatus());
        }catch (NullPointerException e){
        }
        return videoStatus;
    }
}