When you upload a video in the ApsaraVideo VOD console, you can set a thumbnail for the video. If you do not set a thumbnail for the video, ApsaraVideo VOD randomly selects one of the video snapshots as the thumbnail. You can change the thumbnail after the video is uploaded.
Overview
Each video that you upload to ApsaraVideo VOD has a thumbnail.
If you set an image when you upload a video, the specified image is used as the thumbnail. If you do not set an image when you upload a video, ApsaraVideo VOD captures up to eight snapshots from the video and selects a snapshot as the thumbnail. For more information, see Video snapshots. You can change the thumbnail after the video is uploaded.
Prerequisites
ApsaraVideo VOD is activated. For more information, see Activate ApsaraVideo VOD.
The RAM user or RAM role that you use has required permissions to access ApsaraVideo VOD. To improve the security of your Alibaba Cloud account and resources, we recommend that you use a RAM user or RAM role to set the thumbnail of a video. For more information, see Create a RAM user and grant permissions to the RAM user.
If you have configured an accelerated domain name in ApsaraVideo VOD, a certificate is configured for the accelerated domain name. For more information, see Enable HTTPS secure acceleration.
Set a thumbnail during upload
ApsaraVideo VOD supports multiple upload methods. For more information, see Overview. You can set a custom thumbnail only if you upload a video by using the ApsaraVideo VOD API or SDK. The following content describes the configurations:
Important After you specify a URL for the CoverURL parameter, ApsaraVideo VOD saves and returns the URL to users. Make sure that the URL that you specify for CoverURL is accessible.
Change the thumbnail after the video is uploaded
After a video is uploaded to ApsaraVideo VOD, you can use one of the following methods to change the thumbnail of the video.
Set a regular thumbnail
Use the ApsaraVideo VOD console
After a video is uploaded to ApsaraVideo VOD, you can change the video thumbnail in the ApsaraVideo VOD console. You can use a local image or video snapshot as the thumbnail.
Log on to the ApsaraVideo VOD console.
In the left-side navigation pane, choose Media Files > Audio/Video.
Find the video for which you want to change the thumbnail and click Manage in the Actions column.
On the Basic Information tab, click Editing Video Information.
Click Save.
You can check whether the thumbnail is changed on the Video and Audio page.
Use the ApsaraVideo VOD API
After a video is uploaded to ApsaraVideo VOD, you can call the following operations to change the video thumbnail: You can specify an accessible URL as the thumbnail URL.
Important After you specify a URL for the CoverURL parameter, ApsaraVideo VOD saves and returns the URL to users. Make sure that the URL that you specify for CoverURL is accessible.
Specify a thumbnail URL for the CoverURL
parameter when you call the UpdateVideoInfo operation.
Specify a thumbnail URL for the CoverURL
parameter in UpdateContent
when you call the UpdateVideoInfos operation.
If you want to change the thumbnail of a video that is stored in an Object Storage Service (OSS) bucket, specify a thumbnail URL for the CoverURL
parameter in RegisterMetadata
when you call the RegisterMedia operation.
Example - Set the first frame of the video as the thumbnail
Configuration process
Create a snapshot template for capturing the first frame of a video.
Create a snapshot task to capture the first frame of the video.
Change the video thumbnail to the first frame of the video.
Procedure
Use the ApsaraVideo VOD console
Create a snapshot template for capturing the first frame of a video.
Log on to the ApsaraVideo VOD console.
In the left-side navigation pane, choose Configuration Management > Media Processing > Snapshot Templates.
On the Snapshot Templates page, click Add Template. On the page that appears, configure the parameters for capturing the first frame of a video.
Specify a custom template name for Template Name, set Snapshot Type to Normal Snapshot, Start Time to 00:00:00, and Snapshot Count to 1. Specify other parameters based on your business requirements. For more information about the parameters, see Manage snapshot templates.
Click Save.
Create a snapshot task to capture the first frame of the video.
Note To create a snapshot task in the ApsaraVideo VOD console, you must create a workflow, add a Snapshots node in the workflow, and then specify the snapshot template that you created to capture the first frame of a video. For more information about how to create a workflow, see Manage workflows.
We recommend that you configure the SnapshotComplete event notification before you create a snapshot task. This way, you can obtain information such as the task status and snapshot URL from the callback after the task is complete. For more information about how to configure event notifications, see Configure callbacks.
Scenario 1: Capture a snapshot during upload
Log on to the ApsaraVideo VOD console. In the left-side navigation pane, choose .
On the Video and Audio page, click Upload. On the Upload Media page, click Add Media.
In the Add Media dialog box, specify the upload method, storage address, and the file that you want to upload. Then, select Use Workflow from the drop-down list and the workflow that you created for capturing the first frame of a video.
Click Upload.
Scenario 2: Capture a snapshot after upload
Log on to the ApsaraVideo VOD console. In the left-side navigation pane, choose .
On the Video and Audio page, find the file for which you want to capture snapshots and click Media Processing in the Actions column.
In the Media Processing dialog box, set Processing Type to Use Workflow and Workflow to the workflow that you created for capturing the first frame of a video.
Click OK.
Obtain the results of the snapshot task
If you configure the SnapshotComplete event notification before you create a snapshot task, you can obtain information such as the task status and snapshot URL from the callback after the task is complete.
If you do not configure the SnapshotComplete event notification before you create a snapshot task, you can use the round-robin method to call the ListSnapshots operation to obtain information such as the task status and snapshot URL.
Change the video thumbnail to the first frame of the video.
Note You can change the thumbnail of a video in the ApsaraVideo VOD console only after the video is uploaded. To use the first frame of a video as the thumbnail, you must save the snapshot that you captured in Step 2 to your local device.
Log on to the ApsaraVideo VOD console. In the left-side navigation pane, choose .
On the Video and Audio page, find the video for which you want to change the thumbnail and click Manage in the Actions column.
On the Basic Information tab, click Editing Video Information.
On the page that appears, click Upload, select the snapshot that you captured in Step 2 from your local device, and then click Open.
Click Save.
You can check whether the video thumbnail is changed on the Video and Audio page.
Use the ApsaraVideo VOD API
Note The following section describes how to call API operations to set the thumbnail of a video and provides sample code. ApsaraVideo VOD SDK for Java is used as an example.
Procedure
Create a snapshot template for capturing the first frame of a video.
Call the AddVodTemplate operation to create a snapshot template for capturing the first frame of a video. Sample code:
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. The following sample code is used to create a snapshot template for capturing the first frame of a video and export an image.
* 2. We recommend that you create a snapshot template in the ApsaraVideo VOD console.
*/
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.
// We recommend that you do not include your AccessKey pair (AccessKey ID and AccessKey secret) in your project code. Otherwise, the AccessKey pair may be leaked and the security of all resources within your account may be compromised.
// In this example, ApsaraVideo VOD reads the AccessKey pair from the environment variables to implement identity verification for API access. Before you run the sample code, configure the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET.
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 in which ApsaraVideo VOD is activated.
String regionId = "cn-shanghai";
DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
DefaultAcsClient client = new DefaultAcsClient(profile);
return client;
}
}
Create a snapshot task to capture the first frame of the video.
Call the SubmitSnapshotJob operation to submit a snapshot task. Sample code:
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.*;
/**
* Note:
* 1. The following sample code is used to create a snapshot task after you create a snapshot template for capturing the first frame of a video.
* 2. After you upload a video to ApsaraVideo VOD, you can create a snapshot task for the video only after the VideoAnalysisComplete event callback is returned. This is not required for existing videos that are in the normal state.
* 3. Snapshot tasks are asynchronously processed. We recommend that you query the snapshot URL after the SnapshotComplete event callback is returned.
* 4. You can obtain the output URL of the snapshot from the SnapshotRegular parameter in the callback. For more information, see the "Generation of snapshot URLs" section of the SnapshotComplete topic.
* 5. If you do not enable the callback feature, you can call the ListSnapshots operation to query the most recent snapshot information. For more information, see ListSnapshots.
*/
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.
// We recommend that you do not include your AccessKey pair (AccessKey ID and AccessKey secret) in your project code. Otherwise, the AccessKey pair may be leaked and the security of all resources within your account may be compromised.
// In this example, ApsaraVideo VOD reads the AccessKey pair from the environment variables to implement identity verification for API access. Before you run the sample code, configure the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET.
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. The custom pass-through parameters. This parameter is used to identify the snapshot of the first frame. You can use the parameter in event callbacks.
request.setUserData("{\"Extend\":{\"SnapshotType\":\"FirstFrame\",\"VideoId\":\"a42bf540********33b79f0102\"}}");
return vodClient.getAcsResponse(request);
}
public static DefaultAcsClient initVodClient(String accessKeyId, String accessKeySecret) throws ClientException {
// The region in which ApsaraVideo VOD is activated.
String regionId = "cn-shanghai";
DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
DefaultAcsClient client = new DefaultAcsClient(profile);
return client;
}
// Call the ListSnapshots operation 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);
}
}
Change the video thumbnail to the first frame of the video.
The following content describes how to change the video thumbnail after the video is uploaded. For more information about how to set the video thumbnail in other scenarios, see Set a thumbnail during upload and Change the thumbnail after the video is uploaded.
To change the thumbnail of an uploaded video, call the UpdateVideoInfo operation and configure the CoverURL parameter. Sample code:
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. The following sample code is used to change the thumbnail of a video. For more information about how to modify other video parameters, see the documentation of the UpdateVideoInfo operation.
* 2. Make sure that the thumbnail URL that you specify is accessible.
*/
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.
// We recommend that you do not include your AccessKey pair (AccessKey ID and AccessKey secret) in your project code. Otherwise, the AccessKey pair may be leaked and the security of all resources within your account may be compromised.
// In this example, ApsaraVideo VOD reads the AccessKey pair from the environment variables to implement identity verification for API access. Before you run the sample code, configure the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET.
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");
// Set CoverURL to the URL of the image that is generated by the snapshot task for capturing the first frame of the video.
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 {
// Specify the region in which ApsaraVideo VOD is activated.
String regionId = "cn-shanghai";
DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
DefaultAcsClient client = new DefaultAcsClient(profile);
return client;
}
}
Complete sample code
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. The following sample code is used to capture a video snapshot and change the video thumbnail. You can modify the code based on your business requirements.
* 2. You must add specific logic to the following sample code before you can use the code.
* 3. The following sample code is for reference only. You can use other methods to capture a video snapshot and modify the video thumbnail.
*/
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.
// We recommend that you do not include your AccessKey pair (AccessKey ID and AccessKey secret) in your project code. Otherwise, the AccessKey pair may be leaked and the security of all resources within your account may be compromised.
// In this example, ApsaraVideo VOD reads the AccessKey pair from the environment variables to implement identity verification for API access. Before you run the sample code, configure the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET.
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);
// The video ID.
String videoId = "a42bf540b1b37*******b79f0102";
// Scenario 1: The callback feature is enabled or Message Service (MNS) is activated
// You can set the thumbnail of a newly uploaded video only after the VideoAnalysisComplete callback is returned.
// To set the thumbnail of an existing video, call the submitSnapshotJob operation.
submitSnapshotJob(vodClient,videoId);
// After the SnapshotComplete callback is returned, obtain the snapshot type and the snapshot URL.
JSONObject callBackMessage = new JSONObject(); // Specify the callback message.
String snapshotType = callBackMessage.getJSONObject("UserData").getJSONObject("Extend").getString("SnapshotType");
if("FirstFrame".equals(snapshotType)){
// Specify the path in which the snapshot is stored based on your business requirements.
String coverUrl = callBackMessage.getJSONArray("SnapshotInfos").getJSONObject(0).getString("SnapshotRegular").replace("{SnapshotCount}","00001");
// Change the video thumbnail.
updateVideoInfo(vodClient,videoId,coverUrl);
}
// Scenario 2: The callback feature is not enabled and MNS is not activated
// To set the thumbnail of a newly uploaded video, use the round-robin method to obtain the video status.
String videoStatus = "";
while(!"Normal".equals(videoStatus)){
videoStatus = getVideoInfo(vodClient,videoId);
Thread.sleep(1000);
}
// To set the thumbnail of an existing video, call the submitSnapshotJob operation.
submitSnapshotJob(vodClient,videoId);
// Use the round-robin method to obtain the result of the snapshot task.
String coverUrl = "";
while(StringUtils.isBlank(coverUrl)){
coverUrl = listSnapshots(vodClient,videoId);
Thread.sleep(1000);
}
// Change the video thumbnail.
updateVideoInfo(vodClient,videoId,coverUrl);
}catch (Exception e){
}
}
/**
* Submit a snapshot task
*/
public static SubmitSnapshotJobResponse submitSnapshotJob(DefaultAcsClient vodClient, String vid) throws ClientException {
SubmitSnapshotJobRequest request = new SubmitSnapshotJobRequest();
request.setVideoId(vid);
request.setSnapshotTemplateId("1f27a7f*********70eba2756");
// Optional. The custom pass-through parameters. This parameter is used to identify the snapshot of the first frame. You can use the parameter in event callbacks.
request.setUserData("{\"Extend\":{\"SnapshotType\":\"FirstFrame\",\"VideoId\":\"a42bf540********33b79f0102\"}}");
return vodClient.getAcsResponse(request);
}
/**
* Change the video thumbnail
*/
public static UpdateVideoInfoResponse updateVideoInfo(DefaultAcsClient vodClient, String vid, String coverUrl) throws ClientException {
UpdateVideoInfoRequest request = new UpdateVideoInfoRequest();
request.setVideoId(vid);
// Set CoverURL to the URL of the image that is generated by the snapshot task for capturing the first frame of the video.
request.setCoverURL(coverUrl);
return vodClient.getAcsResponse(request);
}
/**
* Initialize the SDK instance
*/
public static DefaultAcsClient initVodClient(String accessKeyId, String accessKeySecret) throws ClientException {
// Specify the region in which ApsaraVideo VOD is activated.
String regionId = "cn-shanghai";
DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
DefaultAcsClient client = new DefaultAcsClient(profile);
return client;
}
/**
* Query the captured 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 information about a 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;
}
}