Video preview lets users watch or listen to content, such as videos and audio, for a limited time. This feature is common for paid services, such as paid courses and short-form dramas. This topic describes how to enable the video preview feature for ApsaraVideo VOD and obtain a preview URL.
How it works
Alibaba Cloud ApsaraVideo VOD (VOD) provides the preview feature. You can obtain a preview URL by setting a preview duration when you call the server-side GetPlayInfo API operation, or by manually constructing a signed URL that contains the preview information.
If the specified preview duration exceeds the total duration of the original video, users can play the entire video using the preview URL that is returned by ApsaraVideo VOD.
The video preview feature of ApsaraVideo VOD is implemented based on Alibaba Cloud CDN. The basic principle is as follows: The player sends a request that contains a preview URL (a CDN-accelerated domain name) with the specified preview duration. The cloud authenticates the preview URL. If the authentication is successful, the specified file content is returned. Otherwise, access is denied and a 403 error is returned. The following figure shows the video preview workflow.

You configure a CDN-accelerated domain name and enable the video preview feature.
ImportantBefore you can use the preview feature, you must configure a domain name and enable the preview feature. For more information, see Enable the preview feature.
Your player sends a preview request to ApsaraVideo VOD.
ApsaraVideo VOD generates a preview URL based on the domain name configuration and the preview duration specified in the request.
NoteYou can call a server-side API operation to generate a preview URL or construct a preview URL. For more information, see Call an API operation to obtain a preview URL or Manually construct a preview URL.
Your player sends a request to CDN to play the preview video.
Limits
The video preview feature supports the MP4 and HLS file formats. For MP4 videos, the metadata must be in the file header. MP4 videos with metadata at the end of the file are not supported. When you use ApsaraVideo VOD to transcode a video into the MP4 container format, the metadata is placed in the file header.
The preview duration depends on keyframes. By default, a keyframe is generated every 10 seconds for output files that are transcoded by ApsaraVideo VOD. Therefore, we recommend that you do not use the preview feature for short videos. For long videos, set the preview duration to at least 30 seconds.
NoteYou can modify the keyframe interval in transcoding templates. For more information, see Transcoding Templates.
For HLS files, the preview precision is based on the duration of a TS segment. The preview duration is rounded up to the nearest multiple of the TS segment duration. For example, if a TS segment is 10 seconds long and you set the preview duration to 15 seconds, a 20-second preview is returned.
Enable the video preview feature
Before you can call an API operation or manually construct a URL to obtain a preview URL, you must configure a domain name and enable the video preview feature. Follow these steps:
Configure a CDN-accelerated domain name. For more information, see Add an accelerated domain name.
ImportantThe video preview feature is available only for domain names that are configured as CDN-accelerated domain names.
You can enable URL signing for a domain name and also enable the preview feature. For more information, see URL signing.
ImportantIf the video preview feature is disabled for a domain name, you cannot include preview parameters in requests to ApsaraVideo VOD. The returned URL will be inaccessible. Therefore, you must turn on the Support Previewing switch when you enable URL signing.
The following figure shows the console interface for enabling the video preview feature.
NoteTo manually construct a preview URL, you must use the privateKey parameter to calculate the authentication value. Set this parameter to the value of the Primary Key or Secondary Key that you obtain from the console. Record the key value for future use.

Enable Range origin fetch and video seeking for the domain name. For more information, see Configure Range origin fetch and Video seeking.
Call an API operation to obtain a preview URL
ApsaraVideo VOD provides an API operation to obtain video playback URLs. For more information about the API operation, see Obtain audio and video playback URLs. You can integrate a server-side SDK and use the SDK to call this operation to obtain a preview URL. To obtain the preview URL, follow these steps:
Before you call this operation, make sure that the preview feature is enabled. For more information, see Enable the preview feature.
Integrate the appropriate server-side SDK. For more information, see Quick Start for ApsaraVideo VOD SDKs.
Call the Get audio and video playback URL API operation using the SDK. When you call the API operation, set the PreviewTime parameter within the PlayConfig parameter to specify the preview duration. The server returns a preview URL based on the specified duration. For more information about the PlayConfig parameter, see PlayConfig.
Manually construct a preview URL
You can manually construct a signed URL that contains preview information. Follow these steps:
Before you construct a preview URL, make sure that the preview feature is enabled. For more information, see Enable the preview feature.
Construct a signed URL that contains the preview parameters. Unlike when you construct a signed URL for full playback, you must include the preview duration parameter
previewTimein the MD5 hash calculation for URL signing.MD5 hash calculation for a full video URL
MD5 hash calculation for a preview URL
MD5(uri-timestamp-rand-uid-PrivateKey)MD5(uri-timestamp-rand-uid-PrivateKey-previewTime)NoteFor more information about the parameters for the MD5 hash calculation and how to manually construct a signed URL, see Configure URL signing. If you want to allow full playback of the video, do not include the previewTime parameter.
Append
&end=to the end of the constructed signed URL. Then, add the preview parameterpreviewTimeafter&end=to generate the complete preview URL.