Overview

ApsaraVideo VOD provides a complete preview solution. You can specify whether to allow users to preview a specified duration of a video or view the complete video. ApsaraVideo VOD provides streaming URLs that contain the specified preview duration to limit video playback.

Prerequisites

The preview solution adopts the following basic principle: The Content Delivery Network (CDN) URL that is used to play a specific video contains the specified preview duration for the video. When a request to play the video is received, Alibaba Cloud CDN authenticates the request. If the request passes the authentication, Alibaba Cloud CDN returns the specified content for preview. Otherwise, Alibaba Cloud CDN rejects the request and returns status code 403.
  • A domain name for CDN is configured in the ApsaraVideo VOD console. The preview feature is implemented on top of Alibaba Cloud CDN.
  • The authentication type A for URL signing is enabled. To prevent the preview duration from being tampered, the preview duration is included in the calculation result of the auth_key field.
  • The object chunking and video seeking features are enabled for the domain name. To enable the two features for the domain name, go to the Video Related tab on the configuration page of the domain name in the ApsaraVideo VOD console.

Procedure

  1. Enable the preview feature for the domain name. For more information, see the "Configuration" section of this topic.
    Note If the preview feature is disabled for the domain name, do not include the preview duration in your request for video playback. Otherwise, a streaming URL that you cannot access is returned.
  2. Send a request to ApsaraVideo VOD for video playback. In the request, set the preview duration. For more information, see the "Methods for obtaining preview URLs" section of this topic.
  3. Obtain the preview URL.
  4. Use the preview URL to access Alibaba Cloud CDN. Then, Alibaba Cloud CDN returns the specified content for preview.

Process

Process

Configuration

Log on to the ApsaraVideo VOD console. In the left-side navigation pane, choose Configuration Management > CDN Configuration > Domain Names. Click Configure for the domain name that you want to configure. On the configuration page of the domain name, click Resource Access Control. On the page that appears, click the URL Authentication tab. On the URL Authentication tab, enable the authentication type A for URL signing and the preview feature.Configuration

Methods for obtaining preview URLs

Use the ApsaraVideo VOD API
Note
  • To use the preview feature, make sure that a domain name for CDN is configured.
  • If the preview feature is disabled for the domain name, do not include the preview duration in your request for video playback. Otherwise, a streaming URL that you cannot access is returned.
  • ApsaraVideo VOD supports the preview of MP4 and M3U8 files.
  • The preview duration is determined based on keyframes. Therefore, we recommend that you do not apply the preview feature to short videos. For long videos, we recommend that you set the preview duration to at least 30 seconds. The default keyframe interval of transcoded files is 10 seconds. You can modify the keyframe interval in transcoding templates.
  • The preview granularity of M3U8 files is the duration of each TS segment. If the specified preview duration is not an integral multiple of the TS segment duration, the preview duration is extended. For example, if the TS segment duration is 10 seconds and the preview duration is 15 seconds, Alibaba Cloud CDN returns the specified content of 20 seconds for preview.
You can set the preview duration in the PreviewTime parameter of PlayConfig when you call the GetPlayInfo operation.
The following code provides an example:
package com.ali.vod.test;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.oss.ClientException;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoRequest;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoResponse;

/**
 * @author wb-zzb
 * @date 2020/6/3
 */
public class VodPreviewTest {
    public static void main(String[] args) {
        // Select a region of ApsaraVideo VOD. For more information, see [VOD centers and access domains](https://help.aliyun.com/document_detail/98194.html?spm=a2c4g.11186623.6.612.51c6534bqLs9Wd).
        String regionId = "cn-shanghai";
        String accessKeyId = "<your accessKeyId>";
        String accessKeySecret = "<your accessKeySecret>";
        String videoId = "595d020bad3*****f37433451720";
        DefaultAcsClient client = InitVodClient(regionId, accessKeyId, accessKeySecret);
        GetPlayInfoResponse response = null;
        try {
            response = getPlayInfo(client, videoId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("response = " + JSONObject.toJSONString(response));

    }

    /**
     * Initialize the client.
     *
     * @param regionId
     * @param accessKeyId
     * @param accessKeySecret
     * @return
     * @throws ClientException
     */
    public static DefaultAcsClient InitVodClient(String regionId, String accessKeyId, String accessKeySecret) throws ClientException {
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
        DefaultAcsClient client = new DefaultAcsClient(profile);
        return client;
    }

    /**
     * Obtain the streaming URL.
     *
     * @param client
     * @param videoId
     * @return
     * @throws Exception
     */
    public static GetPlayInfoResponse getPlayInfo(DefaultAcsClient client, String videoId) throws Exception {
        GetPlayInfoRequest request = new GetPlayInfoRequest();
        request.setVideoId(videoId);
        // Set the expiration time, in seconds. Default value: 3600.
        request.setAuthTimeout(3600L);
        request.setFormats("mp4");
        JSONObject playConfig = new JSONObject();
        // Set the preview duration, in seconds. Minimum value: 1.
        playConfig.put("PreviewTime", "30");
        request.setPlayConfig(playConfig.toJSONString());
        return client.getAcsResponse(request);
    }
}
                

Manually calculate the preview URL

  • If the auth_key field in the URL of a video contains the preview duration, include the preview duration in the calculation result of the md5hash field for URL signing. Use the following method to calculate the md5hash field: MD5(uri-timestamp-rand-uid-auth_key-preview_time).
  • Add &end={Preview duration} to the end of the URL. To allow users to view the complete video, do not set the preview duration and do not include the preview duration in the calculation result of auth_key.
The following code provides an example:
    private String generateRand() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    public String genAuthKey(String object, String privateKey, Long expireTime, Long previewTime) {
        String rand = "0";
        String uid = "0";
        if (StringUtils.isBlank(privateKey)) {
            return "";
        }
        rand = generateRand();
        long timestamp = System.currentTimeMillis() / 1000 + (expireTime == null ? 0 : expireTime);
        String authStr = timestamp + "-" + rand + "-" + uid;
        String md5Str = object + "-" + authStr + "-" + privateKey;
        if(previewTime! =0)
            md5Str = md5Str + "-" + previewTime;
        String auth_key = authStr + "-" + MD5Util.md5( md5Str);
        return auth_key;
    }

    public void previewTest() throws Exception {
        try {
            String key = "your cdn auth key";
            String fileUrl = "http://test.yourdomain.com/test/bee21427ca3346848835c1bd786054c5-19bd8528c1d51576cd726cf86471ca0****.mp4";
            URL url = new URL(fileUrl);
            String file = url.getFile();
            Long previewtime = 120L;
            Long expireTime = 1800L;
            String auth_key =genAuthKey(file, key, expireTime, previewtime);
            fileUrl = fileUrl + "? auth_key=" + auth_key;
            if(previewtime ! = 0)
                fileUrl = fileUrl + "&end=" + previewtime;
            System.out.println(fileUrl);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }